html - How do i echo selected options from a loop of questions with php -


i have loop echo questions database form predetermined answers in select option yes, no, not sure. user can answer amount of question except @ least 1 question must answered. how echo question(s) , corresponding selected answer user on submitting form.

here retrieve questions dbase

<form method="post" action=""> <?php              $sql = 'select starttime, country,league, home, away,gamecode      games      starttime > date_format(now(),"%d/%m/%y %h:%i")      order starttime' ;     $retval = mysql_query($sql);     if(! $retval )     {       die('could not games date selected: ' . mysql_error());     }      while($row = mysql_fetch_array($retval, mysql_assoc))     { 

here echo question user

 echo "{$row['country']} | {$row['league']} | time:{$row['starttime']}  <br> ".    "<div align='center'><span class='style3'>{$row['home']} vs ".          "{$row['away']} <br> ". 

here, echo predetermined selectable answers each of above question

edited //my form head somewhere above sql query

     "<select name='gm" . $row['gamecode'] ."' >          <option value=''>select option</option>     <option value='btsyes'>bts (yes)</option>     <option value='btsno'>bts (no)</option>     <option value='over2.5'>over2.5(total goals)</option>     <option value='under2.5'>under 2.5(total goals)</option>     <option value='oddtg'>odd(total goals)</option>     <option value='eventg'>even(total goals)</option>     </select></span></div>".              "<hr>";     }   ?>     <input type="submit" name="play" value="register bet" />     </form></div> 

all above works fine here want echo question , answer selected user confirmation

this try

 if (isset($_post ['play'])){ echo "confirm selection </br>"; foreach ($_post $key => $value ) {   if($value !=''){           echo $key."-".$value;            echo "<br>";      }else{ echo "no valid selection made <br>";}     } } 

i use developers tool , on network tab, dont know exacly focus on. if user selects fist 2 options confirm selection 1 2 no valid selection made no valid selection made no valid selection made register bet

<select name= 'gm[]'>          <option value='so'>select option</option> <option value='btsyes'>bts (yes)</option> <option value='btsno'>bts (no)</option> <option value='over2.5'>over2.5(total goals)</option> <option value='under2.5'>under 2.5(total goals)</option> <option value='oddtg'>odd(total goals)</option> <option value='eventg'>even(total goals)</option> </select> 

by doing gm[] saying values array. can have multiple values set under $_post['gm']

additional

your form send selects default, if default value not provided first value selected , transferred. avoid that, did when checking if value != ''

<select name="gm"><option value=''>choose answer</option></select> 

if user not select '' empty field passed, , skip it.

edit #2 working copy

<form action="" method="post">  <select name='gm[firstquestion]'>     <option value=''>select please</option>     <option value='foo'>foo</option>     <option value='bar'>bar</option>     <option value='zoo'>zoo</option>     <option vlaue='park'>park</option> </select> <br> <select name='gm[secondquestion]'>     <option value=''>select please</option>     <option value='foo'>foo</option>     <option value='bar'>bar</option>     <option value='zoo'>zoo</option>     <option vlaue='park'>park</option> </select> <br> <select name='gm[thidquestion]'>     <option value=''>select please</option>     <option value='foo'>foo</option>     <option value='bar'>bar</option>     <option value='zoo'>zoo</option>     <option vlaue='park'>park</option> </select> <br> <input type="submit" value="answer"> 

constructing form php code

foreach($_post['gm'] $key => $answer){     if($answer != ''){         echo "your question #" . $key . ' answer ' . $answer . php_eol;     }  } 

check output


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -