duanre1891 2015-09-28 19:33
浏览 34

jQuery和PHP代码没有连接到mySql DB

For the life of me I can't figure out why the following code is not working. I'm quite inexperienced so any assistance appreciated.

I want to get a simple connection working where an ajax call will return a username from a mySQL DB (this is a precursor to a much larger project). My HTML file is the following:

<!DOCTYPE html>

<html>
<head>
<title>TEST</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="jquery/test-theme.min.css"/>
<link href="jquery/jquery.mobile.icons.min.css" rel="stylesheet" />
<link href="jquery/jquery.mobile.structure-1.4.5.min.css" rel="stylesheet" />
<link href="jquery/app.css" rel="stylesheet" />
<script src="jquery/jquery-2.1.4.min.js"></script>
<script src="jquery/jquery.mobile-1.4.5.min.js"></script>
<script> function login(){

            $(document).ready(function(){
                var user = $("#username")[0].value;
                var email = $("#email")[0].value;
                $.ajax({
                    type: "GET",
                    url: "http://localhost:8888/test/connection.php",
                    data: "username="+user+"&email="+email,
                    success: function(result){
                        if(result){
                            $("#message")[0].value = "Success" +result;
                        }
                        else{
                            $("#message")[0].value = "Fail :(";
                        }
                    },
                    error: function(result){
                        $("#message")[0].value = "Ajax error!"+result;
                    }


                });


        });
        }

</script>
</head>

<body>
<div data-role="page">
    <div data-role="header" data-theme="c">
        <h1>Sign Up!</h1>
    </div><!-- /header -->
     <div role="main" class="ui-content">

            <h3>Sign Up</h3>
            <label for="txt-first-name">Username</label>
            <input type="text" name="txt-first-name" id="username" value="" onkeyup="login()">
            <label for="txt-email">Email Address</label>
            <input type="text" name="txt-email" id="email" value="" onkeyup="login()">
            <input type="text" id="message"></input>
            <button id="submit" onclick="login()"> Submit</button>

        <div data-role="popup" id="dlg-sign-up-sent" data-dismissible="false" style="max-width:400px;">
    </div>
</div><!-- /content -->
</div>
</body>
</html>

And my PHP:

<?php
$user = $_REQUEST['username'];
$email = $_REQUEST['email'];

mysql_connect("localhost:8889","root","root") or die(mysql_error());
mysql_select_db("TEST") or die(mysql_error());



$result = mysql_query("SELECT username, email FROM login WHERE username ='$user'");
while($row = mysql_fetch_array($result)){
if($user = $row["username"]){
    echo $row["id"];    
}
else{
    echo "failed getting user"
}

}
?>

I have a DB called TEST running via MAMP with some entries in the login table for username and email. I just want to get the connection running properly but I'm stumped.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计