微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AJAX向数据库发送null到PHP脚本

我正在尝试使用ajax将一个简单的表单插入我的数据库(使用insert.PHP)来练习. var_dump($email)下面是空的.脚本贯穿到这里:

echo "Data for $name inserted successfully!";

问题是如上所述变量为空.

所以我们到那里,但输出一个空变量字段,如下所示:

插入数据成功!

在这里错过了什么吗?

的index.PHP

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">

$(document).ready(function(){
//Get the input data using the post method when Push into MysqL is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#insert").click(function(){
//Get values of the input fields and store it into the variables.
var name=$("#name").val();
var email=$("#email").val();

//use the $.post() method to call insert.PHP file.. this is the ajax request
$.post('insert.PHP', {name: name, email: email},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(1500); //Fade in the data given by the insert.PHP file
});
return false;
});
});
</script>
</head>
<body>
<form>
<label>Name: </label> <input id="name" type="text" />
<label>E-Mail: </label> <input id="email" type="text" />
</form>
<a id="insert" title="Insert Data" href="#">Push into MysqL</a>
 <!-- For displaying a message -->

<div id="message"></div>
</body>
</html>

insert.PHP

 <?PHP
//Configure and Connect to the Databse
 include "db_conx.PHP";
 if (!$db_conx) {
 die('Could not connect: ' . MysqLi_error());
 }
 //Pull data from home.PHP front-end page
 $name=$_POST['name'];
 $email=$_POST['email'];
 echo "<pre>";
var_dump($email);
echo "</pre><br>";
 //Insert Data into MysqL          INSERT INTO best_rate (name,email) 
$query= "INSERT INTO best_rate(name,email) VALUES('$name','$email')";
$result = MysqLi_query($db_conx,$query);
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!"; }
?>

更新PHP#2

<?PHP
//Configure and Connect to the Databse
 include "db_conx.PHP";
 if (!$db_conx) {
 die('Could not connect: ' . MysqLi_error());
 }
 //Pull data from home.PHP front-end page
 $name=$_POST['myname'];
 $email=$_POST['myemail'];
 echo "<pre>";
var_dump($email);
echo "</pre><br>";
 //Insert Data into MysqL          INSERT INTO best_rate (name,email) 
$query= "INSERT INTO best_rate(name,email) VALUES('$name','$email')";
$result = MysqLi_query($db_conx,$query);
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!"; }
?>

HTML#2

<html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <!-- The ajax/jquery stuff -->
    <script type="text/javascript">

    $(document).ready(function(){
    //Get the input data using the post method when Push into MysqL is clicked .. we pull it using the id fields of ID, Name and Email respectively...
    $("#insert").click(function(){
    //Get values of the input fields and store it into the variables.
    var name=$("#name").val();
    var email=$("#email").val();

    //use the $.post() method to call insert.PHP file.. this is the ajax request
    $.post('insert.PHP', {myname: name, myemail: email},
    function(data){
    $("#message").html(data);
    $("#message").hide();
    $("#message").fadeIn(1500); //Fade in the data given by the insert.PHP file
    });
    return false;
    });
    });
    </script>
    </head>
    <body>
    <form>
    <label>Name: </label> <input id="name" type="text" name="myname"/>
    <label>E-Mail: </label><input id="email" type="text" name="myemail"/>
    </form>
    <a id="insert" title="Insert Data" href="#">Push into MysqL</a>
     <!-- For displaying a message -->

    <div id="message"></div>
    </body>
    </html>

表结构

===============================================
id | name | email

db_conx.PHP

<?PHP
$db_conx = MysqLi_connect("localhost", "user", "pass", "database");
if (MysqLi_connect_errno()) {
    echo MysqLi_connect_error();
    exit();
}
?>

解决方法:

我可以看到你有post方法问题,所以我们可以使用$.get而不是$.post并接收$_GET [“name”]上的数据

我认为现在这是正确的解决方案.

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐