提交from,从ajax请求处理

时间:2015-07-13 15:24:50

标签: javascript php jquery ajax forms

<head>
<script>
function showit(str) {
      if (str.length==0) { 
        document.getElementById("txtHint").innerHTML="";
        return;
      }
      var xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","showuser.php?q="+str,true);
      xmlhttp.send();
    }       
}
</script>
<head>
<body>
<button onclick='showit(1)'>First User</button>
<button onclick='showit(2)'>Second User</button>
<button onclick='showit(3)'>Third User</button>
<?php
if(isset($_POST['submit'])){

?>
<div id="txtHint">
</div>
</body>

这是主页面,当我单击按钮时,它会向showuser.php处理Ajax请求并为我提供表格。

SHOWUSER.PHP

<?php
if(isset($_GET['q'])){
    $id=$_GET['q'];
    $con = mysqli_connect("localhost","root","","application");
    $sql="SELECT * FROM users WHERE id=$id";
    $result=mysqli_query($con,$sql);
    echo "<table>";
    while($row=mysqli_fetch_array($result)){
        echo "<form method='post'>";
        echo "<tr><td><input type='radio' name='status' value='delete'>Delete</td><td><input type='radio' name='status' value='update' checked>Update</td></tr>";
        echo "<tr><td>ID:<td><input type='text' name='id' value='".$row[id]."'></td></tr><tr><td>Name:<td><input type='text' name='uname' value='".$row[uname]."'></td></tr><tr><td>Store:<td><input type='text' value='".$row[store]."' disabled></td><td>";
        echo "<select name='store'>";
        $displaystore=new admin();
        $displaystore->storeoption();
        echo"</select>";
        echo "</td></tr><tr><td>Date:<td><input type='text' name='date' value='".$row[date]."'></td></tr>";
        echo "<tr><td></td><td><input type='submit' name='submit' value='submit'></td></form>";     
    }
    echo "</table>";
}
?>

它显示表格,但是当我点击时,它不起作用!!那是当我从表单中单击“提交”按钮时,没有任何反应!!!
有人可以帮忙吗?谢谢你。

4 个答案:

答案 0 :(得分:1)

表格内的表格。这似乎是一件坏事。但我认为它不起作用,因为表格缺少一个动作

<form method="post" action="">

答案 1 :(得分:1)

你没有对帖子数据做任何事情,在这一行

<?php
if($_POST) { print_r($_POST); }

if(isset($_GET['q'])){
    $id=$_GET['q'];
    $con = mysqli_connect("localhost","root","","application");
    $sql="SELECT * FROM users WHERE id=$id";
    $result=mysqli_query($con,$sql);
    echo "<table>";
    while($row=mysqli_fetch_array($result)){
        echo "<form method='post'>";
        echo "<tr><td><input type='radio' name='status' value='delete'>Delete</td><td><input type='radio' name='status' value='update' checked>Update</td></tr>";
        echo "<tr><td>ID:<td><input type='text' name='id' value='".$row[id]."'></td></tr><tr><td>Name:<td><input type='text' name='uname' value='".$row[uname]."'></td></tr><tr><td>Store:<td><input type='text' value='".$row[store]."' disabled></td><td>";
        echo "<select name='store'>";
        $displaystore=new admin();
        $displaystore->storeoption();
        echo"</select>";
        echo "</td></tr><tr><td>Date:<td><input type='text' name='date' value='".$row[date]."'></td></tr>";
        echo "<tr><td></td><td><input type='submit' name='submit' value='submit'></td></form>";     
    }
    echo "</table>";
}
?>

你告诉表单发布给自己。添加操作属性或添加一些其他代码来处理数据,如下所示:

$id=$_GET['q'];

所有这一切都会显示发布数据,但您可以根据需要将其插入数据库。

另一件事,你抓住一个未经过滤的$ _GET变量,这里D3D11CreateDeviceAndSwapChain可能会导致一些安全问题。请查看转义输入或使用预准备语句。

答案 2 :(得分:1)

我认为@PierreDuc有正确的想法。

首先,我修复了index.php以正确终止PHP if语句和javascript以正确关闭大括号

<head>
<script>
function showit(str) {
      if (str.length==0) { 
        document.getElementById("txtHint").innerHTML="";
        return;
      }
      var xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","showuser.php?q="+str,true);
      xmlhttp.send();
 }       
//}                                               <-- fix1
</script>
<head>
<body>
<button onclick='showit(1)'>First User</button>
<button onclick='showit(2)'>Second User</button>
<button onclick='showit(3)'>Third User</button>
<?php
if(isset($_POST['submit'])){

}                                                  <-- fix2
?>
<div id="txtHint">
</div>
</body>

然后这个脚本运行没有错误

然后我将<form> .... </form>移到<table> ... </table>之外,一切正常。

<?php
if(isset($_GET['q'])){
    $id=$_GET['q'];
    $con = mysqli_connect("localhost","root","","application");
    $sql="SELECT * FROM users WHERE id=$id";
    $result=mysqli_query($con,$sql);

    echo "<form method='post'>";                       <-- fix3

    echo "<table>";
    while($row=mysqli_fetch_array($result)){

        echo "<tr><td><input type='radio' name='status' value='delete'>Delete</td><td><input type='radio' name='status' value='update' checked>Update</td></tr>";
        echo "<tr><td>ID:<td><input type='text' name='id' value='".$row[id]."'></td></tr><tr><td>Name:<td><input type='text' name='uname' value='".$row[uname]."'></td></tr><tr><td>Store:<td><input type='text' value='".$row[store]."' disabled></td><td>";
        echo "<select name='store'>";
        $displaystore=new admin();
        $displaystore->storeoption();
        echo"</select>";
        echo "</td></tr><tr><td>Date:<td><input type='text' name='date' value='".$row[date]."'></td></tr>";
        <-- next line...removed </form> added </tr>
        echo "<tr><td></td><td><input type='submit' name='submit' value='submit'></td></tr>";  
    }
    echo "</table></form>";                            <-- fix4
}
?>

然后新表单的提交按钮

答案 3 :(得分:0)

您可以尝试在客户端登录readystate并检查服务器端日志。

&#13;
&#13;
function showit(str) {
  if (str.length == 0) {
    document.getElementById("txtHint").innerHTML = "";
    return;
  }
  var xmlhttp = new XMLHttpRequest();
  var txtHint = document.getElementById("txtHint");
  var rState = document.getElementById("rState");
  xmlhttp.onreadystatechange = function() {
    rState.innerHTML += "readyState: " + xmlhttp.readyState + ", status: " + xmlhttp.status + ", responseText: " + xmlhttp.responseText + "\n";
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      txtHint.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.onerror = function() {
    var html = "<b>ERROR</b>\n"
    for (var k in xmlhttp) {
      if (xmlhttp[k] && typeof xmlhttp[k] !== "function") {
        html += k + ": " + xmlhttp[k] + "\n";
      }
    }
    rState.innerHTML += html;
  }
  xmlhttp.open("GET", "showuser.php?q=" + str, true);
  xmlhttp.send();
}
&#13;
<button onclick='showit(1)'>First User</button>
<button onclick='showit(2)'>Second User</button>
<button onclick='showit(3)'>Third User</button>
<div id="txtHint"></div>
<pre id="rState">
<h2>ready state log</h2>
</pre>
&#13;
&#13;
&#13;

相关问题