Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ajax将post数据提交到php文件_Php_Jquery - Fatal编程技术网

使用ajax将post数据提交到php文件

使用ajax将post数据提交到php文件,php,jquery,Php,Jquery,我只需要使用ajax调用向php文件提交两个值。php文件将使用$\u POST[]函数处理数据。问题是我的ajax代码没有正确地发送数据,所以我在console.log(result)函数中并没有得到任何结果。我怎样才能解决这个问题?有什么想法吗 Jquery: <script type="text/javascript"> $("#signup_submit").on( "click", function() {

我只需要使用ajax调用向php文件提交两个值。php文件将使用$\u POST[]函数处理数据。问题是我的ajax代码没有正确地发送数据,所以我在console.log(result)函数中并没有得到任何结果。我怎样才能解决这个问题?有什么想法吗

Jquery:

<script type="text/javascript">
                $("#signup_submit").on( "click", function() {
                  //alert("foo");
                  var _username = "foo";
                  var _email = "test@gmail.com";


                  $.post("check.php", {username: _username, email: _email}, function(result){
                    console.log(result);
                  });



                });
            </script>

我已经向您评论了可能的答案,但为了简化,我将把它写在这里。 您的javascript代码看起来不错,但请将PHP脚本更改为以下内容,然后尝试:

$username = "";
$email = "";
$response = ""
if ($_POST["username"] && $_POST["email"]) {
    $username = $_POST["username"];
    $email = $_POST["email"];
}

if ($username != "" && $email != "") {
    if (!username_exists($username) && !signup_email($signup_email)) {
        $response = json_encode("good");
    }else{
        $response = json_encode("bad");
    }
}
echo $response;
echo$response
因为如果不添加该行,您将无法从ajax请求中获得任何结果,因此console.log不会显示任何内容


希望这对你有帮助

我认为这段代码非常适合你

 $("#signup_submit").on( "click", function() {
  var _username = "foo";
  var _email = "test@gmail.com";

  $.ajax({
    type: 'POST',
    url: 'check.php',
    dataType: 'text',
    data: {
     username: _username,
     email: _email
   },
   success: function(response) {
    console.log(response);
   },
   error: function(err) {
      alert(err);
   }
  });
 });
像用户#molinet写在php文件中一样,使用$response,然后回显它

$username = "";
$email = "";
if ($_POST["username"] && $_POST["email"]) {
 $username = $_POST["username"];
 $email = $_POST["email"];
}

if ($username != "" && $email != "") {
 if (!username_exists($username) && !signup_email($signup_email)) {
    $response = "good";
 }else{
   $response = "bad";
 }
}
echo $response;

您可以看到,我已经尝试使用$.post()提交数据。请确保在post调用中具有正确的路径。当我说正确路径时,您的javascript文件是否与php文件位于同一目录中?然后,只有您可以像$.post(“check.php”)那样编写,否则您需要提供一个相对路径。是的,check.php位于同一个目录中。您的php脚本缺少结果。添加类似于
$response='good'
的内容,而不是
json\u encode(“good”)
并回显
**$response**
变量,因为无论哪种方式,您都不会在控制台上得到任何响应。log()您必须
回显
json\u encode()
。如果没有,它也不会产生任何结果。@难以置信你是对的,我在写答案时错过了它。现在更新!这就是答案
$username = "";
$email = "";
if ($_POST["username"] && $_POST["email"]) {
 $username = $_POST["username"];
 $email = $_POST["email"];
}

if ($username != "" && $email != "") {
 if (!username_exists($username) && !signup_email($signup_email)) {
    $response = "good";
 }else{
   $response = "bad";
 }
}
echo $response;