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 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类