Reputation: 1049
<form name="search" method="post" >
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="category1">category1</option>
<Option VALUE="category2">category2</option>
</Select>
<input type="submit" name="search" value="Search" />
</form>
<?php
if(!empty($_POST)){
$options = array('category1'=> array('1' => 'dog', '2' => 'cat'), 'category2' =>array('1'=>'flower', '2'=>'grass'));
$input = trim($_POST['find']);
$category = $_POST['field'];
$output = $options[$category][$input];
echo $output;
}
?>
Question:
if i input 1 and select 'category2', it shows:flower, but the input filed became empty, and select box went back to 'category1', is there a way that i can let input box and select box keep the value i put there, in my case, after i click submit botton, '1' still is in input field, and 'category2' shows in select box.
Upvotes: 3
Views: 5550
Reputation: 20199
Use value="<?php if(isset($_POST[])) echo $_POST[]; ?>"
<form name="search" method="post" >
Seach for: <input type="text" name="find" value="<?php echo (isset($_POST['find']) ? $_POST['find'] : ''); ?>" /> in
<Select NAME="field">
<Option VALUE="category1" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category1') ? 'selected="selected"': ''; ?>>category1</option>
<Option VALUE="category2" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"': ''; ?>>category2</option>
</Select>
<input type="submit" name="search" value="Search" />
</form>
Upvotes: 2
Reputation: 16495
Let's say this is your username input/form
<input type="text" name="username" value="" />
now inside where it says value=""
, You have to put PHP code to be remembered if someone posts something. Much like:
<?php if(isset($_POST['username'])) echo $_POST['username']; endif ?>
which means, if a persone already submits a username, it will stay there, else, it will just be empty.
You could create a little function though, instead of putting that much of code inside,
Upvotes: 2