How To Pass A Single Form Value With Ajax To Php?
I´m trying to pass a value from an Ajax function to my PHP(in the same page) but can´t manage to catch it. I show the value with an alert and it seems to work properly, but the P
Solution 1:
$(function(){
$('#ucli').on('input', function() {
var client = $('input[name="ucli"]').val();
var dataString = "ucli=" + client;
alert(dataString);
$.ajax({
type: "POST",
data: {ucli: client},
success: function(response){
$('ucli').html(data);
}
});
});
});
Solution 2:
$(function(){
$('#ucli').on('input', function() {
var client = $('input[name="ucli"]').val();
var dataString = "ucli=" + client;
alert(dataString);
$.ajax({
url: "path to php file",
type: "POST",
data: dataString,
success: function(response){
$('ucli').html(data);
}
});
});
});
Solution 3:
You are not using GET method, which means you read the post payload from php://input , like that:
$entityBody = file_get_contents('php://input');
Solution 4:
The PHP code ( when sending to the same page ) needs careful attention in order that the entire page contents are not returned in the response data. To that end I tend to use the following approach - ob_clean
is useful in that it will clear the output buffers of any html that is generated to that point in the document
<?phpif( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['ucli'] ) ){
/*
Prevent other page content prior to this point
being included in whatever response is sent by
clearing the output buffer
*/
ob_clean();
/* Capture post variable */$ucli = filter_input( INPUT_POST, 'ucli', FILTER_SANITIZE_STRING );
/* Do stuff *//* send response */echo$ucli;
/*
Immediately terminate from this portion of code so that
the only response sent is what you determine above
*/exit();
}
?>
The ajax function needs a url - as you are attempting to send a POST request to the same page you can use location.href
. The callback (success) function should process the response whereas before it looked like it was processing the data that was sent.
$( function(){
$('#ucli').on('input', function() {
var client = $('input[name="ucli"]').val();
var dataString = 'ucli=' + client;
$.ajax({
url: location.href,
type: 'POST',
data: dataString,
success: function( response ){
$('ucli').html( response );
},
error: function( err ){
alert( err )
}
});
});
});
Full page example
<!doctype html><html><head><title>jQuery</title><scriptsrc='//code.jquery.com/jquery-latest.js'></script><script>
$( function(){
$('#ucli').on('input', function() {
var client = $('input[name="ucli"]').val();
var dataString = 'ucli=' + client;
$.ajax({
url: location.href,
type: 'POST',
data: dataString,
success: function( response ){
$('#result').html( response );
},
error: function( err ){
alert( err )
}
});
});
});
</script></head><body><?phpif( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['ucli'] ) ){
/*
Prevent other page content prior to this point
being included in whatever response is sent by
clearing the output buffer
*/
ob_clean();
/* Capture post variable */$ucli = filter_input( INPUT_POST, 'ucli', FILTER_SANITIZE_STRING );
/* Do stuff *//* send response */echo$ucli;
/*
Immediately terminate from this portion of code so that
the only response sent is what you determine above
*/exit();
}
?><formname='jqt'method='post'>
ucli:<inputtype='text'name='ucli'id='ucli' /><divid='result'></div></form></body></html>
Post a Comment for "How To Pass A Single Form Value With Ajax To Php?"