问题
I am using this javascript code to object a data of a td when pressing a button in an onclick event
function rfk(element) {
var element = element.parentElement.parentElement;
var id = parseInt(element.children[0].innerText);
$.ajax({
url: './queryuser.php',
type: "POST",
dataType:'text',
data: {'id': id},
});
}
when using an alert instead of var = id shows the value of the td
but this is failing the sentence of ajax when sending to php I do not know how to solve it
PHP code queryuser.php :
<?php
require("conextion.php");
require("sql.php");
$connect=start();
$display= show($connect,$_POST['id']);
disconnect=($connect);
header("Location:index.php");
?>
Query sql:
function show($con, $id){
$query=" SELECT user_name FROM user
WHERE $id='?'";
$call = $con->prepare($query);
$call ->execute();
return $call;
}
回答1:
Pass the data like this to the ajax call (http://api.jquery.com/jQuery.ajax/): and select elements perfectly to get text
function rfk(element) {
var element = element.parentElement.parentElement;
var id = parseInt(element.children[0].innerText);
$.ajax({
url: '/queryuser.php',
type: "POST",
data: {id: id}
});
}
来源:https://stackoverflow.com/questions/53489168/how-to-use-ajax-to-send-a-javascript-variable-to-php