我在將該變量傳遞給php頁面時遇到問題。未將Ajax Post值傳遞給php文件
這裏是下面的代碼:
var varFirst = 'something'; //string
var varSecond = 'somethingelse'; //string
$.ajax({
type: "POST",
url: "test.php",
data: "first="+ varFirst +"&second="+ varSecond,
success: function(){
alert('seccesss');
}
});
PHP:
$first = $_GET['first']; //This is not being passed here
$second = $_GET['second']; //This is not being passed here
$con=mysqli_connect("localhost","root","pass","mydb");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO mytable (id, first, second) VALUES ('', $first, $second)");
mysqli_close($con);
}
我我失去了一些東西?實際的數據保存到數據庫BUT $ first和$ second value沒有被傳遞給php文件。
您應該將您的數據作爲JSON傳遞。 – Chris