After submitting a form via ajax, I want to get the data that I passed into the form and put it in a JS variable.
<form>
<input type="text" />
<input type="submit" value="Submit" />
</form>
$("#form").submit(function(event) {
var form = $(this);
$.ajax({
type: 'POST',
url: form.attr('action'),
data: form.serialize(),
success: function(data) {
alert(data);
}
});
event.preventDefault(); // avoid to execute the actual submit of the form.
});
So how do I get the data (eg what the person posted into input) processed via POST and put it in a JS variable? NOTE: I do NOT want to get the input via input.val(), It MUST be the data submitted into the databse.
In your server side code you will receive the posted data in $_POST
variable.So if you want to receive it back in the response then simply post it back to the ajax request.
Try
echo json_encode($_POST);
And also change the dataType of your ajax like this
dataType : 'json'
And in your ajax success try console.log(data)
to see the response and check your browser console
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.