在通过动态生成的内容发送和解释通过ajax发送的表单数据时遇到问题


Trouble sending and Interpreting form data sent via ajax through dynamically generated content

很抱歉这个标题太复杂了,我不知道该怎么描述它。所以,这就是我所拥有的。

我动态地为每个数据库行内容创建div,作为gui的框架。这部分有效。它生成一个新的div,并为数据库的每一行正确显示它,并传播所请求的数据。当您单击生成的gui时,它将数据发送到另一个页面进行处理,该页面显示在另一个div中。这一切都有效。我遇到的麻烦是在从gui接收发送表单数据的页面上。它要么显示db的全部内容从请求,或者如果我添加一个地方的选择语句,获取请求似乎没有火在所有。

<?php
  $con=mysqli_connect("localhost","base","password","util");
  // Check connection
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sql = "SELECT * FROM recipes";
  $result = mysqli_query($con,$sql);
  while($row = mysqli_fetch_array($result)) {
    echo"
        <script>
          $(document).ready(function() {//start document ready
            $('#front').click(function (e){
              e.preventDefault();
              $.ajax({
                type: 'POST',
                url: 'http://localhost/pages/receivingpage.php',
                data: $('"f2'").serialize(),
                success: function(d){
                  $('"#content-disp'").html(d);
                }
              });
            });
          });//end document ready
        </script>
        <div id='"covera'">
        <div id='"holder'" class='"holder'">
        <div id='"disp'" class='"disp'">
        <div class='"click diagonal panel'">
          <div id='"front'"  class='"front'">
            <form id='"" . $row['recipe'] ."'" name='"f2'">
              <input type='"hidden'" name='"recipe'" value='"" . $row['recipe'] ."'">
              <h2>" . $row['recipe'] ."<br></h2>
            </form> 
          </div>
          <div class='"back'">
            <div class='"pad'">
              <h2>" . $row['recipe'] ."</h2>
              <p>" . $row['id'] ."</p>
              <p>Number of Servings " . $row['servings'] ."</p>
            </div>
          </div>
        </div></div></div></div>";
  }
  mysqli_close($con);
?>  

这是接收页面的代码。gui代码位于主页面,接收页面位于主页面的一个div中,但不位于其他div中。

如果我在select语句中添加where,那么fetch请求似乎根本不会触发。将显示回显2,但不会显示请求的结果。

  $r = $_POST["f2"];
  $sql = "SELECT * FROM recipes WHERE recipe ='".$r."'";
  $result = mysqli_query($con,$sql);
  echo "  2 ";
  while ($row = mysqli_fetch_array($result)) {
    echo "  " . $row['recipe'] ." ";
  }
  mysqli_close($con);
?>  

在while循环中关闭mysqli_connect(),尝试在第二个php脚本的while循环后移动mysqli_close($con);