0

I have a list on one of my website pages that acts like a delete button, however I was wondering if you could call a php script when the user clicks on it, a bit like a submit button.

Here is the list:

<li>
    <a class="actions_dropdown" href="#">&nbsp;</a>
    <ul class="actions_content">
        <li><a href="#">Delete</a></li>
        <li><a href="#">Report</a></li>
        <li><a href="#">Mark as Read</a></li>
        <li><a href="#">Mark as Unread</a></li>
        <li><a href="#">Move to Folder 1</a></li>
        <!----whole loop of folders--->
    </ul>
</li>

Is there anyway so when the user clicks on, say the delete li that it calls a php script? I can't seem to work it out.

1
  • may I ask why is this tagged "mysql"? Commented Aug 12, 2012 at 11:59

2 Answers 2

1

assign a class to your Delete li like

<li><a class="actions_dropdown" href="#">&nbsp;</a>
                <ul class="actions_content">
                    <li class="del"><a href="#">Delete</a></li>
                    <li><a href="#">Report</a></li>
                    <li><a href="#">Mark as Read</a></li>
                    <li><a href="#">Mark as Unread</a></li>
                    <li><a href="#">Move to Folder 1</a></li>
                    <!----whole loop of folders--->
                </ul>
               </li>

now attach a click event handler to the li with class del

$("li.del").click(function(e){  
var db = $(':checkbox:checked').map(function(i,n) {
    return $(n).val();
}).get(); //get converts it to an array

if(db.length == 0) { 
    db = "none"; 
}        
 $.post('/test.php',{'db[]':db},function(data){    
 console.log(data);//it should log data deleted successfully     
 });    
});

in test.php

<?php

 //delete logic here
 echo json_encode("data deleted successfully");

?>

Post array of multiple checkbox values

Sign up to request clarification or add additional context in comments.

5 Comments

ok brilliant, that sounds good, the only problem i have is that when the user presses the delete button, they are deleting data that is selected via checkbox, in other words how would i get the checked box values into the test.php, normally if the php is on the same page i just POST them, but how would i do it via ajax? Thanks for all the help
ok brilliant that looks great, i only have one more question, the whole reason of this is for a mailbox, so the checkboxes being selected have a value of the $message_id which will allow me to delete them all in the php script, will the ajax send the value of the checkbox as well, say my checkbox looked like this and the value number is the $message_id <input name="inbox_select" class="global_select" type="checkbox" value="inbox4" /> Thanks again for your great help
the ajax will not do any thing on its own try to understand the code, the map function described above puts the value of checked checkboxes in an array and that array of values is then shipped off to the server via ajax
in your case inbox4 will be sent
ok brilliant so if i selected inbox4, inbox5, and inbox7 they would all get sent as inbox4, inbox5, inbox7?
0

You can simply set the anchor's href to your PHP page, but as with a standard form submission that would either navigate away from the current page or cause the current page to reload (depending on what your PHP returned).

So probably you want to make an Ajax request. Unlike standard form submission, Ajax doesn't cause the current page to reload, it returns the response back to your JS code.

Assuming you can add an id to your li or anchor element (e.g., id="deleteLink"), or otherwise identify it, you can bind a click handler that does the Ajax:

$("#deleteLink").click(function(e) {
    e.preventDefault();

    $.ajax({
       url: "yourPHPpageHere.php",
       data: { paramName1 : paramValue, paramName2 : paramValue2 },
       dataType : "json"   // or "html", "xml", or "text", whatever your PHP returns
    }).done(function( response ) {
       // optionally do something with response.
    });
});

Any data you need to pass to the delete PHP (presumably some kind of record id at minimum) can be set in the data parameter of the $.ajax() call.

For more detail about the $.ajax() method see the jQuery doco.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.