0

test5.php

<form action="test4.php" method="post">
Please input one package:(Goa, Kashmir, Rajasthan):<input type="text" name="package">
<input type="submit" value="submit">
<form>

test4.php

<label>Select Tour Package<span class="note">*</span>:</label>    
   <select name="package">  
    <option value="Goa" <?php ($_POST['package'] == "Goa")? "selected":"";?>>Goa</options>  
    <option value="Kashmir" <?php ($_POST['package'] == "Kashmir")? "selected":"";?>>Kashmir</options>  
    <option value="Rajasthan" <?php  ($_POST['package'] == "Rajasthan")? "selected":"";?>>Rajasthan</options>  
   </select> 

我想有这样的功能:当有人在 上输入一个包时test5.php,会在 select-box on 中选择/选择相同的包test4.php,但似乎不起作用,那么上面的脚本有什么问题?

4

6 回答 6

1

我认为这很简单echo

于 2013-08-02T05:27:53.570 回答
1

您想echo按如下方式输出值:

<option value="Goa" <?php echo ($_POST['package'] == "Goa")? "selected":"";?>>Goa</options>
于 2013-08-02T05:28:12.427 回答
1

你忘了echo

<option value="Goa" <?php echo ($_POST['package'] == "Goa") ? "selected" : "" ?>>Goa</options>

对于您可以使用的更简单的符号<?=,它将打印出内部任何表达式的结果<?= ?>

    <option value="Goa" <?= ($_POST['package'] == "Goa") ? "selected" : "" ?>>Goa</options>
于 2013-08-02T05:28:18.777 回答
1

你需要做两件事...

  1. 在开发期间启用错误报告。

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);
    
  2. 选择回声

    <?= isset($_POST['package']) && $_POST['package'] == 'Goa' ? 'selected' : '' ?>
    
于 2013-08-02T05:29:55.327 回答
1

在每个选项中添加 echo 作为

<?php echo ($_POST['package'] == "Goa")? "selected":"";?>
于 2013-08-02T05:30:13.037 回答
1

也尝试使用trim($_POST['package'])删除空格

<select name="package">  
        <option value="Goa" <?php if($_POST['package'] == "Goa") echo 'selected="selected"';?>>Goa</options>  
        <option value="Kashmir" <?php if($_POST['package'] == "Kashmir") echo 'selected="selected"';?>>Kashmir</options>  
        <option value="Rajasthan" <?php  if($_POST['package'] == "Rajasthan") echo 'selected="selected"';?>>Rajasthan</options>  
       </select> 
于 2013-08-02T05:31:50.897 回答