php - input and search form issue -
<form name="search" method="post" action="<?php $_server['php_self'];?>"> seach for: <input type="text" name="find" /> in <select name="field"> <option value="english">english</option> <option value="spanish">spanish</option> </select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="search" /> </form> <?php $options = array('english'=> array('1' => 'one', '2' => 'two'), 'spanish' =>array('1'=>'uno', '2'=>'dos')); if (($_post['find'] == '1')&& ($_post['field'] == 'english')){ echo $options['english']['1']; } ?>
i want have function, when user input '1' , seclet 'english', show one, when user input '1' sellect 'spanish', show uno, , on.
questions:
- is there way replace line?
if (($_post['find'] == '1')&& ($_post['field'] == 'english')){ echo $options['english']['1']; }
imagine if have number 1 100, impossible me repete these codes(==1 && ==english, ==2 && == english...).
2 if u run script, shows
notice: undefined index: find ...
what probem , how fix it?
first point :
<form name="search" method="post" action="<?php $_server['php_self'];?>">
the <?php $_server['php_self'];?>
nothing. shall have use echo here. nevertheless, bug not show because it's default behaviour form call script @ $_server['php_self'] when action attribute not set.
also :
<input type="hidden" name="searching" value="yes" />
seems useless.
second point :
of course don't want parse each possible choice in static fashion. you'll use abstraction , dynamic behavior.
you have 1 array index language , each sub array gives word used each number. hence, have rule , computations :
<?php $input = trim($_post['find']); $language = $_post['field']; $output = $options[$language][$input]; echo $output; ?>
that's minimal code. please don't use 'as is'. you'll have @ least check if keys exist , provide adequate error messages.
Comments
Post a Comment