Why can't jQuery's $.get() receive the return information from php?

WBOY
Release: 2016-07-06 13:54:12
Original
1276 people have browsed it

HTML part

<code>    <p>姓名<input type="text" name="name"></p>
    <p>性别<input type="radio" name="sex" value="m">男<input type="radio" name="sex" value="f">女</p>
    <p>手机号码<input type="text" name="mobile"></p>
    <button id="sub">提交</button>
    <script type="text/javascript">
        $("#sub").click(function() {
            var name = $("input[name='name']").val();
            var sex = $("input[name='sex']:checked").attr('value');
            var mobile = $("input[name='mobile']").val();
            if (name&&sex&&mobile) {
                alert(name+sex+mobile);
                $.get('user_info_save.php',{name: name,sex: sex,mobile: mobile},function(data){console.log(data);});
            }
            else{alert("信息未填写完整");}
        });
    </script></code>
Copy after login
Copy after login

Server’s php

<code>$db=mysqli_connect($host,$user,$password,$database);
if($db){
        $db->query("set names utf8");//设置UTF-8编码(JSON的唯一编码)
}else{
        echo 'DATABASE_CONNECTION_DIE';//数据库连接失败
        exit;
}
$name = $_REQUEST['name'];
$sex = $_REQUEST['sex'];
$mobile = $_REQUEST['mobile'];
$sql = "insert `customer`(`name`,`sex`,`mobile`) values('".$name."','".$sex."','".$mobile."')";
$result=$db->query($sql);
echo "ok";
</code>
Copy after login
Copy after login

The result is that the data has been entered into the database, but the function seems to be invalid. Why?

This question has been closed, reason: Unable to obtain exact results

Reply content:

HTML part

<code>    <p>姓名<input type="text" name="name"></p>
    <p>性别<input type="radio" name="sex" value="m">男<input type="radio" name="sex" value="f">女</p>
    <p>手机号码<input type="text" name="mobile"></p>
    <button id="sub">提交</button>
    <script type="text/javascript">
        $("#sub").click(function() {
            var name = $("input[name='name']").val();
            var sex = $("input[name='sex']:checked").attr('value');
            var mobile = $("input[name='mobile']").val();
            if (name&&sex&&mobile) {
                alert(name+sex+mobile);
                $.get('user_info_save.php',{name: name,sex: sex,mobile: mobile},function(data){console.log(data);});
            }
            else{alert("信息未填写完整");}
        });
    </script></code>
Copy after login
Copy after login

Server’s php

<code>$db=mysqli_connect($host,$user,$password,$database);
if($db){
        $db->query("set names utf8");//设置UTF-8编码(JSON的唯一编码)
}else{
        echo 'DATABASE_CONNECTION_DIE';//数据库连接失败
        exit;
}
$name = $_REQUEST['name'];
$sex = $_REQUEST['sex'];
$mobile = $_REQUEST['mobile'];
$sql = "insert `customer`(`name`,`sex`,`mobile`) values('".$name."','".$sex."','".$mobile."')";
$result=$db->query($sql);
echo "ok";
</code>
Copy after login
Copy after login

The result is that the data has been entered into the database, but the function seems to be invalid. Why?

Does there need to be a space in && inside if?
What about this?
name && sex && mobile

Also, $.get has four parameters. Generally, the last parameter is set to json
You should also change it in your php file accordingly
echo json_encode(array('status' => 1, ' info' => 'ok'));

Open the console and see what results are returned

<code>$.get('user_info_save.php',{name: name,sex: sex,mobile: mobile},function(data){console.log(data);},'JSON')</code>
Copy after login

Declare the returned data type

First check whether the background returns data, then check whether the data returned by the PHP background is json data, then download the usage of $.get in Baidu, and then see if there is any prompt message, and check step by step. Or you can debug the code directly and monitor to see where the problem is. It is difficult to find out the problem simply by looking at the code you sent. It is recommended to use Phpstorm to debug and see

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!