简体   繁体   中英

How to successfully $_POST <options> from a <select> to another page in PHP?

I know there have a been a lot of questions like this but none (that I have found, and I've looked at many, but correct me if I am wrong) have solved my problem.

As the title of the question suggests, I need to Post select options to another page. My code looks something like this:

<form id="join_pool_form" name="join_pool_form"  action="connect.php" method="post"> 

<select name="pool_name" style="width:170px">
<?php
            //The options are from an array. This works fine.
        foreach($poolnames as $value):
            echo '<option value="">'.$value.'</option>';
        endforeach;
        unset($value);
?>
</select>

<input name="passcode" type="password" autofocus required id="passcode" size="35"style="width:170px">

<input type="submit" id="submit" value="Join Pool"> 

</form>

The connect.php (action of the form) page has something like this:

<?php

if (isset($_POST['pool_name'])) {
    echo "do this";
}else {
    echo "do other";
}

So the result of this should be "do this", but I always get "do other". I'm very new to this, so excuse me if the solution is extremely simple. However, I have looked over it multiple times to no avail.

Thanks in advance.


Thanks everyone for your help. Problem solved.

you just have to pass the value in the html attribute of the option:

<select name="pool_name" style="width:170px">
<?php
        //The options are from an array. This works fine.
    foreach($poolnames as $value):
        echo '<option value="'.$value.'">'.$value.'</option>';
    endforeach;
    unset($value);
?>
</select>
<?php

if (isset($_POST['pool_name'])) {
    $variable =  $_POST['pool_name'];
}else {
    $variable =  'Unknown';
}

The value of the select option has to be set to what your intending to send. If you don't set it, it will send nothing.

<option value='test'>Dont choose this</option>

The above will send the value test.

replace

echo '<option value="">'.$value.'</option>';

with

echo '<option value="'.$value.'">'.$value.'</option>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM