【问题标题】:Sending form data to page nested in div将表单数据发送到嵌套在 div 中的页面
【发布时间】:2015-09-21 17:01:03
【问题描述】:

这是我第三次尝试找到这个问题的答案,每次我都因为某种原因而被否决。所以我再试一次。我正在尝试通过 ajax 从表单中的隐藏输入发送数据。隐藏的输入从 php 脚本中获取其值。现在我似乎无法在接收页面上提取隐藏的输入值。现在,我发送的表单在 php 中生成和传播,触发将表单发送到其他页面的 ajax 也是如此。

当我尝试从接收页面上的表单调用信息时,它似乎没有从第一页接收数​​据。我认为这是我没有错误的原因,它不会显示数据,但它会触发位于 fetch 数组之前的 echo。

这是第一页的代码。除了表单发送部分之外,它在所有方面都适用于我正在尝试做的事情。我遗漏了很多内容,但 ajax 和表单部分都在里面。

<?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
      $('#" . $row['id'] ."').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=\"" . $row['id'] ."\"  class=\"front\">
    <form id=\"" . $row['recipe'] ."\" name=\"f2\">
    <input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
        <h2>
        " . $row['recipe'] ."<br></h2>
     <img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">

    </form> 
    </div>


    <div class=\"back\">
        <div class=\"pad\">
            <h2>" . $row['recipe'] ."</h2>

            <p>" . $row['id'] ."</p>
            <p>" . $row['id'] ."</p>
                        <p>Number of Servings " . $row['servings'] ."</p>
            <p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
            <p>Numer of Calories: " . $row['calories'] ."</p>
        </div>
    </div>
</div>

</div>






            </div>

            </div>";


}

  mysqli_close($con);


?>   

这里是接收页面。它加载但只显示回声。如果我在 SELECT 语句中删除 WHERE,它会显示所有数据库结果(不是所需的)。

<?php


$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$r = $_POST["f2"]['recipe'];
$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);


?> 

任何帮助将不胜感激。

【问题讨论】:

标签: javascript php ajax forms


【解决方案1】:

尝试使用 id 而不是表单名称发布序列化数据。请参阅下面的示例代码。

data: $(\"#f2\").serialize(),

希望这会对您有所帮助。

请参阅下面更新的工作代码。

更新答案:

page1.php

</script>
<?php

    $rows[0]['id'] = 1;
    $rows[0]['recipe'] = "Veg Rec";
    $rows[0]['cooking'] = "Hot cooking";
    $rows[0]['calories'] = 1000;
    $rows[0]['image'] = "image.png";
    foreach ($rows as $key => $row) {
    # code...
    echo"




        <div id=\"covera\">

        <div id=\"holder\" class=\"holder\">


<div id=\"disp\" class=\"disp\">

<div class=\"click diagonal panel\">
    <div id=\"" . $row['id'] ."\"  class=\"front\">
    <form id2=\"" . $row['recipe'] ."\" id=\"f2\">
    <input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
        <h2>
        " . $row['recipe'] ."<br></h2>
     <img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">

    </form> 
    </div>


    <div class=\"back\">
        <div class=\"pad\">
            <h2>" . $row['recipe'] ."</h2>

            <p>" . $row['id'] ."</p>
            <p>" . $row['id'] ."</p>
                        <p>Number of Servings " . $row['servings'] ."</p>
            <p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
            <p>Numer of Calories: " . $row['calories'] ."</p>
        </div>
    </div>
</div>

</div>






            </div>

            </div>



<script>
   $(document).ready(function() {//start document ready
      $('#" . $row['id'] ."').click(function (e){
        e.preventDefault();

        $.ajax({
        type: 'POST',
        url: 'page2.php',
        data: $(\"#f2\").serialize(),
success: function(d){
   $(\"#content-disp\").html(d);
}
    });
  });
 });//end document ready
</script>


            ";


}

?>  

page2.php

<?php

print_r($_POST);


?>

【讨论】:

  • 我只是尝试使用 id 而不是更改 ajax 然后在接收页面上更改它。它呈现出与之前相同的结果
  • 对不起,我尝试使用格式化,但没有完全搞定在搞砸了一些之后,我相当有信心问题是接收页面上的 $_POST[#f2] 没有接收到数据从隐藏的输入表单。所以问题出在某个地方,因为您正在猜测 $.ajax({ type: 'POST', url: 'localhost/pages/receivingpage.php', data: $(\"#f2\").serialize(), success: function(d ){ $(\"#content-disp\").html(d); } 或者这里
    '
  • 您还必须更改 ajax 代码。如果您不更改 ajax 代码,则在表单 ID 中所做的更改将无济于事。如果您担心您将不得不在接收页面中进行大量更改。如果希望事情按预期运行,您必须在接收页面中进行更改。
  • 确实,你是对的。在我正在使用的代码中,我已经这样做了
  • 让我重新表述一下我看到的情况,这样也许你们都可以更好地理解我看到的问题。为给定数据库中的每一行生成 ajax 和表单。隐藏的输入/表单位于 div 中。 ajax 特定于显示的每个结果。当我点击生成的 div 时,ajax 应该将数据发送到另一个 div 中加载的接收页面。在第一页和接收页之间的翻译中丢失了一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2014-01-18
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多