user2507818
user2507818

Reputation: 3037

php: issue with the select box

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> 

I want to have this kind of function: when someone inputs one package on test5.php, the same package will be chosen/selected in select-box on test4.php, but it seems does not work, so what is wrong with the scripts above?

Upvotes: 0

Views: 83

Answers (6)

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

also try with trim($_POST['package']) for remove spaces

<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> 

Upvotes: 1

PravinS
PravinS

Reputation: 2584

add echo in each option as

<?php echo ($_POST['package'] == "Goa")? "selected":"";?>

Upvotes: 1

Phil
Phil

Reputation: 164776

You need to do two things...

  1. Enable error reporting during development.

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);
    
  2. Echo selected

    <?= isset($_POST['package']) && $_POST['package'] == 'Goa' ? 'selected' : '' ?>
    

Upvotes: 1

klkvsk
klkvsk

Reputation: 670

You forgot echo.

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

For a simpler notation you can use <?=, that will print out result of any expression inside <?= ?>

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

Upvotes: 1

vee
vee

Reputation: 38645

You want to echo out the value as follows:

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

Upvotes: 1

James
James

Reputation: 1572

I think it is as simple as an echo

Upvotes: 1

Related Questions