0

I'm able to send some POST requests to a php file. On this php file, I use this to check if my POST request is what I want:

<?php
 if(isset($_POST['message'])) {
 // Some stuff here
 }
?>

I would like to know if I can do the same thing in AJAX / JQuery ?

Not something like this:

<script>
 $.post("page.php", $("form[name=addMessage]").serialize(), function(data) {
 //Do something here
 }).error(function() {
 //error
 })
</script>

EDIT:

I don't want to send POST request in AJAX / JQuery. I just want to check if I receive a POST request, send from another page. Indeed, I send a POST request with a field named "message". And I my question is: Is it possible to check if the field "message" is set, but not in PHP, in AJAX / JQquery.

Thank you so much for your help.

Regards, Lapinou.

asked May 3, 2014 at 12:23

3 Answers 3

1

If I understand what you are trying to do: No.

Do you want to just listen for an incoming request in Javascript without calling any Ajax-methods? This is not possible. Javascript needs to be told that "I am sending a request now, and I want a response". It is not possible to say "If any request is sent, deal with it here".

Think about it. How would this work? If Javascript would listen to any request incoming, how would it know the difference between an user submitting a form and you sending a request using Postman?

Also, once you load the website in your browser, this is said to be clientside. Everything that happens after that is bound to your computer and your instance of the website. Any request sent to the site would be sent to the server, not your browser.

answered May 3, 2014 at 12:47
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for the explanation! To be complete, I have a RESTful webservice. And When I send a POST request to my API, I would like to refresh the client. To do this, I use websocket in PHP. It works very fine, but I don't know how to refresh client when I send a POST request from Postman for example. I know it's possible with SignaLR in ASP .NET, but here, I prefere to use PHP. If this is realy not possible, I will refresh by calling my method every 10 seconds or something like this... Thank you anyway ;)
@Lapinou - Ah, that explains a lot. Well. Using websockets in Javasript you could write a PHP-scripts that sends a ping to the websocket once a POST-request is received. Once Javascript gets this ping, it will refresh the page. The alternative is to have an Ajax-request going very 10 seconds that checks if you should update the page or not.
@Lapinou - By the way, if you still have issues I'd suggest closing this, write a descriptive text and create a new question with all the information needed to solve the problem. Guessing my answer pretty much answered the initial question.
Ok. Got it. I will search some info about the ping to Javascript. Currently, I run every 10 sec an Ajax request and it works fine. The must is really when I get the POST request. I close this subject and will open another if needed. Thank you ;)
0

An other way of doing a post is this:

$.ajax({
 type: "POST",
 url: "page.php",
 cache: false,
 data: "message=" + $(".msgBox").val(), //Or Json format { "message" : $(".msgBox").val() },
 success: function(html){
 $(dashboard_id).html(html);
 }
});
answered May 3, 2014 at 12:36

Comments

0

Try this:

$("form[name=addMessage]").submit(function(e) {
 e.preventDefault();
 $.post("page.php", $(this).serialize(), function(data) {
 //Do something here
 }).error(function() {
 //error
 })
});

*Update

As per your comment you want to check POST value using JavaScript / jQuery, I don't think so you can access POST data using JavaScript / jQuery. But you want to mix php then you can do something like this

var post = '<?php json_encode($_POST); ?>';
if (post.message !== undefined) {
}

You have to put var post = '<?php json_encode($_POST); ?>'; in a php file

answered May 3, 2014 at 12:26

4 Comments

But I send a POST request without a form. It's a POST request via PostMan. Is this code will work ?
Yes you can send a POST request without a form, you need to pass the data in object format like $.post("page.php", {message: 'Your message'}, function(data) {
Ok. But I don't want send a POST request in AJAX / JQuery. I just want to check (in AJAX / JQquery) if I received a POST request from another page. So, the equivalent of if(isset($_POST['message'])){}. Thank you ;)
You can not check POST data using JavaScript, answer updated with mix of PHP and JavaScript

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.