问题
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>
回答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.
回答2:
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
来源:https://stackoverflow.com/questions/11119719/passing-checkbox-id-to-another-php-file