Jump to content

Saving drop down box selection


highjack

Recommended Posts

Hi all,

            Sorry if my code is messy, all I'm trying to do is saved what some drop down lists are set as 1) when you submit the page. and 2)based on information from the database. I have put a small description of the two pages that aren't working and attached my source code.

 

If anyone has any ideas what is wrong with my code I would be greatful. Thanks alot. Now for the actual problem,

 

The following code is meant to get tq_answer which will be something like:

2,3,4. Each of these are split up using "explode()", the first dropdown should then set "checked" on when the tq_id is 2, the second when tq_id is 3 and so on.

 

I have a feeling I've done something pretty stupid here, but I'm not sure what.

 

<?php 
//include db connection
include ("inc.php");

$e = $_GET['e'];

print '<table>';

$result = mysql_query("SELECT tq_answer FROM tbl_questions WHERE tq_id={$e}");

while ($row = mysql_fetch_array($result))
{
  $answers = $row['tq_answer'];
}


$canswers = explode(",", $answers);
$count = count($canswers);


$j = 0;
for ($i = 0; $i < $count; $i++)
{

$checked = "";
$snext = explode(",",$next);

print '<tr><td><input type="text" name="answers" value="' . $canswers[$j] . '"></td><td><select name="next">';

$result = mysql_query("SELECT tq_id, tq_question FROM `tbl_questions`");


if ($snext[$j] == $row['tq_id'])
	{
	$checked = " checked";
	}

while ($row = mysql_fetch_array($result))
{

	print '<option value="' . $row['tq_id'] . '"' .  $checked . '>' . $row['tq_question'] . '</option>';

}
$j++;
print  '</select></td></tr>';
}
print '<tr><td><input type="submit" name="" value="update">';
print '</table>';
?>

 

The other problem I have is the inverse of this, it generates a drop down box with the information from the database, but i need this to be saved when its submited, at the moment ive tried to save it a session, but this could be taken from a $_POST instead.

 

<?php
//include db connection and session_start();
include ("inc.php");

$result = mysql_query("SELECT tq_id, tq_question FROM tbl_questions");

while($questions = mysql_fetch_array($result))
  {
  $checked = "";
  
if($_REQUEST['questiondropdown' . $questions['tq_id']] == $questions['tq_id'])
{ 
      $_SESSION['blah'] = " checked";
}
  
  
  $questiondropdown .=  '<option value="' . $questions['tq_id'] . '"' . $checked . '>' . $questions['tq_question'] . '</option>';
  }
  
  $questiondropdown .= '</select>';
  
  $temp = $questiondropdown;

for ($i=1; $i<=$_REQUEST['number']; $i++)
	{
	$questiondropdown = '<select name="questiondropdown' . $i  . '">' . $temp;
	print '<tr><td>' . $questiondropdown . '</td></tr>';
	}
?>

Link to comment
https://forums.phpfreaks.com/topic/149504-saving-drop-down-box-selection/
Share on other sites

In the first bit the if statement needed to be inside the while loop

 

<?php 
//include db connection
include ("inc.php");

$e = $_GET['e'];

print '<table>';

$result = mysql_query("SELECT tq_answer FROM tbl_questions WHERE tq_id={$e}");

while ($row = mysql_fetch_array($result))
{
  $answers = $row['tq_answer'];
}


$canswers = explode(",", $answers);
$count = count($canswers);


$j = 0;
for ($i = 0; $i < $count; $i++)
{

   $checked = "";
   $snext = explode(",",$next);
   
   print '<tr><td><input type="text" name="answers" value="' . $canswers[$j] . '"></td><td><select name="next">';
   
   $result = mysql_query("SELECT tq_id, tq_question FROM `tbl_questions`");
   
   while ($row = mysql_fetch_array($result))
   {
   if ($snext[$j] == $row['tq_id'])
      {
      $checked = " checked";
      }
      
      print '<option value="' . $row['tq_id'] . '"' .  $checked . '>' . $row['tq_question'] . '</option>';
   
   }
   $j++;
   print  '</select></td></tr>';
}
   print '<tr><td><input type="submit" name="" value="update">';
   print '</table>';
?>

 

and im not entirly sure what your doing in the second.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.