tags:

views:

95

answers:

4

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?

+5  A: 

If I understand you correctly, try this:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
 // your code.........
}
Sarfraz
+1 Nice and simple.
T.J. Crowder
Thanks man! this is just what I need.
SteD
@SteD: You are welcome :)
Sarfraz
A: 

The best solution is "Don't do that". If you want to submit a form then use a submit button (don't do it as a side effect of clicking on a hyperlink to the top of the page). Any JavaScript you want to run can then be handled in the form's submit event.

If you really want to do it as a side effect, then check for the existence of any other field that you know will be set. You could add a hidden field to ensure there will be one of a given name/value combination if you like.

David Dorward
+1  A: 

If I understood your problem correctly that you can simply change input type to hidden.

<form method="post" id="form_submit">
...
<input type="hidden" name="Submit">
</form>

$_POST['Submit'] variable will be defined.

Anpher
+1  A: 

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 ..)

Gaby