I have a question. I have a php file with queries of delete, insert, and to display data. There's a delete button in the file as well. To execute the delete query, it'll be something like
if($_POST['delete'])
{
//execute delete query
}
which is in the PHP file. Now I have implemented ajax and I want to execute the delete query only when the button is clicked, but by using a function in javascript(I'm not too sure if this is the correct way or most suitable way). So everytime the delete button is clicked, a javascript function would be executed so that ajax would work. However it doesn't seem to do anything. Am I doing it the right way by using a javascript function to execute on button click or is there any other better ways? I'm doing this because I would like all the queries to be in one file so that's why I'm using an if
statement to detect whether the button was clicked previously.
P.S. I'm using pure javascript codes as I am still learning so if there are any suggestions, I would prefer the raw codes rather than jQuery. Do comment if you would like to see the codes as I have asked a similar question before but I didn't find the answer I was looking for.
EDIT: Here are the codes
This is the javascript external file(for delete function only)
function deleteButtonClick(){
//Perform the Ajax request to delete this user
var del = document.getElementById("delete").innerHTML;
var delRow = document.getElementById("deleteRow").innerHTML;
var page = "database.php";
var parameters = 'delete=true&deleteRow='+delRow;
var xmlhttp = new XMLHttpRequest();
if(confirm('Are you sure you want to delete this?')==true){
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
return false;
}
}
xmlhttp.open("GET", page+"?"+parameters, true);
xmlhttp.send(null);
}
}
These are the codes in the PHP file to execute the query
// Delete Row
if(isset($_POST['delete'])){
$id = $_POST['deleteRow'];
$query_string = "delete from $table_info where user_id='$id'";
$result = @mysql_query($query_string);
echo "row deleted";
}
and these are the codes in the same PHP file, for the delete button at every row in a table
echo "<td><input type='hidden' id='deleteRow' value=".$row['user_id']."/>";
echo "<input type='button' id='delete' value='Delete' onclick='deleteButtonClick()'/></td>";
echo "</tr></form>";