5

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?

2
  • Are you submitting via AJAX/some plugin, or you want to know .submit() was invoked? If you submit the form via javascript, the button that otherwise would be present, will not be in the posted data. Commented May 23, 2010 at 12:02
  • I'm working on a facebook app, and I'm calling a FB popup before invoking the .submit(), thanks for everyone's help, Sarfraz nailed the problem right. The hidden input is a good choice nonetheless :) Commented May 23, 2010 at 12:08

4 Answers 4

5

If I understand you correctly, try this:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
 // your code.........
}
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

1

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.

Comments

0

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.

Comments

Your Answer

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.