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
4 Answers
Reset to default 2document.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
版权声明:本文标题:How to call javascript functionwrite script inside PHP echo - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741828245a2399774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论