drz73366 2016-10-29 12:09
浏览 83

在HTML中的ajax,从php获得结果

I have html:

<body>
<div class="po-logo">
  <img src="img/logos/lw.png" alt="logo watchbees"/>
</div>
<form>
<div class="mui-textfield mui-textfield--float-label reg-window">
  <input id="username" name="username" type="text"/>
  <label>Username</label>
</div>
<div class="mui-textfield mui-textfield--float-label reg-window">
  <input id="password" name="password" type="password"/>
  <label>Password</label>
</div>
<div class="mui-textfield mui-textfield--float-label reg-window">
  <button id="login" value="submit" onclick="return result" class="mui-btn mui-btn--raised reg-win-button">Sign in</button>
</div>
</form>

Jquery in html:

$("#login").click(function(){
      var username=$("#username").val();
      var password=$("#password").val();
      if(($.trim(username).length > 0) & ($.trim(password).length > 0))
      {
        $.ajax({
        type: "POST",
        url: "http://127.0.0.1/app/login.php",
        data: {username: username, password: password},
        beforeSend: function(){ $("#login").html('Prihlasovanie...');},
        success: function(data){
        if(data == "success")
        {
          localStorage.login="true";
          localStorage.username=username;
          window.location.href = "profil.html";
          }
        else if (data = "failed")
        {
          alert("Problem s prihlasením, skúste znovu.");
          $("#login").html('Prihlásiť sa');
          }
          }
        });
      }return false;
    });

And php file on localserver, login.php:

if (isset($_POST['username'], $_POST['password'])){
$username=mysql_real_escape_string(htmlspecialchars(trim($_POST['username'])));
$password=mysql_real_escape_string(htmlspecialchars(trim($_POST['password'])));
$login = mysql_num_rows(mysql_query("SELECT * FROM `login` WHERE `username`='$username' AND `password`='$password'"));
if($login != 0){
echo "success";
}
else{
echo "failed";
}
}

But something is wrong, ajax doesn't send my back any data. It only changes #login to "connecting..." but nothing happens. Inscribing information is correct and they are uploaded to the database. Most likely php doesn't send back any data or ajax in html doesn't correct processing data from php. I don't where is problem. Profil.html is in the same folder as index.html. But login.php is on local server.

HTML: index.html; PHP: login.php on local server on my latop; Server: 127.0.0.1, database name db_login.

Any help or pointers greatly appreciated.

  • 写回答

3条回答 默认 最新

  • dtjo87679 2016-10-29 14:36
    关注

    First - you have to check is ajax request pointed correctly to your php file. Open inspector in Chrome, switch to tan network then Emanuel filter xhr. After click on #login you should see new request there. Click on it, switch to response tab and paste its content here Please

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题