I can’t get the string from the JS code to the PHP code
here is the code, which initializes when I push enter in a text input:
$('#user').keypress(function(e) {
if(e.which == 13) {
var val = $(this).val();
$.ajax({
type: 'POST',
url: 'index.php',
data: ({'latt':val
})
});
<?php
$val = $_POST['latt'];
echo "alert('$val')";
?>
}
});
where #user is the id of this text input
When I run the code, I see the alert that shows nothing
Before I exactly know that the JS val is not empty
Thank you in advance
P.S. my page is index.php
PHP code runs before the page is sent to the browser. That PHP block will simply echo the value of
$_POST['latt']as it stood when you navigated to the page. You need to access the response from the server in the success callback: