0

let's say i'm generating some sort of html table (from mysql queried data) with a checkbox at each row with something like

$display_string = "<form action=\"delete.php\" method=\"post\">";
$display_string .= "<div><input type=\"button\" VALUE=\"Button1\"></div>";
$display_string .= "<div><input type=\"button\" VALUE=\"Button2\"></div>";
while($row = mysql_fetch_array($qry_result)){
    $display_string .= "<tr onmouseover=\"this.className = 'hlt';\" onmouseout=\"this.className = '';\">";
    $display_string .= "<td class='blank'><input type=\"checkbox\" /></td>";
    $display_string .= "<td class='data'>" . $row['first_name'] . "</td>";
    $display_string .= "<td class='data'>" . $row['last_name'] . "</td>";
    $display_string .= "<td class='data'><a href='" . $row['email'] . "'>" . $row['email'] . "</a></td>";
etc...
etc...
$display_string .= "</form>";

what i'd like to happen now, after various checkboxes are selected are two things: 1) for example, call delete.php when Button1 is clicked to delete the selected rows. 2) some other php file called when Button2 is clicked

I have access to $row['ID'] which I can use to name each checkbox, i'm just not sure how to incorporate it since i'm new to php


UPDATE The following seems to work for my purposes

I've got the following html-

<form name='myForm' method=\"post\" >
<input type=\"submit\" onClick=\"deleterow(document.myForm)\" VALUE=\"Delete ROWs\">

while($row = mysql_fetch_array($qry_result)){
    <input type=\"checkbox\" name='rows' value=" .$row['ID']. "/>

Javascript is as follows-

<script language=\"javascript\" type=\"text/javascript\">
function deleterow(form){

    if (!confirm(\"Are you sure you want to delete?\")) return false;

    var queryString = \"?ID=\";

    for (var i = 0; i < document.myForm.rows.length; i++) {
        if (document.myForm.rows[i].checked) {
            ID = document.myForm.rows[i].value;
            ID = ID.slice(0, -1);
            queryString += ID;
            queryString += \"-\";
        }
    }
    queryString = queryString.slice(0, -1);

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");
            } catch (e){
                // Something went wrong
                alert(\"Your browser broke!\");
                return false;
            }
        }
    }

    var ajaxRequest;  // The variable that makes Ajax possible!
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }

    ajaxRequest.open(\"GET\", \"delete_row.php\" + queryString, true);
    ajaxRequest.send(null); 
    confirm('Delete successful!');
}

then with delete_row.php you can do all you need to with a mysql query, and new data can be sent back and displayed with

<div id='ajaxDiv'></div>
Constantino
  • 2,243
  • 2
  • 24
  • 41

2 Answers2

1

To get a clear code I suggest you to avoid two buttons firing two several actions. Instead use a simple javascript method to append a parameter to the submitted form to get rid of what button has been pushed.

<script type="text/javascript">
    function submitForm( act ){
         $('#act').val(act);
         $('#myForm').submit();
}

</script>

<form id="myForm" ... >
<input type="hidden" id="act" value="" />

<button onClick="submitForm(1)">1</button>
<button onClick="submitForm(2)">2</button>
</form>

this is just an example (using jquery).

About your second answer (checkboxes):

<input type="checkbox" name="selectedboxes[]" value="someIdHere" />

when the form is submitted you'll receive an array "selectedboxes" as a param in which you get checked checkboxes.

Gianpaolo Di Nino
  • 1,139
  • 5
  • 17
  • thanks, this looks like what i need. can you help me complete the javascript though? i need some sort of if statement like: if act = 1 do bla bla1; if act = 2 do bla bla2 i should be able to piece all the rest together. thanks! – Constantino Jun 20 '12 at 22:43
  • you don't need ajax.. submit method will bring you to another page in which you handle your request. – Gianpaolo Di Nino Jun 21 '12 at 15:55
  • ok, that makes sense but i'm not quite sure how to access the id (act) from this new page. could you show me a quick code writeup on how i'd do this on the other page? – Constantino Jun 21 '12 at 16:03
  • the example i wrote above will pass you a "$_REQUEST['act'] in which you'll find the value 1 or 2 based on the button you pushed. – Gianpaolo Di Nino Jun 22 '12 at 14:09
  • ok great! how can i access the ID from the checkbox from within another php page? and how to i 'activate' that other php page through the submit method? – Constantino Jun 22 '12 at 17:33
  • dear @user1153897 this is basic html with a bit of php. Make your tries, write some code, and try to learn by yourself. Writing code for you is not my job, none else wants here :) – Gianpaolo Di Nino Jun 23 '12 at 14:20
  • and, by the way, the answer for your question is already in my answer.. just read it better – Gianpaolo Di Nino Jun 23 '12 at 23:54
1

You can do it changing the Form action, for example if you click BUTTON 1 it will change the action to DELETE.PHP, take a look to this post:

Changing the action of a form with javascript/jquery

They use JQuery and they have multiples answer how to do it

Community
  • 1
  • 1
jcho360
  • 3,724
  • 1
  • 15
  • 24