![](/img/trans.png)
[英]Best way to pass data from PHP to JavaScript for my particular case
[英]Cannot pass data from my JavaScript to my PHP file
當我單擊“ subcribeButton”時,我希望它保存值並將該值傳遞給我的PHP文件。 我想要那個PHP文件然后發布到SQL數據庫。
這是我的js:
$(".subscribeButton").on( "click", function() {
var email = $(".subscribeInput").val();
var isValid = isEmailValid(email);
if(isValid){
$(".errorMsg").css( "display", "none" );
modal.style.display = "block";
$.post( "save.php", { email: email });
$(".subscribeInput").val("");
} else {
$(".errorMsg").css( "display", "initial" );
};
});
$(".subscribeInput").on( "click", function() {
$(".subscribeInput").val("");
});
這是我的php代碼,我希望我的php代碼接受我js文件中的數據,然后將數據發布到我的sql數據庫中:
<?php
$servername = "localhost";
$username = "user";
$password = "pw";
$dbname = "db_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$text = $_POST['email'];
echo $text
$sql = "INSERT INTO MyGuests (email)
VALUES ($text)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
結果是出現以下錯誤:
POST http://flockto.it/home/save.php 500 (Internal Server Error)
send @ jquery.js:4
ajax @ jquery.js:4
m.(anonymous function) @ jquery.js:4
(anonymous function) @ email.js:13
dispatch @ jquery.js:3
r.handle @ jquery.js:3
因此,為什么不在您的ajax請求中添加回調,以便您可以在控制台中對其進行調試,也可以在出現警報時看到它可能會對您有所幫助
$.post( "save.php", { "email" : email } , function(result){
console.log(result);//here the result will display what you will echo after getting the post in your php file
});
因此,在您的php文件中,您可以添加此內容
if (isset($_POST['email'])){
$text = $_POST['email'];
echo $text;
}
//so if you check the console you will get the email
//value from your php file so then you can insert it at the DB as you want
您有語法錯誤。
您的代碼缺少;
在此行的末尾:
echo $text
它應該是:
echo $text;
解決方案:
js文件中的網址錯誤,並且電子郵件周圍的引號丟失了
$(".subscribeButton").on( "click", function() {
var email = $(".subscribeInput").val();
var isValid = isEmailValid(email);
if(isValid){
$(".errorMsg").css( "display", "none" );
modal.style.display = "block";
$.post( "data/save.php", { "email": email });
$(".subscribeInput").val("");
} else {
$(".errorMsg").css( "display", "initial" );
};
});
$(".subscribeInput").on( "click", function() {
$(".subscribeInput").val("");
});
在php文件中使用了錯誤的變量
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$text = $_POST['email'];
if (isset($_POST['email'])){
$text = $_POST['email'];
echo $text;
}
$sql = "INSERT INTO MyGuests (email)
VALUES ('$text')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
一百萬感謝所有回復此主題並幫助我解決問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.