如何提交值并在下一页中选择该选项?

时间:2014-08-01 03:25:40

标签: javascript php html forms

我不太了解HTML,所以当我点击一个按钮说life时,当我点击此按钮时,它应该转到下一页(这是一个联系人)表格)并应从下拉列表中选择该选项。

第一页

<input type="submit" name="type" value="trauma">

第二页

<select class="form-control" id="type" name="type">
    <option value="life" >Life Insurance</option>
    <option value="trauma">Trauma Insurance</option>
    <option value="tpd">Total &amp; Permanent Disability Insurance</option>
    <option value="income">Income Protection Insurance</option>
    <option value="redundancy">Redundancy Insurance</option>
    <option value="private">Private Medical Insurance</option>
    <option value="mortgage">Mortgage Protection Insurance</option>
    <option value="health">Health Insurance</option>
</select> 

任何人都可以建议我使用服务器端或cliet端代码吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

当然,有很多方法可以解决这个问题。在第一页(我们称之为first_page.php)上,设置一个表单。 action=""属性应指向第二页(我们称之为second_page.php)。那么第二页应该能够获得来自第一页的值并进行处理。在此示例中,使用了$_GET

first_page.php(简单表格)

<form method="GET" action="second_page.php">
    <h1>Select Insurance</h1>
    <button type="submit" name="button" value="life">Life</button>
    <button type="submit" name="button" value="trauma">Trauma</button>
</form>

这只是一个简单的表格。 method="GET"action="second_page.php"

second_page.php

// if there is a button variable
$selected = '';
if(isset($_GET['button'])) {
    $selected = $_GET['button']; // get the value
}

// default select values
$select_values = array(
    'life' => 'Life Insurance',
    'trauma' => 'Trauma Insurance',
    'tpd' => 'Total &amp; Permanent Disability Insurance',
    'redundancy' => 'Income Protection Insurance',
    'private' => 'Private Medical Insurance',
    'mortgage' => 'Mortgage Protection Insurance',
    'health' => 'Health Insurance',
);

?>

// loop it, in iteration, if the selected value from the first page matches,
// then add the attribute SELECTED
<select class="form-control" id="type" name="type">
    <?php foreach($select_values as $value => $name): ?>
        <option value="<?php echo $value; ?>" <?php echo ($selected == $value) ? 'selected' : '' ; ?> ><?php echo $name; ?></option>
    <?php endforeach; ?>
</select>
相关问题