doushan15559 2017-05-02 11:25
浏览 29
已采纳

如何在不丢失变量值的情况下使用多个表单?

I'm making my first database (pokémon database) for school using PHP and I thought it would be nice if you could sort all pokémon using an 'option form'. Now, I tried to use multiple option forms, so you could combine different selections to get a certain list of pokemon.

For example, would you select 'type=water' and 'sort by name', you would get all water type pokémon, sorted by name. I've tried to do this, but it keeps forgetting the value of the variable in one form, when I select an new option in my other forms.

this is my code (it's very sloppy, sorry for that)

HTML:

<form method="post" action="SQLpokemondb.php">
    <select name="sort" id="sort" onchange="this.form.submit();">
        <option value="ID" <?php if ($option == 'ID'){echo 'selected="selected"';};?>>ID</option>
        <option value="Naam" <?php if ($option == 'Naam'){echo 'selected="selected"';};?>>Naam</option>
        <option value="Soort" <?php if ($option == 'Soort'){echo 'selected="selected"';};?>>Soort</option>
    </select>
</form>
</span>

<form method="post" action="SQLpokemondb.php">
    <select name="gen" id="gen" onchange="this.form.submit();">
        <option value="0" <?php if ($where == '0'){echo 'selected="selected"';};?>>All gens</option>
        <option value="1" <?php if ($where == '1'){echo 'selected="selected"';};?>>Gen 1</option>
        <option value="2" <?php if ($where == '2'){echo 'selected="selected"';};?>>Gen 2</option>
        <option value="3" <?php if ($where == '3'){echo 'selected="selected"';};?>>Gen 3</option>
        <option value="4" <?php if ($where == '4'){echo 'selected="selected"';};?>>Gen 4</option>
        <option value="5" <?php if ($where == '5'){echo 'selected="selected"';};?>>Gen 5</option>
        <option value="6" <?php if ($where == '6'){echo 'selected="selected"';};?>>Gen 6</option>
        <option value="7" <?php if ($where == '7'){echo 'selected="selected"';};?>>Gen 7</option>           
    </select>
</form>
</span>

In case of first visiting the page, I added the next PHP code to give values to variables without a value:

if (empty( $_POST['sort']))
{ 
    $option = 'id';
}
else
{ 
    $option = $_POST['sort'];
}

if (empty( $_POST['gen']))
{   
    $where =  'ID=ID'; 
}
else
{
    $where = $_POST['gen'];
}

if (empty( $_POST['type']))
{
    $where2 =  'ID=ID'; 
}
else
{
    $where2 = $_POST['type'];
}

It will automatically submit the form when you choose a selection. Next thing I do is determine which option was picked using PHP:

if (empty( $_POST['sort']))
{ 
    $sort =  'id'; 
}
elseif ( $_POST['sort'] == 'ID' )
{
    $sort = 'id';
}
elseif ( $_POST['sort'] == 'Naam' )
{
    $sort = 'naam, ID'; 
}
elseif ( $_POST['sort'] == 'Soort' )
{
    $sort = 'type, ID';
}

if (empty( $_POST['gen']))
{ 
    $where =  'ID=ID'; 
}
elseif ( $_POST['gen'] == '0' )
{ 
    $where = 'ID=ID';
}
elseif ( $_POST['gen'] == '1' )
{ 
    $where = 'generation = 1';
}
elseif ( $_POST['gen'] == '2' )
{ 
    $where = 'generation = 2'; 
}
elseif ( $_POST['gen'] == '3' )
{ 
    $where = 'generation = 3';
}
elseif ( $_POST['gen'] == '4' )
{ 
    $where = 'generation = 4';
}
elseif ( $_POST['gen'] == '5' )
{ 
    $where = 'generation = 5';
}
elseif ( $_POST['gen'] == '6' )
{ 
    $where = 'generation = 6';
}
elseif ( $_POST['gen'] == '7' )
{ 
    $where = 'generation = 7';
}

Last, I put the variables into the query for mysql:

mysql_connect("localhost", "root", "usbw");
mysql_select_db("pokemon");

$result = mysql_query("SELECT * FROM pokemon WHERE ($where2) AND ($where) ORDER BY $sort");
while($data = mysql_fetch_assoc($result)) { ........ }

I was unable to find a fix for myself, so I would be grateful if someone could help me.

  • 写回答

1条回答 默认 最新

  • dongziche8030 2017-05-02 11:33
    关注

    The problem is here is that you are using two FORM tags. If the first form sent its value, the second form will be disregarded and vice versa. So wrapping your inputs in a single FORM should make it work.

    <form method="post" action="SQLpokemondb.php">
        <select name="sort" id="sort" onchange="this.form.submit();">
            <option value="ID" <?php if ($option == 'ID'){echo 'selected="selected"';};?>>ID</option>
            <option value="Naam" <?php if ($option == 'Naam'){echo 'selected="selected"';};?>>Naam</option>
            <option value="Soort" <?php if ($option == 'Soort'){echo 'selected="selected"';};?>>Soort</option>
        </select>
    
        <select name="gen" id="gen" onchange="this.form.submit();">
            <option value="0" <?php if ($where == '0'){echo 'selected="selected"';};?>>All gens</option>
            <option value="1" <?php if ($where == '1'){echo 'selected="selected"';};?>>Gen 1</option>
            <option value="2" <?php if ($where == '2'){echo 'selected="selected"';};?>>Gen 2</option>
            <option value="3" <?php if ($where == '3'){echo 'selected="selected"';};?>>Gen 3</option>
            <option value="4" <?php if ($where == '4'){echo 'selected="selected"';};?>>Gen 4</option>
            <option value="5" <?php if ($where == '5'){echo 'selected="selected"';};?>>Gen 5</option>
            <option value="6" <?php if ($where == '6'){echo 'selected="selected"';};?>>Gen 6</option>
            <option value="7" <?php if ($where == '7'){echo 'selected="selected"';};?>>Gen 7</option>
        </select>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测