So, I’m working on a small web app and I have it to a point where I can input data into a form, click a button, and delete that data from a form. I’d like to be able to just click on the data in a table and have it do a similar function, i.e. somehow use $.post to post the data instead of making the form do it. I’m at a loss of how to do it though. I’ve read several tutorials and articles and am at a loss of how to replicate the form’s submit functionality.
Here’s my current way of doing things via form/php:
The Form Template
<form name="DeleteMarker" action="index.php" target="_self" method="post">
<fieldset class="Splice">
<legend>Delete Marker</legend>
<label>Employee: </label><select name="DEmployee">
<label>Job Name: </label><select name="DJob">
<label>Customer Name: </label><select name="DCustomer">
<br>
<label>Latitude: </label><input name="DLatitude" min="-180" max="180" step="0.00001" required />
<label>Longitude: </label><input name="DLongitude" min="-180" max="180" step="0.00001" required />
<br>
<input type="submit" value="Delete" />
</fieldset>
</form>
The PHP
if(isset($_POST['DEmployee'])) {
$sql="DELETE FROM Splices WHERE (Employee='$_POST[DEmployee]' AND Job='$_POST[DJob]' AND Customer='$_POST[DCustomer]' AND Latitude=$_POST[DLatitude] AND Longitude=$_POST[DLongitude])";
if (!mysql_query($sql,$Splices)) {
die('Error: ' . mysql_error());
}
I have a javascript function where a click on the table will display an alert, I just need to figure out how to put a $.post or $.ajax statement in there to replicate the functionality of the form I already have.
The (incorrect) javascript I have:
function deleteRecord(e,j,c,l,ln) {
confirm("Delete Record ("+e+", "+j+", "+c+", "+l+", "+ln+")?");
if(confirm){
$.post("index.php", {DEmployee:e, DJob:j,DCustomer:c,DLatitude:l,DLongitude:ln});;
}
}
you did not mention about jQuery, but $.post is an available method on jQuery. if you are using jQuery and include it to your page it’s ok. than you can do that like this,
EDIT:
Actually your
deleteRecord()function has some other incorrect usage.confirm()returns a boolean(true or false), so you have to use this returned value to continue your code.if(confirm) {}is not right way to do it,you must use it like,
or more useful,
you said ‘the url is still just “index.php”‘, how did you look that, I don’t know but it’s normal, because the method is not GET, so url will not include posted data as querystring. If it doesn’t work, there might be some other problem.
EDIT-2:
Actually there is no differences between them, ok here is a full example, you can look your browser javascript dev-tool to see what’s happening on background.