我一直在这里寻找许多问题,但似乎对我没有用...它不会更新数据库。
function language(id,lang){
$.ajax({
url: 'modules/tca/updatedb.php',
type: 'POST',
data: 'id='+id+'&lang='+lang,
});
}
<li><a href="#en" onclick="return language($id,"en");"><span class="flag flag-usa flag-1x"></span> EN</a></li>
<li><a href="#fr" onclick="return language($id,"fr");"><span class="flag flag-frc flag-1x"></span> FR</a></li>
这是我的updatedb.php。
<?php
@include_once('setdb.php');
$id = $_POST['id'];
$lang = $_POST['lang'];
mysql_query("UPDATE users SET lang='$lang' WHERE id = '$id' ");
?>
最佳答案
它是工作选项。
<script>
function language($id, $lang){
//get the input value
$.ajax({
//the url to send the data to
url: "modules/tca/updatedb.php",
//the data to send to
data: {id : $id, lang: $lang},
//type. for eg: GET, POST
type: "POST",
//on success
success: function(data){
console.log("***********Success***************"); //You can remove here
console.log(data); //You can remove here
},
//on error
error: function(){
console.log("***********Error***************"); //You can remove here
console.log(data); //You can remove here
}
});
}
</script>
在身体上:
<li><a href="#en" onclick="language(1,'en')"><span class="flag flag-usa flag-1x" ></span>EN</a></li>
<li><a href="#fr" onclick="language(2,'fr')"><span class="flag flag-frc flag-1x"></span> FR</a></li>
还有您的帖子页面(在此示例中为modules / tca / updatedb.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "code";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_POST['id'];
$lang= $_POST['lang'];
$sql = "UPDATE users SET lang='$lang' WHERE id = '$id'";
if ($conn->query($sql) === TRUE) {
echo "New record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
关于javascript - 更新数据库OnClick()<a>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42286305/