繁体   English   中英

如何用不同的动作提交表格

[英]how to submit the form with a different actions

我正在尝试使用具有相同名称的提交按钮从数据库中检索信息。 我给的值是行的id,因此我可以执行其他操作,但是我不能确定该怎么做。

例如当尝试这样

<form name="form" action="index.php" method="post">
<input type="image" name="x" value="1">

<input type="image" name="x" value="2">
</form>

和PHP =>

<?php
if (isset($_POST['x'])){
    echo $_POST['x'];
}
?>

在chrome中可以正常工作,但是在其他浏览器中不能正常工作吗? 谢谢

更新

if ($r = $con->query("SELECT * FROM table")){
    if ($row = $r->fetch_assoc()){
        echo "<input type='image' name='submit' value='" . $row['id'] . "'>";
        // and other data which I need to
    }
    $r->free();
}

我的意思是这样

的HTML

<form id="form1" action="index.php" method="post">
   <input type="button" name="x" value="1">    
   <input type="button" name="x" value="2">
   <input type="text" name="action" id="action">
</form>​

JavaScript

var nodes = document.getElementById('form1').childNodes;
for(var i=0; i<nodes.length; i++) {    
    nodes[i].onclick = function(){        
        if(this.name == 'x'){
          document.getElementById('action').value = this.value;
        }
    };
}

的PHP

if(isset($_POST['action'])){

   if($_POST['action'] == "1"){
      ...
   }

   if($_POST['action'] == "2"){
      ...
   }

}

http://jsfiddle.net/qLubz/

准备部署时,只需将type="text"更改为type="hidden"即可。

另外,也可以不使用JavaScript:

<form action="index.php" method="post">
   <button type="submit" name="action" value="delete">Delete</button>
</form>​
<form action="index.php" method="post">
   <button type="submit" name="action" value="copy">Copy</button>
</form>​
<form action="index.php" method="post">
   <button type="submit" name="action" value="edit">Edit</button>
</form>​

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM