Page 1 of 1

DropList problem

Posted: Wed Dec 28, 2005 10:34 am
by galligraph
Could semeone help me?

I'm using PHP with MySql. I got a DropList where's 4 options. If i choose one of the 3 first options the value should go to the database. If i choose option 4 i need 2 write something 2 the textarea and the text should go 2 the database. So if u have written somethin' 2 the textarea and the option 4 is not chosen the text don't go 2 the database. So simple question is... How 2 make the post code or do i have 2 change something else?

Code: Select all

<select name='answer'>"; 
	echo "<option value='yes'>Yes</option> ";
	echo "<option value='no'>No</option> ";
	echo "<option value='else'>Else</option> ";
	echo "<option value='text'>Text</option> ";
	echo "</select>"; 

	echo "<textarea name='textanswer' colls='2' rows='5'></textarea>";

Re: DropList problem

Posted: Wed Dec 28, 2005 11:22 am
by LazyJones

Code: Select all

if(($_POST['answer'] == "text") && ($_POST['textanswer'] != "")) {
	// write to database
}

Posted: Wed Dec 28, 2005 11:47 am
by galligraph
Thx a lot LazyJones :wink: Answer was quick and that was exactly what i was lookin' 4...

Posted: Wed Dec 28, 2005 1:10 pm
by Ambush Commander
Erm... code probably throws a E_NOTICE. May I suggest checking for the existence of the variables before using them?

Edit

Code: Select all

if((isset($_POST['answer']) && $_POST['answer'] == "text") &&
   (isset($_POST['textanswer']) && $_POST['textanswer'] !== '')) {
   // write to database
}
Instill good coding practices!