I have the following form where a worker code is entered.
<form id="tck" method="POST">
<input type="text" name="ticket" maxlength="10">
<button type="submit">ticket</button>
</form>
And this data is sent throughAJAX
$(document).ready(function () {
$(".dismiss").click(function () {
$(".response").fadeOut(500);
});
$(document).on('submit', '#tck', function () {
var data = $(this).serialize();
$.ajax({
type: 'POST',
url: 'employee.php',
data: data,
success: function (response) {
//$(".response").html(response);
$(".response>span.message").html(response);
$(".response").fadeIn(200);
window.setTimeout(function () {
$('.response').fadeOut(500);
}, 6000);
}
});
return false;
});
});
So, what I need is to know what value was sent in the following input
and save it in a session
.
How can I know what data was sent in the field input
?
The question works like this:
A. On the client side:
index.php
You have a form and Javascript/jQuery code that handles sending the Ajax request.
What Ajax does in this case is send/receive things to and from the server through a file that is executed on the server side. In your example that file would be
employee.php
.It is important that you know that Ajax requests have some methods that help you to know what has happened on the server side. Generally the Ajax request should have at least one method
done
and one other methodfail
. The methoddone
would do almost the same thing as your current methodsuccess
, but the latter is deprecated since jQuery 3, so it's better to usedone
.Ok, so inside
done
will handle the possible successful response from the server side. There you can collect what the server responded and operate with that data, to display it on the screen or for any other thing.But the request may fail. In that case, you must implement a method
fail
which will report the failure and its possible cause.The client side code would then be this. Here I put
index.php
complete.I have made several changes. Among them, I have put the action on the form directly, that way the jQuery code is more uniform. One very important thing is that for the file URL to work it
employee.php
must be correct. Since you have it now, it should be in the same folder where theindex.php
. If it is not in the same folder, you must indicate it.I've removed the
beforeSend
... I don't know if it's necessary. I have left whatfadeOut
you have and the setTimeOut... although it is not clear what you need them for. I say this so you know that this is not necessary, unless you need it for something.B. On the server side:
employee.php
The content of
employee.php
should handle the data sent throughdata
in the jQuery request. In this case it is the content of a serialized form.One way to test whether the expected elements have data is to use
isset
a ternary operator.The PHP would look something like this:
If all goes well, implementing this code, you will see in the
div
withid='respuesta'
the content of the variable$ticket
.Other types of data can be sent from PHP, for example a complete JSON object... or other things.
The possibilities of Ajax are very interesting in this sense, for example, obtaining information from the server and dynamically updating the content that is presented on the screen, without having to refresh the page...