Icebird Ro
Icebird Ro

Reputation: 95

Auto-submitting HTML form to self PHP

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!

Upvotes: 2

Views: 3143

Answers (4)

Boopathi
Boopathi

Reputation: 166

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

Upvotes: 0

Muthu Krishnan
Muthu Krishnan

Reputation: 1658

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

Upvotes: 1

GordonM
GordonM

Reputation: 31730

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

Upvotes: 5

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

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

Upvotes: 0

Related Questions