Maintain button submit value when using link to su

2019-09-02 08:36发布

问题:

i am trying to create a link that submits a form. I use this and works fine :

<a name="submit" href="javascript:document.theForm.submit();" class="rollover-button gray small"><span>Send Message</span></a>

However, i have a problem. My previous submit button was :

<input type="submit" name="submit" value="Send Message" />

When this was clicked, i was getting a $_POST['submit'] value that i was checking with isset in my php script, to see whether the form is submitted or not. However, this does not work with my submit link. Does anybody know how i can do that ?

EDIT:

I tried that, as suggested :

<form action="." name="theForm" class="contactForm" method="post">

<input type="hidden" name="submit" value="Send Message" />

</form>

<a name="submit" href="javascript:document.theForm.submit();" class="rollover-button gray small"><span>Send Message</span></a>

But still does not work.

回答1:

You can create input type of hidden and check for its existence:

if (isset($_POST['hiddenName'])) {....}


回答2:

You can use a hidden field instead. So when the form is submitted, you can check if the hidden element exists.

Like this:

<input type="hidden" name="submit" value="Send Message" />

This way, you can check for $_POST['submit'] when you submit the form. Just make sure the hidden <input> is inside the <form> element, so it will POST with the rest of the form.



回答3:

add a hidden input.

<input type="hidden" name="submit" value="Send Message" />

it will not be visible to the user, but it will be send with the form contents.



回答4:

You can always hide the submit button (with css display: none) and click it with JavaScript:

document.forms.theForm.elements.submit.click();