将checkbox ID传递给另一个php文件 [英] passing checkbox ID to another php file

查看:138
本文介绍了将checkbox ID传递给另一个php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我生成一些html表(从mysql查询数据),每行有一个复选框,如

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>";

我想要发生的事情,在选择了各种复选框后,有两件事:
1)例如,当单击Button1以删除所选行时,调用delete.php。
2)点击Button2时调用一些其他php文件

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

我可以访问$ row ['ID'],我只是不知道如何整合它,因为我是新的php

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
以下似乎适用于我的目的

UPDATE The following seems to work for my purposes

我有以下html -

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如下 -

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!');
}

然后使用delete_row.php,你可以做一个mysql查询,并且新数据可以被发回并用

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>


推荐答案

为了获得一个明确的代码,我建议你避免两按钮发射两个几个动作。而是使用一个简单的javascript方法来附加一个参数到提交的表单中去掉已经推送的按钮。

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>

这只是一个例子(使用jquery)。

this is just an example (using jquery).

关于第二个答案(复选框):

About your second answer (checkboxes):

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

提交表单时,您将收到一个数组selectedboxes作为参数复选框。

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

这篇关于将checkbox ID传递给另一个php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆