sql - PHP Populate Dropdown From Database and then Update on Button Click -


i'm having trouble populating dropdown list database , updating selected item on button click. i'm trying have when pick option b in dropdown, it'll update option on click of submit button.

i can dropdown box populate fine, when clicking button won't update correctly. i'm learning go, pointers appreciated.

<form id='filter' name='filter' method='post' action=''>                  <?php                  $getissuedvouchers2 = "select * vouchercodes status = 'active'";                 $issuedvouchersresult2 = mysql_query($getissuedvouchers);                  ?>                  <select>                      <?php                          while ($ivselectrow = mysql_fetch_array($issuedvouchersresult2)) {                             echo "<option name='updatestatus'>" . $ivselectrow['vouchid'] . "</option>";                         }                      ?>                  </select>                  <input type="submit" value="update record" name="submit" />                  <?php                     if(isset($_post['submit'])) { //if submit button clicked                      $updatingquery = "update vouchercodes set status='expired' vouchid = '".$ivselectrow['vouchid']."' ";                     mysql_query($updatingquery) or die("cannot update");//update or error                     }                 ?>              </form> 

php server side language. runs once when request page, , after done serves page you. not run until page request, php can't because that's client side action.

what looking javascript , ajax. client side languages. i'd recommend using jquery this, it's pretty easy use once hang of it. makes lot less awful standard javascript.

if aren't wanting avoid page reload (submit -> handler -> page reload), can cook answer that. maybe.


update comment: want set "selected" attribute, makes option selected on page load, correct? e.g. pick second item in dropdown instead of first.

you need use selected attribute on option tag. makes selected. here's example use on 1 of sites.

<select data-wavenum='<?=$i;?>' id="enemy<?=$i;?>" name="enemy<?=$i;?>" class="enemylist">    <option value="assaulttrooper" <?=($enemy == 'assaulttrooper') ? "selected" : ""?>>assault trooper</option> 

this prints out like:

   <option value="assaulttrooper" selected>assault trooper</option> 

in case, $enemy item makes me print selected out makes option selected.


in case, be. it's kind of ugly. think fixed of code, without db backend can't test.

while ($ivselectrow = mysql_fetch_array($issuedvouchersresult2)) {    $selected = false;    if (isset($_post['submit'])) {     $selected = ($_post['vouchid'] == $ivselectrow['vouchid']);    }    echo "<option name=".$ivselectrow['vouchid']." ".($selected) ? "selected" : "".">" . $ivselectrow['vouchid'] . "</option>"; } 

please aware change option name or send post cause expire value, or delete entire table data not sanitized.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -