ranbir ranbir - 6 months ago 33
PHP Question

how to give name in auto submit form javascript

this is my code that i am using to submit form with post value

<form action="<?php echo DOMAIN; ?>contact/booking-form.php" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
<input type="submit" name="submit" id="submit" />
<script>document.getElementById('submit').submit();</script>
</form>


Can anybody help me to pass name="submit" value of submit button to another page?

Answer

A submit button is only going to be a successful control if it is used to submit the form (and even then only if it has a name and a value … which yours does not).

If you want submit=submit in your form data when you submit the form with JavaScript, then don't use a submit button to put that data in the form in the first place. Use a hidden input.

<input type="submit">
<input type="hidden" name="submit" id="submit" value="submit">

Then you have two other problems.

First, submit is a method of form elements, not inputs. So you need to change your script to call the right element.

<script>document.getElementById('submit').form.submit();</script>

Second, if a form has a control called submit then that will clobber the submit method. So you need to get one from a different form (not supported in old versions of Internet Explorer):

<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>