I have this inside a while loop. Everytime I confirm the onclick delete of any table row it always deletes the last row.
echo '<td><img src=img/view.png width=20px height=20px id="view"></td>';
echo '<td><img src="img/delete.png" width=20px height=20px name="delete"></td></tr>';
?>
<script type="text/javascript">
function deleteShit()
{
if (confirm('Delete?'))
window.location='delete.php?id=<?php echo $id;?>'
}
this is the delete.php page
$id=$_GET['id'];
$query = "UPDATE `main` SET status=0 where id = $id";
$sql = $db->prepare($query);
if ($sql->execute()) {
echo "<script>
window.alert('Item deleted!')
window.location='view.php';
</script>";
}
You must write your javascript function outer the while, with an arg :
<script type="text/javascript">
function deleteShit(id)
{
if (confirm('Delete '+id+'?')) {
window.location = 'delete.php?id='+id;
}
}
</script>
And call the function with the arg :
echo '<td><img src="img/delete.png" width=20px height=20px name="delete"></td></tr>';
Related
I am trying to pass div id to javascript and print it out on console as :
<?php echo 67;?>
The 67 is id here. I was wondering how can I store this value in my javascript variable storeid as var storeid = 67?
My code:
echo "<h2 class='editme' id='<?php echo $id;?>' contentEditable='true'> Hello</h2>";
echo "<script>
$('h2.editme').click(function(){
console.log(this.id);
var storeid;
});
</script>";
Also, I want to use that id value to update the values in my sql. I can only test my code once I could somehow get the value of that id. How can I store the value of the id to javascript variable?
My code (This is to update sql once I get the id) - Not sure if it's right I can test it after getting id value:
echo "<h2 class='editme' id='<?php echo $id;?>' contentEditable='true'> Hello</h2>";
echo "<script>
$('h2.editme').click(function(){
var content2 = $(this).html();
console.log(this.id);
var storeid;//once I get the id
$.ajax({
url: 'updateDescription(storeid, content2)',
success: function(respon) {
$('.editme').html(respon);
}
});
});
</script>";
function updateDescription($user_unique_id, $content2)
{
try {
require "testing.php";
$sql = "UPDATE icecream SET
desc = :desc
WHERE id = :id";
$stmt->bindParam(':desc', $content2, PDO::PARAM_STR);
$stmt->bindParam(':id', $user_unique_id);
$stmt->execute();
echo 'Record updated';
} catch (PDOException $e) {
echo 'Connection failed ' . $e->getMessage();
}
}
this way:
echo '<script>
var storeid = '. $id .'
</script>';
more readable way:
// somefile.php
<?php
$id = 67;
?>
<h2 class='editme' id='<?php echo $id;?>' contentEditable='true'> Hello</h2>
<script>
var storeid = <?php echo $id; ?>
// rest of javascript code
</script>
<?php
// rest of the php code
Hey guys I have this code that gets data from my database and posts it to an html table. I want to send a particular row or cell data corresponding to the button,once the button is clicked, to a paragraph element I used this
console.log(this.childNodes[0].innerHTML); // column 1 in row1
but it doesn't work
Here is my code
<?php
if(isset($_POST['searchbox'])){
$bloodonation =$_POST['searchbox'];
$multiple= explode(',',$bloodonation);
$var1 = $multiple[0]; // firstname
$var2 = $multiple[1]; // fathername
$var3 = $multiple[2]; // lastname /*bloodtype.blood_type='$var4' AND bodytype.bodytype='$var5' AND */
$_SESSION["firstname"] = $var1;
$_SESSION["fathername"] = $var2;
$_SESSION["lastname"] = $var3;
if(!empty($bloodonation)){
//$myfile = fopen("file.txt", "w");
//file_put_contents('file.txt',$bloodonation);
//fclose($myfile);
$bloodquery ="SELECT personprofile.firstname,personprofile.fathername,personprofile.lastname,bloodtype.blood_type,bodytype.bodytype,personprofile.bloodoner,personprofile.organdoner
FROM personprofile,bloodtype,bodytype
WHERE personprofile.bloodtype= bloodtype.id AND
personprofile.hascancer ='No' AND personprofile.chronicdisease= 'No' AND personprofile.autoimmunedisease= 'No' AND
personprofile.bodytype= bodytype.id";
//$sql = "SELECT `firstname`, `fathername`, `lastname` FROM `personprofile` WHERE chronicdisease=\"No\" AND hascancer=\"No\" AND autoimmunedisease=\"No\"";
$bloodqr=mysqli_query($link,$bloodquery);
echo "<table>";
echo "<tr><th>Firstname</th><th>Fathername</th><th>Lastname</th><th>Blood type</th><th>Body type</th></tr> ";
while($row=mysqli_fetch_assoc($bloodqr)){
echo"<tr><td>";
echo $row['firstname'];
echo "</td><td>";
echo $row['fathername'];
echo "</td><td>";
echo $row['lastname'];
echo "</td><td>";
echo $row['blood_type'];
echo "</td><td>";
echo $row['bodytype'];
echo "</td><td>";?><html><button onclick="outputdata()">Send Email</button></html> <?php
echo"</td></tr>";
}
}
} ?>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var paragraphdata = document.getElementById("demo");
function outputdata() {
console.log(this.childNodes[0].innerHTML); // col1
console.log(this.childNodes[1].innerHTML); // col2
paragraphdata.innerHTML = "Geolocation is not supported by this browser.";
}
</script>
</body>
</html>
You can try to use like this method.
Firstly you should set an unique id in the tr tag.
And you can get data of the td tag with the tr tag id.
echo '<tr id="'.$unique_id.'">';
$("button").click(function(){
console.log($(this).closest('tr').index()); // Get the value of tr tag id.
}
And also you can try to use this link
I have this code that deletes a student record from the database, it deletes the student information as well as where an image is stored on the server however, I also want it to delete the image file itself, not just the record. How can I do this.
This code is where the user clicks to delete the student.
<a href="javascript:void();" onclick="deleteItem
(<?php echo $student_row->id;?>,<?php echo $_REQUEST['regNumber'];?>,
<?php echo $student_row->id;?>,'parent')">Delete</a>
This code is the JS code referenced above:
function deleteItem(registration_number,parent_id,id,type)
{
var parent_id = "<?php echo $_REQUEST['regNumber'];?>";
var url_pass="<?php echo get_site_url();?>/student-delete/?
regNoIndivid="+registration_number+
"&parentId="+parent_id+"&id="+id+"&type="+type;
if (confirm("Are you sure?")) {
window.location.href = url_pass;
}
return false;
}
This is from student-delete:
if($_REQUEST['type'] == "teacher")
{
$where=array("id"=>$_REQUEST['id']);
//echo "<pre>";print_r($where);echo "</pre>";
//$delete_row=$wpdb->delete('wp_new_student_user', $where);
$delete_row = $wpdb->query('DELETE FROM wp_new_student_user WHERE id
='.$_REQUEST['id']);
$wpdb->show_errors();
if($delete_row)
{
$url_location=get_site_url()."/my-account-teacher/?
regNumber=".$_REQUEST['parentId']."&type=tea&back_list=yes";
?>
<script type="text/javascript">
window.location.href="<?php echo $url_location;?>";
</script>
<?php
}
}
elseif ($_REQUEST['type'] == "parent") {
$where=array("id"=>$_REQUEST['id']);
//echo "<pre>";print_r($where);echo "</pre>";
//$delete_row=$wpdb->delete('wp_new_student_user', $where);
$delete_row = $wpdb->query('DELETE FROM wp_new_student_user WHERE id
='.$_REQUEST['id']);
$wpdb->show_errors();
if($delete_row)
{
$url_location=get_site_url()."/my-account-parent/?
regNumber=".$_REQUEST['parentId']."&type=par&back_list=yes";
?>
<script type="text/javascript">
window.location.href="<?php echo $url_location;?>";
</script>
<?php
}
}
Please use PHP UNLINK function to delete the file
if($delete_row)
{
unlink(Path to the file);
}
Please see the scripts below. Onclick of Add gives an error when a php variable ($var)is used, however it will work with a number - i.e. if the line in index.php:
echo '<button id="1" onclick="company_add(\''.$var.'\');">Add</button>';
Is changed to something like:
echo '<button id="1" onclick="company_add(',57776,');">Add</button>';
What am I missing please?
Index.php:
<html>
<head>
<script type ="text/javascript">
function company_add(company_name) {
$.post('company_add.php', {company_name:company_name}, function(data) {
if (data == 'success'){
alert("Cool");
} else{
alert(data);
}
});
}
</script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<?php
include 'connect.php'; //Generic connect file
$var = 'Name';
echo '<button id="1" onclick="company_add(\''.$var.'\');">Add</button>
<br/>';
?>
</body>
</html>
company_add.php:
<?php
include 'connect.php';
function company_exists($company_name) {
return (mysql_result(mysql_query("SELECT COUNT(`company_name`) FROM
`company` WHERE `company_name` = $company_name"), 0) == 0 ) ? false :
true;
}
function add_company($company_name){
mysql_query("INSERT INTO `company` (`id`, `company_name`) values ('',
".$company_name.")");
}
$company_name = $_POST['company_name'];
if (company_exists($company_name) === true) {
echo 'Company already added';
} else {
add_company($company_name);
echo 'success';
}
?>
Use that line like this:
echo "<button id='1' onclick='company_add('" . $var . "');'>Add</button>";
In case if you already have commas after and before the value of the $var you should trim it.
So use it like this:
$var = ltrim(",", $var);
$var = rtrim(", ", $var);
echo "<button id='1' onclick='company_add('" . $var . "');'>Add</button>";
And for your information yes you can even use a String instead of a Number too.
And UPDATE the functions:
function company_exists($company_name) {
$company_name = mysql_real_escape_string($company_name);
$query = "SELECT * FROM company WHERE company_name = '{$company}'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
return true;
}else{
return false;
}
}
function add_company($company_name){
$company_name = mysql_real_escape_string($company_name);
$query = "INSERT INTO company (id, company_name) VALUES ('', '{$company_name}')";
return mysql_query($query);
}
If you are using id field of that company table as AUTO_INCREMENT then you can leave the id field NAME & VALUE in the INSERT Statement.
Like This in the add_company Function:
$query = "INSERT INTO company (company_name) VALUES ('{$company_name}')"
How can I collect the data on the row from the table that I select and use it in the result?
Here is the javascript I am using to show the data entry screen, once the function has been called by selecting the row. Now I just need to design a form in PHP that will include (1) some of the data from the row selected and (2) some new data that will be collected.
Here is the Javascript to select the row and call the data entry form
$(document).ready(function () {
$("tr").live('click',function(){
$.post('data_entry_form.php', function(data) {
$('#section2').html(data);
});
});
});
Here is the PHP Script
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif ORDER BY aif_id DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table id=\"all_aifs\">";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
The "Correct" answer to this problem is NOT to read from the DOM. Never a good idea. I suggest you pass the record id to the ajax call and have the ajax call return an already-populated form.
//PHP
//place the data attribute in the tr
echo "<tr data-recordId='".$row['id']."'>";
//JS
$(document).ready(function () {
$("tr").live('click',function(){
//Get the ID from the row clicked
var id = $(this).data('recordId');
//short-hand
$('#section2').load('data_entry_form.php?id='+id);
});
});
Then your ajax page would just read $_REQUEST['id'] to get the id of the form being edited.
//Ajax side PHP
$id = (int)$_REQUEST['id'];
//Fetch data and use it to pre-populate form item
You would pre-populate your inputs like this
<input type="text" value="<?php echo $row['value']; ?>" />
or
echo '<input type="text" value="'.$row['value'].'" />';
NOTE: If your values contain quotes, you will want to replace them with the code "
echo '<input type="text" value="'.str_replace('"', '"', $row['value']).'" />';
Within the event handler, this and $(this) refer to your selected row:
$("tr").live('click',function(){
// this.cells[1] is the cell that contains Pubco Name
// You can also use $(this).children("td")[1]
...
});