自动将HTML表单提交给自己的PHP

时间:2012-01-03 11:11:06

标签: php javascript html

我有这个简单的代码:

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

问题是,如果我使用JavaScript(onChange =“this.form.submit()”)来自动提交帖子数据,它就不会提交,但如果我使用提交按钮,它会提交给自己。< / p>

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

我的问题是,我该怎么办?我可以有一个自动提交下拉列表,将POST数据提交给自己?

提前致谢!

4 个答案:

答案 0 :(得分:5)

您的选择需要命名。选择名称是返回给PHP的名称,其值将是所选选项的值。

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

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

答案 1 :(得分:1)

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

希望它适合你。

答案 2 :(得分:0)

尝试:


//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();

希望有所帮助

答案 3 :(得分:0)

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