如何在下面的示例中从文本框中检索值

In my code below I am trying to INSERT VALUES into a database. I am using a switch statement where if a textbox equals one of the cases, then it matchs with one of the ‘OptionType’ rows in the database and from that display the ‘OptionId’ which matches the ‘OptionType’.

The problem though is that while doing a var_dump on the $optionquery, it shows that it is not retireving the value from the textbox. How can I get $selected_option from the query to retrieve the value from the textbox ($_POST[‘gridValues’])?

At the moment I am trying to use the case statement to retrieve the values from the textbox. E.g. If the textbox contains the number '4', then case '4' should be triggered.

Now below is the code of the textbox which is appended into a new table row in the application:

     <script>

            function insertQuestion(form) { 

  var context = $('#optionAndAnswer');  

            var $tbody = $('#qandatbl > tbody'); 
            var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
            var $options = $("<td class='option'></td>");
            var $questionType = '';

            $('.gridTxt', context).each( function() {

             var $this = $(this);
             var $optionsText = $("<input type='text' class='gridTxtRow maxRow' readonly='readonly' />").attr('name',$this.attr('name')+"[]")
                             .attr('value',$this.val())

            $options.append($optionsText);
            $questionType = $this.val();

            });

    $tr.append($options);
    $tbody.append($tr); 

        </script>


        <form id="QandA" action="insertQuestion.php" method="post" >

        <table id="optionAndAnswer" class="optionAndAnswer">
        <tr class="option">
        <td>Option Type:</td>
        <td>
        <div>
            <input type="text" name="gridValues" class="gridTxt maxRow" readonly="readonly" />
        </div>
        </td>
        </tr>
        </table>

        </form>

Below is the php code where all the database stuff happens:

$insertquestion = array();


$options = $_POST['gridValues'];

switch ($options){

    case "3": 
    $selected_option = "A-C";
    break;

    case "4": 
    $selected_option = "A-D";
    break;

    case "5": 
    $selected_option = "A-E";
    break;

    default:
    $selected_option = "";
    break;

}      

$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '" 
     . mysql_real_escape_string($selected_option)."')";

$optionrs = mysql_query($optionquery);
$optionrecord = mysql_fetch_array($optionrs);
$optionid = $optionrecord['OptionId'];



foreach($_POST['questionText'] as $question)
{
    $insertquestion[] ="' ".  mysql_real_escape_string( $optionid ) . "'";
}

  $questionsql = "INSERT INTO Question (OptionId) 
  VALUES (" . implode('), (', $insertquestion) . ")";



echo($questionsql);

var_dump($optionquery);

mysql_close();

You need to loop around your switch (based on your var_dump):

$option_array = array();
foreach( $options as $i )
{
  switch( $i )
  {
    case '1':
    if( !empty( $i ) ){ $option_array[] = $selected_option; }
    break;
    //so on, and so forth for all of your field data...
  }
}

Now that you have all of your field values re-worked and stored in the $option_array, you can loop through:

foreach( $option_array as $i )
{
   //do whatever mysql jazz you need to do to get your data
}