I call ajax at my action.php file to get one data from DB and load it dynamically.
$.ajax({
url: 'ajax.php',
type: "POST",
data: "localid="+ <?php echo $prelocal; ?>,
dataType: 'json',
success: function(response){
console.log("repo: "+response);
}
});
My ajax.php is quite simple. I added if condition to check if I will call that file directly, and it is working
<?php
$localid = $_POST['localid'];
if(empty($localid)) {
$localid = 1;
}
$i = "SELECT `userid` FROM `table` WHERE `localid` = '{$localid}'";
$ri = $conn->query($i);
$v = $ri->fetch_assoc();
echo json_encode($v);
?>
Call ajax.php directly giving me response
{"userid":"4"}
But at action.php I do not get any console.log response (why?). What I want to do, is to get only digit as value to input. May I ask for hint?
You need to change your javascript as following.
var local_id = <?php echo $prelocal; ?>;
$.ajax({
url: 'ajax.php',
dataType: 'json',
type: 'POST',
data: {localid:local_id},
success: function(data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});