Jump to content

form help


beansandsausages

Recommended Posts

Sorry if this is in the wrong topic. i have a question i have a script

 

 

<select name=\"age\">
<option value=\"10\">10 - 15</option>
<option value=\"15\">15 - 20</option>
</select>

 

is it possable to keep the value selected that the member has chosen for example :

 

member is 15 - 20 so form would be :

 

<select name=\"age\">
<option value=\"10\">10 - 15</option>
<option value=\"15\" selected>15 - 20</option>
</select>

is there any way to do it from the db if you understand what i am trien to say ???

 

sorry if i dont make no sence no good at explaning.

Link to comment
Share on other sites

Yes, there is.

 

Here's a shorthand coding that's always worked for me (I was never any good at the shorthand style, but this one works ;-)

 

<?php
echo "<option value='10'".($age == '10' ? ' SELECTED' : '').">10-15</option>";
?>

 

Or

 

<option value='10'<? ($age == '10' ? ' SELECTED' : '') ?>>10-15</option>

 

Either one should be satisfactory.

 

The code format works like this:

 

(CONDITION ? ECHO TRUE : ECHO FALSE)

 

It's a special case of the if/else conditions, but to just echo out something... It's mostly used in forms for auto-filling things in.

Link to comment
Share on other sites

This is all the code other than the variables :

 

<td width=\"15%\">$space First Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"first_name\" value=\"".$first_name."\"></td>
				    <tr><td width=\"15%\">$space Last Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"last_name\" value=\"".$last_name."\"></td>
				    <tr><td width=\"15%\">$space Email</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"email\" value=\"".$email."\"></td>
				    <tr><td width=\"15%\">$space Age</td><td width=\"1%\">:</td><td width=\"9%\"><select name=\"age\"><option value=\"10\"".($age == '10' ? 'SELECTED' : '')."\">10 - 15</option><option value=\"15\"".($age == '15' ? 'SELECTED' : '')."\">15 - 20</option><option value=\"20\"".($age == '20' ? 'SELECTED' : '')."\">20 - 25</option><option value=\"25\"".($age == '25' ? 'SELECTED' : '')."\">25+ </option></td>
				    <tr><td width=\"15%\">$space Gender</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"checkbox\" name=\"gender\" value=\"male\" checked> Male $space $space $space<input type=\"checkbox\" name=\"gender\" value=\"female\"> Female</td></table> 

Link to comment
Share on other sites

Full file is :

 

<?php

include_once('db.php');

$check = mysql_query("SELECT * FROM members WHERE username = '$username'")or die(mysql_error()); 
while($info = mysql_fetch_array( $check )) 
		$username = $info['username'];
		$first_name = $info['first_name'];
		$email = $info['email'];
		$last_name = $info['last_name'];
		$age = $info['age'];
echo " <form action=\"update.php\" method=\"post\"> <input type=\"hidden\" name=\"id\" value=\"".$id."\">
			  		<table width=\"25%\"><td width=\"25%\">$space <strong><u>Personal Details</u></strong></td></table>
					<table width=\"25%\">
					<td width=\"15%\">$space First Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"first_name\" value=\"".$first_name."\"></td>
				    <tr><td width=\"15%\">$space Last Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"last_name\" value=\"".$last_name."\"></td>
				    <tr><td width=\"15%\">$space Email</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"email\" value=\"".$email."\"></td>
				    <tr><td width=\"15%\">$space Age</td><td width=\"1%\">:</td><td width=\"9%\"><select name=\"age\"><option value=\"10\"".($age == '10' ? 'SELECTED' : '')."\">10 - 15</option><option value=\"15\"".($age == '15' ? 'SELECTED' : '')."\">15 - 20</option><option value=\"20\"".($age == '20' ? 'SELECTED' : '')."\">20 - 25</option><option value=\"25\"".($age == '25' ? 'SELECTED' : '')."\">25+ </option></td>
				    <tr><td width=\"15%\">$space Gender</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"checkbox\" name=\"gender\" value=\"male\" checked> Male $space $space $space<input type=\"checkbox\" name=\"gender\" value=\"female\"> Female</td></table> 
"; 

?>

 

Link to comment
Share on other sites

Hmm, I want you to try something to see exactly what value we're getting out of the age.

 

Echo the $age variable at the top of the page.

 

Add some numbers in between the <select> so we know which one is activating or not activating (if any).

 

I mean like this:

.($age == '10' ? 'SELECTED' : '1').
.($age == '15' ? 'SELECTED' : '2').
//and so on....

 

Then, view the page, check the $age value, if it's null or doesn't exist, that's your problem. If it's a number that doesn't match with any of the option values, that's your problem. If it's a number that SHOULD match with one of the option values, but doesn't, then there's something else that we're missing.

Link to comment
Share on other sites

Hey echoed out ".$age." and gives the correct value.

 

and code

 

<select name=\"age\">
<option value=\"10\" ($age == 10 ? 'SELECTED' : '')>10 - 15</option>
<option value=\"15\" ($age == 15 ? 'SELECTED' : '')>15 - 20</option>
<option value=\"20\" ($age == 20 ? 'SELECTED' : '')>20 - 25</option>
<option value=\"25\" ($age == 25 ? 'SELECTED' : '')>25+</option>
</select>

 

Dosnt work sorry.  Just in case you need age.

 

$age = 20;

Link to comment
Share on other sites

Hey echoed out ".$age." and gives the correct value.

 

and code

 

<select name=\"age\">
<option value=\"10\" ($age == 10 ? 'SELECTED' : '')>10 - 15</option>
<option value=\"15\" ($age == 15 ? 'SELECTED' : '')>15 - 20</option>
<option value=\"20\" ($age == 20 ? 'SELECTED' : '')>20 - 25</option>
<option value=\"25\" ($age == 25 ? 'SELECTED' : '')>25+</option>
</select>

 

Dosnt work sorry.  Just in case you need age.

 

$age = 20;

 

Ok, I wanna try to figure out if anything is even being echoed in the first place (I tried it on my server, seems to work w/o fail).

 

<select name=\"age\">
<option value=\"10\" ($age == 10 ? 'SELECTED' : 'no1')>10 - 15</option>
<option value=\"15\" ($age == 15 ? 'SELECTED' : 'no2')>15 - 20</option>
<option value=\"20\" ($age == 20 ? 'SELECTED' : 'no3')>20 - 25</option>
<option value=\"25\" ($age == 25 ? 'SELECTED' : 'no4')>25+</option>
</select>

 

The actual presentation probably will look funky, so just look at the source code and see what the above code would output for you. If we see all the no's, that means the $age is not equating correctly.

Link to comment
Share on other sites

actual sorce code is :

 


Age</td><td width="1%">:</td><td width="9%"><select name="age"><option value=10 (25 == 10 ? 'SELECTED' : '')>10 - 15</option><option value=15 (25 == 15 ? 'SELECTED' : '')>15 - 20</option><option value=20 (25 == 20 ? 'SELECTED' : '')>20 - 25</option><option value=25 (25 == 25 ? 'SELECTED' : '')>25+</option></select> 25</td>

Link to comment
Share on other sites

actual sorce code is :

 


Age</td><td width="1%">:</td><td width="9%"><select name="age"><option value=10 (25 == 10 ? 'SELECTED' : '')>10 - 15</option><option value=15 (25 == 15 ? 'SELECTED' : '')>15 - 20</option><option value=20 (25 == 20 ? 'SELECTED' : '')>20 - 25</option><option value=25 (25 == 25 ? 'SELECTED' : '')>25+</option></select> 25</td>

 

Whoa... that's odd... you see that when you view the HTML source code? Then the php is not even running in the first place.. o_O

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.