admin管理员组

文章数量:1310297

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

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 edited Oct 12, 2011 at 18:04 Mike Samuel 121k30 gold badges227 silver badges253 bronze badges asked Oct 12, 2011 at 17:54 n92n92 7,60228 gold badges97 silver badges131 bronze badges 1
  • 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 Commented Oct 12, 2011 at 18:09
Add a ment  | 

4 Answers 4

Reset to default 2

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

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 } ?>

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.

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.

本文标签: How to call javascript functionwrite script inside PHP echoStack Overflow