php - 加载表单在同一个 div 中提交结果

标签 php javascript jquery

我有一个表单,可以在带有提交按钮的表单中加载表格 我希望该表单显示在同一个 div 中

下面是 getdivision.php 的代码

    <div id="record"></div>
  <form id='form4' action='viewrecord.php' method='POST'>
<table id='table-2'>
    <thead>
        <th>RATE</th>
        <th>LAST NAME</th>
        <th>FIRST NAME</th>
        <th>DIVISION</th>
        <th>WORKCENTER</th>
        <th>MENTOR</th>
        <th>View</th>
    </thead>
    <tbody>

 <?php 
  while($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>" . $row[0] . "</td>";
      echo "<td>" . $row[1] . "</td>";
      echo "<td>" . $row[2] . "</td>";
      echo "<td>" . $row[3] . "</td>";
      echo "<td>" . $row[4] . "</td>";
      echo "<td>" . $row[5] . " " . $row[6] . " " . $row[7] . "</td>"
    echo "<td><input type='submit'  name='viewrecord' value='".$row[8]."' class='submit-                           button view-record' /></td>";
      echo "</tr>";
      }
    echo "
    <tbody>
    </table>
    </form>";

这是ajax代码

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">        </script>
    <script>
    $("#form4").submit(function(event){
        event.preventDefault();
        $.post( viewrecord.php, $("#form4").serialize(), function(data){
            $("results").html(data);
        });
    });
    </script>

我好像找不到问题

form4 是从 php 脚本生成的,并放置在主页的 #result div 中,这可能是某些人的原因,请帮助

这里是包含结果div的index.php

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <script type="text/javascript">

    function showdivision(str)
    {
    if (str=="")
      {
      document.getElementById("results").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("results").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getdivision.php?q="+str,true);
    xmlhttp.send();
    }

    function viewrecord()
    {

    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("results").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","viewrecord.php");
    xmlhttp.send();
    }

    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    </head>
    <body>
    <?php
    $con = mysql_connect("localhost","mykoll_frc","frcfrc");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }


    ?>
    <form>

    <select name="DIVISIONS" onchange="showdivision(this.value)">
    <option value="">Select a Division:</option>
    <option value="600">Avionics</option>
    <option value="700">Armament</option>
    <option value="GMD">GMD</option>
    <option value="ADMIN">ADMIN</option>
    </select>
    <?php
    while($row = mysql_fetch_array($mentors))
    {
      echo "<option value=\"".$row['mentorid']."\">".$row['M_RATE']."         ".$row['M_LastName']."                 ".$row['M_FirstName']."\n  ";
    }
    ?>

    </select>

    <button type="button" onclick="showall()">View all records</button>
    <button type="button" onclick="showallmentors()">View Mentor List</button>

    </form>
    <br />
    <div id="results"><b>info will be listed here.</b></div>

    </body>
    </html>

它应该工作的方式是 index.php 加载 用户选择一个部门 结果 div 动态地从 getdivision.php 填充 然后用户可以选择一条记录,该记录应填充结果 div 我希望我说清楚了

最佳答案

jQuery 需要选择器上的 #

$.post( viewrecord.php, $("#form4").serialize(), function(data){
    $("#results").html(data); // changed results to #results
});

关于php - 加载表单在同一个 div 中提交结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550729/

相关文章:

php - Grocery CRUD ajax 性能

php - 如何在php页面中添加或嵌入CKEditor

javascript - 为什么这两个 javascript 文件不同时运行?他们为什么会发生冲突/覆盖?

javascript - 如何访问postMessage中发送的数据?

javascript - 如何获取字符串中美元符号之间的值?

javascript - AngularJS - 选择的 ng-model 无法正常工作

php - Facebook 共享内容中的 Yii 登录问题

javascript - 在滚动条中动画滚动

javascript - 如何在对象创建时运行函数?

php - php 中当月的第一天使用 date_modify 作为 DateTime 对象