简体   繁体   English

ajax不是将发布请求发送到php

[英]ajax isnt sending post request to php

Javascript doesn't send any post data to php file JavaScript不会将任何发布数据发送到php文件

$(document).ready(function(){
                function showComment(){
                  $.ajax({
                    type:"post",
                    url:"process.php",
                    data:"action=showcomment",
                    success:function(data){
                         $("#comment").html(data);
                    }
                  });
                }
                showComment();
$("#button").click(function(){
                      var name = $("#name").val();
                      var message = $("#message").val();
                      var dataString = "name="+name+"&message="+message+"&action=addcomment";
                      $.ajax({
                          type:"post",
                          url:"process.php",
                          data:dataString,
                          success:function(data){
                            showComment();

                          }

                      });

                });
});

form: 形成:

<form action="" method="POST" enctype="multipart/form-data">
           name : <input type="text" name="name" id="name"/>
           </br>
           message : <input type="text" name="message" id="message" />
           </br>
           <input type="submit" value="Post" name="submit" id="button">
           <div id="info" />
           <ul id="comment"></ul>
    </form>

php 的PHP

    $action=$_POST["action"];
if($action=="addcomment"){
    echo "Add comment WORKS!";
}
if($action=="showcomment"){
    echo "default";
}

Tried to add such lines as if post addcomment than show some words, just for a test since sql request didn't but php doesn't show any response at all, like there was no post action at all. 试图添加这样的行,就像在post addcomment之后而不是显示一些单词一样,仅用于测试,因为sql请求没有,但是php根本不显示任何响应,就像根本没有post操作一样。

ps. ps。 I'm really new ajax so if possible show me a solution to solve it. 我真的是新的Ajax,因此,如果可能,请向我显示解决方案。

You're using a submit button so it will be making the form submit and reload which will bypass your ajax, you can change your jQuery to listen for the form submit event instead like this: 您使用的是Submit按钮,因此它将使表单提交并重新加载,这将绕过您的ajax,您可以更改jQuery来监听表单提交事件,如下所示:

$("form").on('submit', function(e){

    // Stop form from submitting
    e.preventDefault();

    var name = $("#name").val();
    var message = $("#message").val();
    var dataString = "name="+name+"&message="+message+"&action=addcomment";
    $.ajax({
        type:"post",
        url:"process.php",
        data:dataString,
        success:function(data){
            showComment();
        }
    });
});

Or simply change the button from type="submit" to type="button" or replace it with a element. 或者只是将按钮从type =“ submit”更改为type =“ button”或将其替换为元素。

You are using submit button as Dontfeedthecode mentioned. 您正在使用“提交”按钮作为Dontfeedthecode提及。 Your form does not have any action so it is self posting. 您的表单没有任何操作,因此是自发布。 I have added action and id to the html form and a hidden field to pass the action. 我已经将动作和ID添加到html表单和一个隐藏字段中以传递动作。 Now javascript serialize the form and send it to the process.php. 现在,javascript将表格序列化并将其发送到process.php。

    $(function () {
        $("#my-form").on("submit", function (e) {
            $("#action").val("addcomment");
            $.ajax(
                {
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (data) {
                        showComment();
                    }
                });
            return false;
        });
    });


<form action="process.php" method="POST" id="my-form" enctype="multipart/form-data">
    <input type="hidden" id="action"  name="action" value="" />

    name : <input type="text" name="name" id="name" />
</br>
message : <input type="text" name="message" id="message" />
</br>
<input type="submit" value="Post" name="submit" id="button">
<div id="info" />
<ul id="comment"></ul>
</form>

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

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