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

时间:2022-10-23 17:16:47

I'm trying to use ajax to insert using a simple form into my database(using insert.php) to practice. Below the var_dump($email) is hitting null. The script runs through to here:

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

echo "Data for $name inserted successfully!";

The problem is the variables are null as stated.

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

So we make it to there, but the output is an empty variable field like below:

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

Data for inserted successfully!

插入数据成功!

Am I missing something here?

我在这里错过了什么吗?

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!"; }
?>

UPDATE 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>

Table Structure

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

db_conx.php

<?php
$db_conx = mysqli_connect("localhost", "user", "pass", "database");
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}
?>

4 个解决方案

#1


1  

I can see you are having post method issue so we can use $.get instead of $.post and receive the data on $_GET["name"]

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

I think this is correct solution for now.

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

Thanks

#2


4  

you havent gave name attribut to your feilds

你还没有给你的名字命名

<input id="name" type="text" />

use instead

<input id="name" type="text" name="myname"/>

and then used like this in your php file

然后在你的php文件中使用这样的

$name=$_POST['myname'];

#3


0  

I have checked your code and working correctly, as I can see there might be some issue with database connection or something mysql related. Your code working correct no need to give name or any other parameter in HTML as you have posted and given variable in jquery.

我检查了你的代码并正常工作,因为我可以看到数据库连接可能存在一些问题或与mysql有关。您的代码正常工作无需在HTML中提供名称或任何其他参数,因为您已在jquery中发布并给出变量。

If you want more details you need to provide mysql related config file and table structure so I can check correctly.

如果您需要更多详细信息,您需要提供与mysql相关的配置文件和表结构,以便我可以正确检查。

Thanks

#4


0  

It sounds to me that the values from the inputs aren't getting passed to the php script to insert them.

听起来我输入的值没有传递给php脚本来插入它们。

I have noticed in your code that you pass an oject that contains these values:

我在你的代码中注意到你传递了一个包含这些值的对象:

$.post('insert.php', {myname: name, myemail: email},

I beleive that you are setting the name of the property (ie. myname) incorrectly. From my understanding, the javascript is interpriting myname as a variable rather than a name. The correct code would be:

我相信你正确地设置了属性的名称(即.myname)。根据我的理解,javascript将myname作为变量而不是名称进行interpriting。正确的代码是:

$.post('insert.php', {'myname': name, 'myemail': email},

This would then properly set the POST variables to use in your php code.

然后,这将正确设置POST变量以在您的PHP代码中使用。

#1


1  

I can see you are having post method issue so we can use $.get instead of $.post and receive the data on $_GET["name"]

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

I think this is correct solution for now.

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

Thanks

#2


4  

you havent gave name attribut to your feilds

你还没有给你的名字命名

<input id="name" type="text" />

use instead

<input id="name" type="text" name="myname"/>

and then used like this in your php file

然后在你的php文件中使用这样的

$name=$_POST['myname'];

#3


0  

I have checked your code and working correctly, as I can see there might be some issue with database connection or something mysql related. Your code working correct no need to give name or any other parameter in HTML as you have posted and given variable in jquery.

我检查了你的代码并正常工作,因为我可以看到数据库连接可能存在一些问题或与mysql有关。您的代码正常工作无需在HTML中提供名称或任何其他参数,因为您已在jquery中发布并给出变量。

If you want more details you need to provide mysql related config file and table structure so I can check correctly.

如果您需要更多详细信息,您需要提供与mysql相关的配置文件和表结构,以便我可以正确检查。

Thanks

#4


0  

It sounds to me that the values from the inputs aren't getting passed to the php script to insert them.

听起来我输入的值没有传递给php脚本来插入它们。

I have noticed in your code that you pass an oject that contains these values:

我在你的代码中注意到你传递了一个包含这些值的对象:

$.post('insert.php', {myname: name, myemail: email},

I beleive that you are setting the name of the property (ie. myname) incorrectly. From my understanding, the javascript is interpriting myname as a variable rather than a name. The correct code would be:

我相信你正确地设置了属性的名称(即.myname)。根据我的理解,javascript将myname作为变量而不是名称进行interpriting。正确的代码是:

$.post('insert.php', {'myname': name, 'myemail': email},

This would then properly set the POST variables to use in your php code.

然后,这将正确设置POST变量以在您的PHP代码中使用。