Jump to content

drop down and php problem


x_maras

Recommended Posts

Hi,

 

I have a drop down menu that load its options from a database, which consists of categories.

When a category is being selected then another drop down appears with questions who have the category id, also loaded from the database.

When a question is being selected the a form it appears to fill some data.

 

The problem is that the questions disappear when the form is being submitted. If I want to select another question of the same category or even the same question I have to select first the category and then the question.

 

I think this happening because I say that if the category isset then show the second drop down. Maybe a session solve this, but is it possible to show the second drop down without pressing the select button, just from selecting the option?

 

And something else.

How I can make a form to extends itself with the press of a button. For example, in the phpmyadmin when you insert data, you can press a button and some other fields appearing for another insert.

I d like to make something similar.

 

 

Link to comment
https://forums.phpfreaks.com/topic/155955-drop-down-and-php-problem/
Share on other sites

yes ofcourse,  :)

this is an example when i press the button add another posts a new form.

I d like to be more beautifull and adding the new form to the existing one, just like phpmyadmin

 

<?php
/*
adds answers in the questioin that is being selected.
It first appears a drop down menu with the categories. After selecting a category
the questions that are type 2 and 3 are appearing in another drop down menu. 
After the selection,  depending the type of the question,  a form with eom field to fill in 
appears, with 2 buttons. One that just adds the answer in the database and anothe on that 
adds the answer and appears again the form for adding another one answer in the same 
question.
*/
echo "	<form action='' method='post'>
			<h2 align='center'>Add Answers</h2>
			select a category:<br />
			<select name='categories'>
				<option>select a category</option>";
$sqlCategories = "SELECT * FROM categories";
$resCategories = mysql_query($sqlCategories);
while($category = mysql_fetch_array($resCategories)) {
	echo"		<option value='". $category['id'] ."'>". $category['category'] ."</option>";
}
echo "		</select>
			<input type='submit' name='selectCategory' value='select' />
		</form>";

//checks the first selection
if(isset($_POST['selectCategory'])){
	echo "<form action='' method='post'>
			select a question:<br />
			<select name='question'>
				<option>select a question</option>";
	$sqlQuestions = "SELECT * FROM questions WHERE cat_id='". $_POST['categories'] ."' AND type_id <> '1'";
	$resQuestions = mysql_query($sqlQuestions);
	while($question  = mysql_fetch_array($resQuestions)){
		echo "	<option value='". $question['id'] ."'>". $question['text'] ."</option>";
	}
	echo "	</select>
			<input type='submit' name='selectQuestion' value='select' />
		  </form>";
}

//checks the second selection
if(isset($_POST['selectQuestion'])){
	$_SESSION['storeQuestionIdForAddingAnswer'] = $_POST['question'];
	$sqlAnswers = "SELECT * FROM answers WHERE quest_id='". $_POST['question'] ."'";
	$resAnswers = mysql_query($sqlAnswers);
	$checkAnswers = mysql_fetch_array($resAnswers);
	if($checkAnswers['type'] == 2){
		echo "<form action='' method='post'>
				answer:
				<input type='text' name='answer' size='60' />
				image url:
				<input type='text' name='url' size='70' />
				<br />
				<input type='submit' name='add2' value='add' />
				<input type='submit' name='addAnother2' value='add another one' />
			  </form>
			  <br />
			  <br />
			  <font color='red' size='3'>*The picture shouldn't be bigger than 100x80!!!</font>";
	}elseif($checkAnswers['type'] == 3){
		echo "<form action='' method='post'>
				answer:
				<input type='text' name='answer' size='60' />
				<br />
				<input type='submit' name='add3' value='add' />
				<input type='submit' name='addAnother3' value='add another one' />
			  </form>";
	}
}

//checks if one of the 2 add buttons is pressed for the image answers or
//checks if one of the 2 add buttons is pressed for the check answers
if(isset($_POST['add2']) || isset($_POST['addAnother2'])){
	$questId = $_SESSION['storeQuestionIdForAddingAnswer'];
	$sqlInsert = "INSERT INTO answers (quest_id, answer, image_url, type) VALUES ('". $questId ."', '". $_POST['answer'] ."', '". $_POST['url'] ."', '2')";
	mysql_query($sqlInsert);
	echo "	<br />
			<font color='blue'>added succesfully!</font>";
	if(isset($_POST['addAnother2'])){
		echo "<form action='' method='post'>
				answer:
				<input type='text' name='answer' size='60' />
				image url:
				<input type='text' name='url' size='70' />
				<br />
				<input type='submit' name='add2' value='add' />
				<input type='submit' name='addAnother2' value='add another one' />
			  </form>
			  <br />
			  <br />
			  <font color='red' size='3'>*The picture shouldn't be bigger than 100x80!!!</font>";
	}
}
elseif(isset($_POST['add3']) || isset($_POST['addAnother3'])){
	$questId = $_SESSION['storeQuestionIdForAddingAnswer'];
	$sqlInsert = "INSERT INTO answers (quest_id, answer, type) VALUES ('". $questId ."', '". $_POST['answer'] ."', '2')";
	mysql_query($sqlInsert);
	echo "	<br />
			<font color='blue'>added succesfully!</font>";
	if(isset($_POST['addAnother3'])){
		echo "<form action='' method='post'>
				answer:
				<input type='text' name='answer' size='60' />
				<br />
				<input type='submit' name='add3' value='add' />
				<input type='submit' name='addAnother3' value='add another one' />
			  </form>";
	}
}
?>

 

sorry if my code is not so clear,

newbie  ;D

I rewrote a great deal of your code, this should do the trick in a more proper fashion. Read it and verify you understand the code.

 

<?php
session_start();

function htmlSelect($name, $options) {
   $htmlSelect = "<select name=\"$name\" id=\"$name\">\r\n";
   foreach ($options as $option => $optionText) {
       $htmlSelect .= "\t<option value=\"$option\">$optionText</option>\r\n";
   }
   return $htmlSelect . "</select>\r\n";
}

function htmlForm($formBody, $action, $method = 'post', $enctype = 'application/x-www-form-urlencoded') {
   $htmlForm = "<form action=\"$action\" method=\"$method\" enctype=\"$enctype\">";
   $htmlForm = $htmlForm . $formBody . '<input type="submit" name="submit" id="submit" value="Submit" />';
   return $htmlForm . '</form>';
}

function getCategory($category) {
   $categoryQuery = "SELECT * FROM categories WHERE cat_id = '$category'";
   $queryResult = mysql_query($categoryQuery);
   return mysql_fetch_assoc($queryResult);
}

function displaySurveyEntry($surveyEntry) {
    echo $surveyEntry['question']['question'];
    print_r($surveyEntry['answers']);
}

/**
* If no category is set display the form and wait for it to be submitted
*/
if (!(isset($_GET['cat']) && is_numeric($_GET['cat']))) { // ?cat=<number> should always be set or you will get the select form again
   $categoryQuery = "SELECT * FROM categories";
   $queryResult = mysql_query($categoryQuery);
   $categories = array();
   while ($category = mysql_fetch_assoc($queryResult)) {
       $categories[$category['id']] = $category['category'];
   }
   echo htmlForm(htmlSelect('cat', $categories), $PHP_SELF, 'get');
}

/**
* If the survey hasn't yet been created or a refresh has been ordered, then start creating/refreshing the survey.
*/
else if (!(isset($_SESSION['survey']) && sizeof($_SESSION['survey'])) || isset($_GET['refresh'])) { // ?refresh=1 to refresh the survey best to use as last
    $category = getCategory($_GET['cat']);
    $category = $category['id'];
    $questionQuery = "SELECT * FROM questions WHERE cat_id = '$category'";
    $queryResult = mysql_query($questionQuery);
    $survey = array();
    while ($question = mysql_fetch_assoc($queryResult)) {
        $surveyEntry =& $survey[];
        $surveyEntry['question'] = $question;
        $questionId = $question['id'];
        $answerQuery = "SELECT * FROM answers WHERE quest_id = '$questionId'";
        $_queryResult = mysql_query($answerQuery);
        while ($answer = mysql_fetch_assoc($queryResult)) {
            $surveyEntry['answers'][] = $answer;
        }
    }
    $_SESSION['survey'] = $survey;
    echo displaySurveyEntry($_SESSION['survey'][0]);
}

if (sizeof($_POST) && isset($_POST['answer'])) {
    ..validate..
    ..verify answer..
    // if a valid answer was supplied then remove it from the survey
    array_shift($_SESSION['survey']);
    echo displaySurveyEntry($_SESSION['survey'][0]);
}
?>

The last part should be:

 

<?php

else if (sizeof($_POST) && isset($_POST['answer'])) {
    ..validate..
    ..verify answer..
    // if a valid answer was supplied then remove it from the survey
    array_shift($_SESSION['survey']);
    if (sizeof($_SESSION['survey'])) {
       echo displaySurveyEntry($_SESSION['survey'][0]);
    } else {
        // all questions have been answered
    }
}
?>

 

Their is also a little bug in the code:

<?php
while ($answer = mysql_fetch_assoc($queryResult)) {
   $surveyEntry['answers'][] = $answer;
}
?>

 

should be:

<?php
while ($answer = mysql_fetch_assoc($_queryResult)) {
   $surveyEntry['answers'][] = $answer;
}
?>

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.