简体   繁体   English

在没有页面刷新的情况下在表单上提交复选框值 AJAX PHP

[英]Submit checkbox value on form without page refresh AJAX PHP

I just started learning ajax and its really great and time saving i agree.我刚开始学习 ajax,它真的很棒而且节省时间,我同意。

But i got stuck at this point sending form data without page reload.但是我在这一点上卡住了发送表单数据而无需重新加载页面。

Below is my html code.下面是我的 html 代码。

<form id="form4" method="post">
  <input type="checkbox" name="test" id="agreed" value="check">
  <br>
  <br>
  <input type="submit" id="form-submit" name="submit" value="Send">
  <p class="form-message"></p>
</form>

Below is my Ajax script下面是我的 Ajax 脚本

<script>
    $(document).ready(function() {
        $("#form4").submit(function(event) {
            event.preventDefault();
            var action = 'another_test';
            var agreed = $("#agreed").val();
            var submit = $("#form-submit").val();
            $(".form-message").load("test3.php", {
                test: agreed,
                submit: submit,
                action: action
            });
        });
    });
</script>

Below is my php code下面是我的php代码

<?php

  if (isset($_POST['action'])) {

        if ($_POST['action'] == 'another_test') {

            $test = $_POST["test"];
            $errorEmpty = false;


            if (!empty($test)) {
                echo "<p>Click the checkbox pls</p>";
                $errorEmpty = true;
            }

            else {
                echo "<p>Checkbox clicked</p>";
            }
        } else {
            echo "Error.. cant submit";
        }
    }

?>
<script>

    var errorEmpty = "<?php echo $errorEmpty ?>";
</script>

The php file is on another page called test3.php php 文件位于另一个名为 test3.php 的页面上

This particular code works if it was an input text but doesn't work for a checkbox.如果它是输入文本但不适用于复选框,则此特定代码有效。 Please help me so i can learn well.请帮助我,这样我才能好好学习。 Thanks in advance.提前致谢。

.load() (as per the documentation) performs a GET request, not a POST, but your PHP is (as shown by the $_POST references) expecting a POST request - and it usually makes sense to submit form data using POST. .load() (根据文档)执行 GET 请求,而不是 POST,但您的 PHP(如$_POST引用所示)期待 POST 请求 - 通常使用 POST 提交表单数据是有意义的。

So you'd be better to use $.post() - this will send a POST request.所以你最好使用$.post() - 这将发送一个 POST 请求。 Then you can handle the response and load it into your "form-message" element in the "done" callback triggered by that request.然后,您可以处理响应并将其加载到由该请求触发的“完成”回调中的“表单消息”元素中。

NB You could also make the code shorter by putting the "action" variable as a hidden field in the form, and then simply serialize the form in one command instead of pulling each value out separately.注意您还可以通过将“action”变量作为表单中的隐藏字段来缩短代码,然后简单地在一个命令中序列化表单,而不是分别拉出每个值。

Example:例子:

HTML: HTML:

<form id="form4" method="post">
  <input type="checkbox" name="test" id="agreed" value="check">
  <br>
  <br>
  <input type="submit" id="form-submit" name="submit" value="Send">
  <input type="hidden" action="another_test"/>
  <p class="form-message"></p>
</form>

JavaScript JavaScript

$(document).ready(function() {
    $("#form4").submit(function(event) {
        event.preventDefault();

        $.post(
          "test3.php", 
          $(this).serialize()
        ).done(function(data) { 
            $(".form-message").html(data);
        });
    });
});

Documentation:文档:

jQuery Load jQuery 加载

jQuery Post jQuery 帖子

jQuery Serialize jQuery 序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM