2015-04-20 40 views
3

我想使用ajax插入使用一個簡單的形式到我的數據庫(使用insert.php)來練習。在var_dump($email)下面是空。腳本貫穿到這裏:AJAX到數據庫發送null到PHP腳本

echo "Data for $name inserted successfully!"; 

問題是變量爲null,如上所述。

所以我們讓它在那裏,但輸出是像下面一個空的變量字段:

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

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> 

表結構

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

db_conx.php

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

只是爲了調試,可以使用'GET'而不是'POST'。如果您使用Chrome瀏覽器,然後按控制檯,然後按'F12'。點擊「插入數據」鏈接並檢查值是否通過ajax請求傳遞。 –

+0

@ℛⱥℐℰşℎnope沒有數據通過。 – Chris

回答

1

我可以看到你有文章的方法問題,所以我們可以使用$不用彷徨,而不是$。員額和接收$ _GET數據[「名稱」]

我認爲這是正確的解決方案了。

謝謝

4

你還沒有給名attribut您精密組件

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

改用

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

,然後在使用這樣的你的php文件

$name=$_POST['myname']; 
+0

我已經更新了我的OP。這是你的意思嗎? – Chris

+0

yess ........... –

+0

仍然返回空值。尋找在螢火控制檯I看到交示出 'myemail \t d MYNAME \t D' 和 源是'MYNAME = d&myemail = D' – Chris

0

我檢查了您的代碼並正常工作,因爲我可以看到可能存在與數據庫連接或與mysql有關的某些問題。你的代碼工作正確,不需要在HTML中提供名稱或任何其他參數,就像你在jQuery中發佈和給定變量一樣。

如果您想了解更多詳細信息,您需要提供與mysql相關的配置文件和表結構,以便我可以正確檢查。

感謝

+0

表單本身正在發送空白行,所以我的假設是我們沒有問題連接,對吧? – Chris

+0

我已檢查並且數據正確發送到insert.php。我檢查了我的本地主機,它正在用你的代碼正確地插入數據,所以我確信與數據庫或連接有關的東西。 –

+0

好吧我只是上傳表結構和db_conx.php – Chris

0

這聽起來對我來說,從輸入的數值沒有得到傳遞給PHP腳本插入。

我已經在你的代碼注意到你傳遞一個包含這些值的oject:

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

我beleive你設置的屬性的名稱(即MYNAME)不正確。根據我的理解,這個javascript將myname作爲變量而不是名稱。正確的代碼是:

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

然後,這將正確設置POST變量,以在您的PHP代碼中使用。

+0

改變了這一塊,但我仍然得到''INSERT INTO best_rate(name,email)VALUES('','')「'好像它們還沒有通過 – Chris