Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Javascript 通过Ajax获取的变量为空(Phonegap Ajax Json PHP MySQL)_Javascript_Php_Ajax_Json_Cordova - Fatal编程技术网

Javascript 通过Ajax获取的变量为空(Phonegap Ajax Json PHP MySQL)

Javascript 通过Ajax获取的变量为空(Phonegap Ajax Json PHP MySQL),javascript,php,ajax,json,cordova,Javascript,Php,Ajax,Json,Cordova,我使用Phonegap创建了一个android应用程序。我在中创建了一个帐户,并在服务器上添加了我的PHP文件。在phpMyAdmin中,我创建了我的数据库 现在,我尝试将我的项目与在线数据库连接,并在其中插入或检查一些数据。 问题: 当我在手机上运行该应用程序时,我从success中收到以下警报:。。。ajax中的部分代码: 没有这样的用户名。 我的PHP在所有echo中都有注释,除了:echo json_encode 当我添加这行var\u dump$\u POST;在PHP中从ajax获取

我使用Phonegap创建了一个android应用程序。我在中创建了一个帐户,并在服务器上添加了我的PHP文件。在phpMyAdmin中,我创建了我的数据库

现在,我尝试将我的项目与在线数据库连接,并在其中插入或检查一些数据。 问题:

当我在手机上运行该应用程序时,我从success中收到以下警报:。。。ajax中的部分代码: 没有这样的用户名。 我的PHP在所有echo中都有注释,除了:echo json_encode

当我添加这行var\u dump$\u POST;在PHP中从ajax获取$username并运行我的应用程序后,我立即看到以下警报:array1{[\username\]=>string2\hi\}

当我添加以下行时:如果$username{echo'.'}为空,在运行我的应用程序后,我在错误中的警报中看到:。。。作为ajax的一部分,它将打印此if中的回音。因此,$username肯定是空的

这是我的JavaScript文件:我正确地从html中获取所有值,因此请关注代码的两个Ajax部分

document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    function onDeviceReady() {
        var el = document.getElementById("register"); 
        el.addEventListener("click", Register, false); 
    }

function Register() {
  var username = document.getElementsByName('username')[0];
  var password = document.getElementsByName('password')[0];
  var email = document.getElementsByName('email')[0];

  var strong_flag_user = 0;
  var user = username.value; 

  if (username.value == "") {
    $("#username").focus();
    document.getElementById('username').style.boxShadow = "0 0 7px #f00";
    navigator.notification.vibrate(500);
  } 
  else{
    $.ajax({
            url: "http://www.guidemeforall.freeiz.com/phps/check_for_dublicates/check_username.php",
            type: "POST",
            crossDomain: true,
            data: { username: user },
            dataType:'json',
            success: function(response){ 
                if (response.status == 'success') { 
                    alert(response.message);
                    document.getElementById('username').style.boxShadow = "none";
                    strong_flag_user = 1;
                } 
                else if (response.status == 'error') { 
                    alert(response.message);
                    navigator.notification.alert("This username is already taken! Please use another one!", null, 'Username', 'Okay');
                    document.getElementById('username').style.boxShadow = "0 0 7px #f00";
                    navigator.notification.vibrate(500);
                    strong_flag_user = 0;
                    //window.location("main.html");
                }
                else { 
                  alert("error");                     
                  strong_flag_user = 0;
                }
            },
            error: function(error){ //function(error){
                alert(JSON.stringify(error));
                strong_flag_user = 0;
                //window.location = "main.html";
            }
    });
  }

   //>5 characters, 1 upper case, at least 1 lower case, at least 1 numerical character, at least 1 special character
   var passExp = /(?=^.{6,15}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*/;
   var strong_flag_pass = 0;

  if (!(password.value.match(passExp))) {
    $("#password").focus();
    document.getElementById('password').style.boxShadow = "0 0 7px #f00";
    navigator.notification.alert("Please enter a strong Password! It has to have at least: 6 characters, 1 upper case, 1 lower case, 1 numerical character and 1 special character!", null, 'Password', 'Okay');
    navigator.notification.vibrate(500);
    strong_flag_pass = 0;
  }
  else{
    document.getElementById('password').style.boxShadow = "none";
    strong_flag_pass = 1;
  }

  var emailExp = /^.+@[^\.].*\.[a-z]{2,}$/;
  var strong_flag_email = 0;

  if (!(email.value.match(emailExp))) {
    $("#email").focus();
    document.getElementById('email').style.boxShadow = "0 0 7px #f00";
    navigator.notification.alert("Please enter a correct Email!", null, 'Email', 'Okay');
    navigator.notification.vibrate(500);
    strong_flag_email = 0;
  }
  else {
    document.getElementById('email').style.boxShadow = "none";
    strong_flag_email = 1;
  }

  var gender;
  if (document.getElementById("gender").value == "female")
    gender = 'F';
  else
    gender = 'M';

  var about_you = document.getElementById("about_you").value;

  var age = document.getElementById("radio-choice").value;

  if (document.getElementById('radio-choice-1').checked) {
    age = document.getElementById('radio-choice-1').value;
  }
  else if (document.getElementById('radio-choice-2').checked) {
    age = document.getElementById('radio-choice-2').value;
  }
  else if (document.getElementById('radio-choice-3').checked) {
    age = document.getElementById('radio-choice-3').value;
  }
  else if (document.getElementById('radio-choice-4').checked) {
    age = document.getElementById('radio-choice-4').value;
  }
  else if (document.getElementById('radio-choice-5').checked) {
    age = document.getElementById('radio-choice-5').value;
  }

  else if (document.getElementById('radio-choice-6').checked) {
    age = document.getElementById('radio-choice-6').value;
  }

  if (strong_flag_user == 1 && strong_flag_pass == 1 && strong_flag_email == 1){      
    //add to db
    register_db(email.value, password.value, username.value, gender, about_you, age);
  }
}

function register_db(em, pass, user, gend, about, ag) {
    $.ajax({
            url: "http://www.guidemeforall.freeiz.com/phps/sign-up.php",
            type: "POST",
            crossDomain: true,
            data: { username:user, password:pass, email:em, gender:gend, about_you:about, age:ag },
            dataType:'json',
            success: function(data)
            {
                if (data.status == 'success')
                {
                      alert("Success!");
                }
                else if (data.status == 'error')
                {
                      alert("Failure!");
                }
            }
   });
}
这是我的PHP文件,我在其中检查用户名是否已存在用户名=主键:

 <?php 

      header('Content-type: application/json'); 
      header('Access-Control-Allow-Origin: *');

      //require_once('../database_config.php');

      $server = "my***.000webhost.com";
      $database = "a1****37_guideme";
      $username = "a1****37_guideme";
      $password = "*****";

      $con = mysql_connect($server, $username, $password);

      // if($con) { //echo "Connected to database!"; }
      // else { //echo "Could not connect!"; }

      mysql_select_db($database, $con);

      $topost = file_get_contents('php://input');

      $thedata = json_decode($topost, true);

      $username = $thedata['username'];
      //var_dump($_POST);

      //if (empty($username)) {
      //     echo 'The username is either 0, empty, or not set at all';
      //}

      $sql = "SELECT COUNT(*) as Count FROM `user` WHERE `username`='$username'";

      $result= mysql_query($sql, $con);

      $rows = mysql_fetch_array($result); 

      $count = $rows['Count'];

      if (!$result) {
        die('Error: ' . mysql_error());
        //$response_array['status'] = 'error';  
        //echo json_encode($response_array);
      } 
      else {
        if ($count == 0) {
          echo json_encode(array('status' => 'success','message'=> 'There is no such username'));
          //$response_array['status'] = 'success';  
          //echo json_encode($response_array);
        }
        else
        {
          echo json_encode(array('status' => 'error','message'=> 'The username already exists'));
          //$response_array['status'] = 'error';  
          //echo json_encode($response_array); 
        }
      }

      mysql_close($con);
    ?>

通过将PHP中获取数据的方式更改为->$user=$\u POST['username'],解决了我的问题;请尝试console.logerror并在控制台窗口中查看对象,而不是使用Json_decode e.t.c..

而不是alert@itachi,关于console.logError我在哪里可以看到它的输出@jcesarmobile如果你使用chrome,我会试试它,点击F12并查看控制台选项卡。关于这一点,我可能听起来有点愚蠢,但在服务器中我只添加了我的php文件,因此我无法从chrome运行我的应用程序。。最好将我的整个项目添加到web主机服务器中,以便我可以通过chrome进行检查?
<?php 

  header('Content-type: application/json');
  header('Access-Control-Allow-Origin: *');

  //require_once('database_config.php');

  $server = "mys****.000webhost.com";
  $database = "a***37_guideme";
  $username = "a***37_guideme";
  $password = "******";

  $con = mysql_connect($server, $username, $password);

  // if($con) { //echo "Connected to database!"; }
  // else { //echo "Could not connect!"; }

  mysql_select_db($database, $con);

  $topost = file_get_contents('php://input');

  $thedata = json_decode($topost, true);

  $username = $thedata['username'];
  $password = $thedata['password'];
  $email = $thedata['email'];
  $gender = $thedata['gender'];
  $age = $thedata['age'];
  $about_you = $thedata['about_you'];

  $sql = "INSERT INTO user (username, password, email, gender, age, about_you) ";
  $sql .= "VALUES ('$username', '$password', '$email', '$gender', '$age', '$about_you')";

  if (!mysql_query($sql, $con)) {
      die('Error: ' . mysql_error());
      // $response_array['status'] = 'error';  
      // echo json_encode($response_array);
  } 
  else {
    echo json_encode(array('status' => 'success','message'=> 'No problem'));
    // $response_array['status'] = 'success';  
    // echo json_encode($response_array);
  }

  mysql_close($con);
?>