最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Auto-submitting HTML form to self PHP - Stack Overflow

programmeradmin2浏览0评论

I have this simple code:

<?
if (isset($_POST["phpinfo"])) phpinfo(); else echo "you haven't chosen what to do yet";
?>
<h1>Test page 1</h1>
<form method="post" action="">
<select>
<option>Choose what to do</option>
<option value='phpinfo'>phpinfo();</option></select></form>

The problem is that if I use JavaScript (onChange="this.form.submit()") to auto-submit the post data it doesn't submit but if I use a submit button it does submit to self.

<input type="submit" name='phpinfo' id="phpinfo">

My question is, what do I have to do so I can have an auto-submitting drop down list that submits POST data to self?

Thanks in advance!

I have this simple code:

<?
if (isset($_POST["phpinfo"])) phpinfo(); else echo "you haven't chosen what to do yet";
?>
<h1>Test page 1</h1>
<form method="post" action="">
<select>
<option>Choose what to do</option>
<option value='phpinfo'>phpinfo();</option></select></form>

The problem is that if I use JavaScript (onChange="this.form.submit()") to auto-submit the post data it doesn't submit but if I use a submit button it does submit to self.

<input type="submit" name='phpinfo' id="phpinfo">

My question is, what do I have to do so I can have an auto-submitting drop down list that submits POST data to self?

Thanks in advance!

Share Improve this question asked Jan 3, 2012 at 11:11 Icebird RoIcebird Ro 952 silver badges8 bronze badges 1
  • Try giving the <form> an id="" and using document.getElementById().submit() instead. – DaveRandom Commented Jan 3, 2012 at 11:13
Add a ment  | 

4 Answers 4

Reset to default 5

Your select needs to be named. The select name is what's returned to PHP, and its value will be the value of the option selected.

<select name="performaction">
    <option value="phpinfo">phpinfo ()</option>
</select>

if ($_POST [performaction] == 'phpinfo')
{
    phpinfo ();
}
<?php

if($_POST['txtVal'] != "") {
            echo "Form Submited";
       }
?>


 <form method="post" action="">
    <select name="txtVal" onchange="this.form.submit();">
        <option>Choose what to do</option>
        <option value='phpinfo'>phpinfo();</option></select>
 </form>

Hope it will works for you.

Try:


//add a name to form
<form method="post" action="" name="some_name">

//then on your select's onchange function do:
document.forms['some_name'].submit();

Hope it helps

<?php

if(isset($_POST['selctVal']) && $_POST['selctVal']!="" )
{

    echo "Form Submited =><br/>";

    echo  "Form Submited =>".$_POST['selctVal'];
}
else
{
    echo "you haven't chosen what to do yet";

}
?>


 <form method="post" action="" name="anyformname">
    <select name="selctVal" onchange="document.anyformname.submit();">
        <option>Choose what to do</option>
        <option value='<?php echo phpinfo();?>'>phpinfo();</option>
    </select>
 </form>
发布评论

评论列表(0)

  1. 暂无评论