JQuery/AJAX 和 PHP 中的 POST 表单提交

我对在 JQuery/AJAX 和 PHP 中处理表单提交有点陌生,所以我一直在尝试在线学习一些教程并遇到了一些问题。


我正在尝试构建一个通过 PHP 处理提交的表单。这是我的 index.html 文件的内容。


<body>


    <h1>Food Preference</h1>


    <p>Please let us know what types of foods you would like to see on the menu.</p>


    <form id="food-form">

        <label for="appetizer">Appetizer</label>

        <input type="text" id="appetizer" required>


        <label for="entree">Entree</label>

        <input name="entree" type="entree" id="entree" required>


        <label for="dessert">Dessert</label>

        <textarea name="dessert" id="dessert" required></textarea>


        <button id="submit_button" type="submit">Send</button>


        <p id="form_content">

        </p>

    </form>

这是我的 index.js 文件


 jQuery.ajax({

        url: "handler.php",

        data: "appetizer=" + $("#appetizer").val() +

              "&entree=" + $("#entree").val() +

              "&dessert=" + $("#dessert").val(),

              type: "POST",

              success: function(data) {

                  $("#form_content").html(data);

              },

              error: function() {}

    });

这是handler.php


<?php


class runForm {

public function handle_food_form($request) {


    if(opinion($_POST["appetizer"], $_POST["entree"], $_POST["dessert"])) {

        print "<p class='success'>Thank you for your opinion.</p>";


        return array('post_id' => $new_post_id );

    }

 }

}


runForm();

?>

我的提交似乎没有保存在任何地方,或者如果有,我不知道如何找到它。任何人都可以为我可能做错的任何事情提供任何指示吗?


我想知道 handler.php 中的这一行是否正确,因为我还没有真正定义“意见”。


if(opinion($_POST["appetizer"], $_POST["entree"], $_POST["dessert"]))


波斯汪
浏览 183回答 2
2回答

智慧大石

用逗号分隔变量。在 jQuery.ajax 中,执行如下操作:&nbsp;jQuery.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: "handler.php",&nbsp; &nbsp; &nbsp; &nbsp; data: "appetizer=" + $("#appetizer").val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "entree=" + $("#entree").val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "dessert=" + $("#dessert").val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#form_content").html(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function() {}&nbsp; &nbsp; });

墨色风雨

您在此代码段中有很多问题,您应该首先检查 PHP 向您显示的错误并尝试先解决它们。PHP 文件 (handler.php)opinion()&nbsp;功能未定义。runForm()不是一个函数,它是一个类的名称,如果你想调用handle_food_form()函数,那么你可以把它变成一个静态函数并像这样调用它runForm::handle_food_form();你的 PHP 文件的最终版本应该是这样的<?phpclass RunForm {&nbsp; &nbsp; public static function opinion($appetizer, $entree, $dessert)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // do your logic here and return true or false&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public static function handle_food_form() {&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($_POST["appetizer"])) $_POST["appetizer"] = null;&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($_POST["appeentreetizer"])) $_POST["entree"] = null;&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($_POST["dessert"])) $_POST["dessert"] = null;&nbsp; &nbsp; &nbsp; &nbsp; if(SELF::opinion($_POST["appetizer"], $_POST["entree"], $_POST["dessert"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $htmlMsg =&nbsp; "<p class='success'>Thank you for your opinion.</p>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $con is a MySQLI object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $con->query("insert into table ........");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_post_id = $con->insert_id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return array('post_id' => $new_post_id, 'htmlMsg' => $htmlMsg );&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return array('post_id' => null , 'htmlMsg' => "");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}echo RunForm::handle_food_form()['htmlMsg'];客户端您应该使用encodeURIComponent()对 URL 的参数进行编码以防止这样的事情dessert=cheesecake&pancake破坏 URL,或者将参数的对象作为数据传递给ajax函数,jquery 将在内部为您进行编码jQuery.ajax({&nbsp; &nbsp; url: "handler.php",&nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appetizer: $("#appetizer").val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entree: $("#entree").val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dessert: $("#dessert").val()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#form_content").html(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; error: function() {}});
打开App,查看更多内容
随时随地看视频慕课网APP