Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have written piece of code, below is the code

<script type="text/javascript">
function submitform()
{
    document.myform.action='http://mysite/index.php';
    document.myform.submit();
}
</script>

<?php
if(isset($_POST['submit_details']))
{
       echo "<script typ=javascript> submitform();</script>";
}

?>
<form id="myform">
  ------
  ------
<input type="submit" name="submit_details">
</form>

when i try to submit the form , it gives me error document.myform is undefined, how to solve this error

share|improve this question
1  
There's a lot wrong with your code as written. You have syntax errors in your PHP code. You're using Javascript to do something the browser will do using <form action="http://mysite/index.php" method="GET"> and if this is index.php, then your PHP will automatically submit the form again... and again... and again... – Herbert Oct 12 '11 at 18:09

4 Answers

document.myForm is undefined when you are calling the script because it is being run before the form element has been received by the browser. You need to put the script after the form tag, or use a document.onload event handler (or similar).

(Although quite why you want to automatically and immediately submit a form by JS without your user doing anything is beyond me.)

share|improve this answer

If that's literally your code, then it's got syntax errors up the wazoo.

Try:

<?php if (isset($_POST['submit_details'])) { ?>
   <script type="text/javascript">submitForm();</script>
<?php } ?>
share|improve this answer
i added the above code at the end of the page, the form is getting submitted but the form values are empty.form is submitting has an empty form – Vinay Oct 12 '11 at 19:02

Try moving the script to the bottom of the page, or better, listen to the window.onload event. When the script is loaded by the browser, the DOM hasn't finished loading and cannot find document.myForm yet.

share|improve this answer

Why dont you just put the javascript submit inside the input like so?

<input type="submit" name="submitdetails" onclick="javascript:submitform();" />

Then use $post with jquery to run the page.

share|improve this answer
1  
The javascript: is superfluous. – Mike Samuel Oct 12 '11 at 18:05
Okay, so now I want to learn, why? – Juan Gonzales Oct 12 '11 at 18:06
By doing this, he wouldn't have to call the function in php. I thought it would be more efficient. – Juan Gonzales Oct 12 '11 at 18:11
1  
You don't need to add javascript: inside on* attributes of elements. Suggesting he add jQuery to his page is overkill - why include a whole JS library to do something simple that can be done in a quick, easy, cross-browser manner? – daiscog Oct 12 '11 at 18:16
1  
The value of an onclick attribute is not a URL, so you don't need the javascript protocol. <input onclick="submitform()"> is just fine. Not that the OP needs javascript -- they could just put the action in the form. – Mike Samuel Oct 12 '11 at 18:18
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.