【问题标题】:passing checkbox ID to another php file将复选框 ID 传递给另一个 php 文件
【发布时间】:2012-06-22 14:22:55
【问题描述】:

假设我正在生成某种 html 表(来自 mysql 查询数据),每行都有一个复选框,类似于

$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 文件

我可以访问 $row['ID'] ,我可以用它来命名每个复选框,我只是不确定如何合并它,因为我是 php 新手


更新 以下似乎适用于我的目的

我有以下 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如下-

<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 查询完成所有您需要的操作,并且可以将新数据发送回并显示

<div id='ajaxDiv'></div>

【问题讨论】:

  • 这通常在客户端使用一些 javascript 代码完成。

标签: php mysql checkbox parameter-passing


【解决方案1】:

为了获得清晰的代码,我建议您避免两个按钮触发两个多个动作。而是使用简单的 javascript 方法将参数附加到提交的表单中,以摆脱按下的按钮。

<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)。

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

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

提交表单后,您将收到一个数组“selectedboxes”作为参数,您可以在其中选中复选框。

【讨论】:

  • 谢谢,这看起来像我需要的。你能帮我完成javascript吗?我需要某种 if 语句,例如: if act = 1 do bla bla1;如果 act = 2 do bla bla2 我应该能够将所有其余部分拼凑在一起。谢谢!
  • 你不需要 ajax.. submit 方法会带你到另一个你处理你的请求的页面。
  • 好的,这是有道理的,但我不太确定如何从这个新页面访问 id (act)。你能告诉我如何在其他页面上快速编写代码吗?
  • 我上面写的例子会给你一个 "$_REQUEST['act'] ,你会根据你按下的按钮找到值 1 或 2。
  • 好的,太好了!如何从另一个 php 页面中的复选框访问 ID?以及如何通过提交方法“激活”其他 php 页面?
【解决方案2】:

您可以更改表单操作,例如,如果您单击按钮 1,它会将操作更改为 DELETE.PHP,请查看此帖子:

Changing the action of a form with javascript/jquery

他们使用 JQuery 并且他们有多个答案如何做到这一点

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2015-05-11
    • 2015-08-19
    • 1970-01-01
    • 2012-06-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多