我一直在爲這個問題困擾了一段時間,我仍然不明白爲什麼它不起作用。我已經嘗試了多種可能性,但沒有一個可以工作,所以有人可以幫助我如何通過Ajax將var superstr = $("#savelyric").text();
傳入我的數據庫?這就是我一直在嘗試用:將字符串通過jquery和ajax傳遞給PHP
function saveinPHP() {
//alert("Came here");
var lyr = $("#savelyric").text();
var superstr = { lyricsave:lyr }
//var superstr = 'lol';
//var hashString = "lol";
//var data = { yoururl:'hmm'}
$.ajax({
type: "POST",
url: "includes/sendlyrics.php",
data: superstr,
success: function(data){
alert("***DATA***"+data+"***MSG***");
alert("Settings has been updated successfully." + data + "~~~" + hashString);
//window.location.reload(true);
}
});
}
正如你所看到的,我試着做這件事的多種方式,但它只是永遠不會奏效。我不明白你是如何做到這一點的。和PHP文件是這樣的:
<?php
include ('db_connect.php');
$data = $_POST['data'];
$query = "UPDATE song SET time='".$data."' WHERE id='1'";
mysqli_query($query);
?>
是的,我深知,我的數據庫是脆弱的SQL注入,我會盡快解決這個問題,因爲我得到這個工作。
這是我試過的,但如果你認爲有必要,我可以做完全不同的事情。
現在我得到了JS:
function saveinPHP() {
var superstr = $("#savelyric").text();
$.ajax({
type: "POST",
url: "includes/sendlyrics.php",
data: {superstr: superstr},
success: function(data){
alert("***DATA***"+data+"***MSG***");
alert("Settings has been updated successfully." + data + "~~~");
//window.location.reload(true);
}
});
和PHP
<?php
include ('db_connect.php');
$data = $_POST['superstr'];
$query = "UPDATE song SET lyrtime='".$data."' WHERE id='1'";
mysqli_query($query);
?>
http://stackoverflow.com/questions/20150130/ajax-and-php-to-enter-multiple-forms-input - 數據庫/ 20150474#20150474 – MonkeyZeus
'$ data = $ _POST ['data'];'應該是'$ data = $ _POST ['songsave'];' – MonkeyZeus
爲什麼你期望你的數據在'$ _POST [ '數據']'?你發佈的對象是'superstr',所以它只有'lyricsave'這個鍵。您是否已經使用'var_dump'查看'$ _POST'內是否有任何內容? –