html - how to set selected option of <select> in php -


i need save info , after submit button need output info need selected option stay selected after submit button on form. code output info output contain number state , need state output string

<?php $name = $_post['name']; $state = $_post['state']; $file = $name.".txt"; $txt = $name.",".$state.","; $wrt = file_put_contents($file, $txt, file_append); $get_file = file_get_contents($file);  $info = array($get_file); foreach($info $k => $v) echo $p; $x = explode(",",$v);  ?>     <form action="" method="post">         <input name="name" value="<?php echo $x['0'];?>"/>         <select name="state">             <option value="0"> stat</option>             <?php             $province = array("aa", "ae" ,'ak" , "al" , "ap" , "ar" ,    "as" , "az" , "ca" , "co" , "ct" , "dc" , "de" , "fl" , "fm" , "ga" , "gu" , "hi" , "ia" , "id" , "il" , "in" , "ks" , "ky" , "la" , "ma" , "md" , "me" , "mh" , "mi" , "mn" , "mp" , "mo" , "ms" , "mt" , "nc" , "nd" , "ne" , "nh" , "nj" , "nm" , "nv" , "ny" , "oh" , "ok" , "or" , "pa" , "pr" , "pw" , "ri" , "sc" , "sd" , "tn" , "tx" , "ut" , "va" , "vi" , "vt" , "wa" , "wi" , "wv" , "wy');             foreach($province $k => $v)             echo "<option value='".($k+1)."'>$v</option>";             ?>          <input type ="submit" name="submit" value="trimite"/>         </select>     </form>  

please check below code:-

<?php if(isset($_post) && !empty($_post)){    $name = $_post['name'];    $state = $_post['state'];    // don't know doing here through code    /* $file = $name.".txt";    $txt = $name.",".$state.",";    $wrt = file_put_contents($file, $txt, file_append);    $get_file = file_get_contents($file);     $info = array($get_file);    foreach($info $k => $v)     echo $p;     $x = explode(",",$v); */ } ?>     <form action="" method="post">         <input name="name" value="<?php if (isset($name)){echo $name;}?>"/>         <select name="state">             <option value="0"> stat</option>             <?php             $province = array("aa", "ae" ,'ak" , "al" , "ap" , "ar" ,    "as" , "az" , "ca" , "co" , "ct" , "dc" , "de" , "fl" , "fm" , "ga" , "gu" , "hi" , "ia" , "id" , "il" , "in" , "ks" , "ky" , "la" , "ma" , "md" , "me" , "mh" , "mi" , "mn" , "mp" , "mo" , "ms" , "mt" , "nc" , "nd" , "ne" , "nh" , "nj" , "nm" , "nv" , "ny" , "oh" , "ok" , "or" , "pa" , "pr" , "pw" , "ri" , "sc" , "sd" , "tn" , "tx" , "ut" , "va" , "vi" , "vt" , "wa" , "wi" , "wv" , "wy');             foreach($province $k=> $v){                 if(isset($state) && $state == $k){                     echo "<option value='".$k."' selected = 'selected'>$v</option>";                 }else{                     echo "<option value='".$k."'>$v</option>";                 }             }             ?>           </select>         <input type ="submit" name="submit" value="trimite" style = "margin-top:30px;"/>     </form> 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -