Jump to content

Trying to get the selected item in a select box


Rezznor

Recommended Posts

Hello all, how goes?  Quick question for you here, but I can't figure this, probably because it is late and I am tired.  But here we go.  This is my code:

 

echo '<select name = "testName">';
if ($result){	
while ($row = mysql_fetch_array($result, MYSQL_NUM))	{		
echo "<option value = '" . $row[1] . "'>" . $row[1] . "</option>";	}}
echo "</select>";

[b]$tName = [/b]

 

What it does is it goes through the database and gets all the different test names, then populates the select box.  Later on I want to update the selected test with:

 

$query1 = "UPDATE tests SET numberofQuestions = (tests.numberofQuestions + 1) WHERE testName = '" . $tName . "'";

 

If I have $tName = $row[1];, then it will fill in the last test name of the select box no matter what I have actually clicked on.  What do I have to have $tName = to update the selected test?

 

Thanking you in advance..

Are you trying to have it updated on the same page, or after a post? If after a post, you can access it by $_POST['testName']. If it's on the same page (ie, select from the dropdown and then the value changes on a different part of the page) you'll need to update it with a javascript function that will change the innerhtml of a div

Both. 

echo "<form action = " . $_SERVER['PHP_SELF'] . " method = 'post'>";

 

and

 

if (isset($_POST['submit'])){
do stuff here.  If all the info is filled out and the new test question and answer is done properly, then one of the things is to update the number of questions in that test by selecting the current number in the database and adding one.

 

Hrmmmm, now that I think about it, probably

$tName = $_POST['tName'];

might do the trick.

Nope, doesn't do it.  I put in an echo

echo $query1;
echo "<p>tName = " . $tName;

Just to see if it would indeed go over, but it doesn't.  What I get back is:

 

UPDATE tests SET numberofQuestions = (tests.numberofQuestions + 1) WHERE testName = ''

 

tName =

 

Nothing but blanks.

 

Just in case, this is everything I have for the page so far:

<?php

if (isset($_POST['submit']))
{	
$message = NULL;	
if (empty($_POST['question']))
{
$Ques = FALSE;
$message .= "You forgot to enter a question.";
}
else
{
$Ques = $_POST['question'];
}
if (empty($_POST['answer1']))
{
$ans1 = FALSE;
$message .= "You need to enter at least one answer.";
}
else
{
$ans1 = $_POST['answer1'];	
}
if ($Ques && $ans1)
{
if ($_POST['answer1'])
{
$TF1 = "T";
}
else
{
$TF1 = "F";
}
if ($_POST['TF2'] = NULL)	
{			
if ($_POST['answer2'])			
{				
$answer2 = $_POST['answer2'];			
}			
$TF2 = "F";		
}

// so on and so forth...

$dbc = @mysql_connect("localhost", "name", "password");		
@mysql_select_db("db") or die ("Could not select the database: " . mysql_error() );
$query = "INSERT INTO questions (question, answer1, TF1, answer2, TF2, answer3, TF3, answer4, TF4, answer5, TF5) VALUES ('$question', '$answer1', '$TF1', '$answer2', '$TF2', '$answer3', '$TF3', '$answer4', '$TF4', '$answer5', '$TF5')";
$query1 = "UPDATE tests SET numberofQuestions = (tests.numberofQuestions + 1) WHERE testName = '" . $tName . "'";		
$result1 = mysql_query($query1);		
$result = mysql_query($query);		
if ($result1)		
{			
if ($result)			
{				
echo $query1;				
echo "<p>tName = " . $tName;				
echo "<p>Question and answers succesfully updated.";			
}		
}	
}
}
$dbc = @mysql_connect("localhost","name","password");@mysql_select_db("db") or die ("Could not select the database: " . mysql_error() );
$query = "SELECT * FROM tests";
$result = mysql_query ($query);
echo "<form action = " . $_SERVER['PHP_SELF'] . " method = 'post'>";
echo 'Select test:';
echo '<select name = "testName">';
if ($result)
{	
while ($row = mysql_fetch_array($result, MYSQL_NUM))	
{		
echo "<option value = '" . $row[1] . "'>" . $row[1] . "</option>";	
}		
}
echo "</select>";$tName = $_POST['tName'];?>
<p>Question:<br><textarea rows = "4" cols = "60" name = "question"></textarea>
<p><fieldset><legend>Enter the answers below and place a checkmark next to the correct answer(s):</legend>
<input type = "hidden" name = "tName" value = "<?php echo $tName; ?>">
<input type = "text" name = "answer1" /> <input type = "checkbox" name = "TF1" value = "T" /><p>
<input type = "text" name = "answer2" /> <input type = "checkbox" name = "TF2" value = "T" /><p>

// so on and so forth...

<font color = "red" size = "-1">*note* Although spaces for 5 answers are available, use of all 5 are not necessary<br>
i.e., True/False answers.</font><p><input type = "submit" name = "submit"value = "Add Question"</form></fieldset> 

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.