用php打印ajax响应

时间:2015-11-29 12:51:59

标签: php ajax

在ajax中发送数据后,我无法获得警报吗?谁能告诉我我做错了什么?

我的表格:

<?php echo "bla"; ?>

这是我的submit2.php文件。

str := "This is a sample 123,456,789 text to 654,321 get the point across. 123"

Pos := 1
While (Pos := RegExMatch(str, "([\d,]+)", M, Pos + StrLen(M)))
{
    StringReplace, M1, M1, ",", ""

result .= M1 . " "
}

result = %result%
result .= "`n"

2 个答案:

答案 0 :(得分:1)

您的代码应该是这样的:

<强> HTML

<form action="" id="form1" method="post">
    First name:<br>
    <input type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input type="text" name="lastname" value="Mouse">
    <br><br>
    <input type="submit" id="submit1" value="Submit">
</form>

<强>的jQuery

 $("#form1").submit(function(e){
    e.preventDefault();
    var self = this;
    $.ajax({
        type: 'post',
        url: 'submit2.php',
        // dataType: 'json', unless you're expecting json object as server response, remove this
        data: $(self).serialize(),
        success: function(data) {
            alert(data);                    
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(errorThrown);
        }
    });
});

<强> submit2.php

<?php

    if(isset($_POST['firstname']) && isset($_POST['lastname'])){
        echo $_POST['firstname'] . " " . $_POST['lastname'];
    }

?>

答案 1 :(得分:1)

您的HTML或JQuery代码没有问题。问题出在submit2.php文件中。您的AJAX函数正在寻找JSON响应。从submit2.php返回JSON响应

<强> submit2.php

echo json_encode(array("message"=>"Here is the message"));