Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a form is submitted via javascript?

Tags:

jquery

php

submit

I have this conventional submit button which submit a form like this:

<form method="post" id="form_submit">
...
<input class="button" type="submit" name="Submit" value="Submit">
</form>

And I check if the submit button is clicked using this:

if(isset($_POST['Submit'])){
   //update DB
}

Now I have a submit link using jquery:

<a href="#" onclick="publish(); return false;">Submit</a>

JS code:

$("#form_submit").submit();

What is the alternative way here to be used here for if(isset($_POST['Submit'])) since I'm submitting the form using javascript?

like image 620
SteD Avatar asked May 23 '10 11:05

SteD


People also ask

What happens when a form is submitted in JavaScript?

The submit event triggers when the form is submitted, it is usually used to validate the form before sending it to the server or to abort the submission and process it in JavaScript.

Does one submit a form using JavaScript?

The method form. submit() is used for dynamic creation and sending the details to the server. The JavaScript form submission can be used for object creation and various attributes can also be used. The attributes can be class, id, tag, etc.

How do you check form is submitted or not in jquery?

submit(function(e) { e. preventDefault(); $("#some-span"). html(data); });

How can I show form data after submitting?

The formtarget attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form. The formtarget attribute overrides the target attribute of the <form> element. Note: The formtarget attribute is new for the <input> element with type="submit" in HTML5.


2 Answers

If I understand you correctly, try this:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
 // your code.........
}
like image 175
Sarfraz Avatar answered Sep 17 '22 12:09

Sarfraz


You should add a hidden input <input type="hidden" name="formsubmit" value="yes" /> to the form which will always get submitted, and check for that instead of the button (which only gets submitted if it is clicked on ..)

like image 39
Gabriele Petrioli Avatar answered Sep 17 '22 12:09

Gabriele Petrioli