通过jQuery发布和获取php

时间:2014-10-21 22:46:04

标签: php jquery post get

更新,我将jQuery更改为此,但仍然无效:

    $("#click").click(function(){
    $.post("accountcreation.php",function(response){ userCreation:"userCreation" 
        $("#justtesting").html(response);
    })
})

什么都没发生。我有HTML和PHP工作,但后来我想添加实时更新并开始这个jQuery代码。它有什么问题?

感谢。

的jQuery

$("#click").click(function(){
    $.post("accountcreation.php", { userCreation:"userCreation" 
    })
    $.get("accountcreation.php", function(data,status){
        $("#justtesting").html(data);
    })
})

HTML

Username: <input required type="text" name="userCreation" 
onchange="userValidate();" 
onkeypress="this.onchange();" 
onpaste="this.onchange();" 
oninput="this.onchange();">
<span id="userRegexJavascriptResponse"></span>
<br>

<input type="button" value="Submit" id="click">


<div id="justtesting"></div>

PHP(部分)

class accountcreation {
    private $options;

    function __construct($con, $ipCreation) {
        $this->passwordCreation = $_POST['passwordCreation'];
        $this->userCreation = $_POST['userCreation'];
        $this->ipCreation = $ipCreation;
        $this->emailCreation = $_POST['emailCreation'];
        $this->con = $con;
    }

1 个答案:

答案 0 :(得分:1)

请改用此代码。您的PHP脚本通过帖子期待数据,您可以在同一个.post()调用中获得响应。

$("#click").click(function(){
    $.post("accountcreation.php", { userCreation:"userCreation"}, function(data){
        $("#justtesting").html(data);
    });
});

注意:我强烈建议不要使用内联javascript;它很难维护,只是不好的做法。事件监听器应该如上所述......这就是要走的路。

您的HTML应该是:

Username: <input required type="text" name="userCreation">
<span id="userRegexJavascriptResponse"></span><br>
<input type="button" value="Submit" id="click">
<div id="justtesting"></div>

而且,您需要在 JavaScript 代码之前加入 jQuery核心

<script src="//code.jquery.com/jquery-1.11.1.js"></script>

如果您打开开发工具并运行下面的代码段,您应该会看到使用您提供的数据进行的后调用。这意味着它应该能够在您的服务器上运行,否则您应该能够在开发工具的控制台中看到任何错误。

   $("#click").click(function(){
        $.post("accountcreation.php", { userCreation:"userCreation"}, function(data){
            $("#justtesting").html(data);
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Username: <input required type="text" name="userCreation">
    <span id="userRegexJavascriptResponse"></span><br>
    <input type="button" value="Submit" id="click">
    <div id="justtesting"></div>