Hello everybody this is my first post. I have a web page in php that allow the users to enter a value from three radio buttons. The problem is, i want to allow the users too change their selection on the edit page but first their selection has to be loaded from the database(mysql) so that the value can be seen as checked on the edit page. The user can insert and update but their previous radio button selection dosen't show up .

I would really appreciate it if someone could help me with this problem. I would post the code that i have but it is too long.

Member Avatar for diafol

So post a snippet.

BTW: I suggest using a more specific thread title in the future.

Hello everybody this is my first post. I have a web page in php that allow the users to enter a value from three radio buttons. The problem is, i want to allow the users too change their selection on the edit page but first their selection has to be loaded from the database(mysql) so that the value can be seen as checked on the edit page. The user can insert and update but their previous radio button selection dosen't show up .

I would really appreciate it if someone could help me with this problem. I would post the code that i have but it is too long.

First form

$garageopt   =trim($_POST['garageopt']);

<label class="yesGreen"><strong><input type="radio" name="garageopt"value="yes">Yes</strong></label>
                    <label class="pendingAmbur"><strong><input type="radio" name="garageopt"'value="pending">Pending</strong></label>
                    <label class="noRed"><strong><input type="radio" name="garageopt"value="no" checked>No</strong></label>

edit form

<td>
<label class="yesGreen"><strong><input type="radio" name="garageopt"value="<? echo garageopt=$_POST['$garageopt'] ?> ">Yes</strong></label> 
<label class="pendingAmbur"><strong><input type="radio" name="garageopt"value="<? echo $_POST['$garageopt'] ?> ">Pending</strong></label> 
<label class="noRed"><strong><input type="radio" name="garageopt"value="<? echo $_POST['$garageopt'] ?> " >No</strong></label> 
 </td>

So post a snippet.

BTW: I suggest using a more specific thread title in the future.

Ok i'll do that

Member Avatar for diafol

Ok your values for the option group are 'yes', 'pending' and 'no'. I assume that these are the values set in the DB.

I'll give you a quick bit of code that I use from time to time, but it ain't great:

<?php
$opvalue = $r['option']; //assume that this is the DB value for the options ['yes','no','pending'];
?>

<input type="radio" id="yesop" name="garageopt"value="yes" <?php if($opvalue=='yes')echo 'checked="checked"';?> />
<label for="yesop" class="yesGreen">Yes</label>
<input type="radio" id="pendingop" name="garageopt"value="pending" <?php if($opvalue=='pending')echo 'checked="checked"';?> />
<label for="pendingop" class="pendingAmbur">Pending</label> 
<input type="radio" id="noop"name="garageopt"value="no" <?php if($opvalue=='no')echo 'checked="checked"';?> />
<label for="noop" class="noRed">No</label>

You can remove the strong tags as bold type can be applied via your css rules.

Ok your values for the option group are 'yes', 'pending' and 'no'. I assume that these are the values set in the DB.

I'll give you a quick bit of code that I use from time to time, but it ain't great:

<?php
$opvalue = $r['option']; //assume that this is the DB value for the options ['yes','no','pending'];
?>

<input type="radio" id="yesop" name="garageopt"value="yes" <?php if($opvalue=='yes')echo 'checked="checked"';?> />
<label for="yesop" class="yesGreen">Yes</label>
<input type="radio" id="pendingop" name="garageopt"value="pending" <?php if($opvalue=='pending')echo 'checked="checked"';?> />
<label for="pendingop" class="pendingAmbur">Pending</label> 
<input type="radio" id="noop"name="garageopt"value="no" <?php if($opvalue=='no')echo 'checked="checked"';?> />
<label for="noop" class="noRed">No</label>

You can remove the strong tags as bold type can be applied via your css rules.

I'll try it. So nothing has to be loaded from the database when the edit page has been called or sessions? Thanks for your help

$garagesql = "UPDATE garage SET  garageopt= '" . $_POST['garageopt'] . "' WHERE id = " . $validid . ";";
		 mysql_query($garagesql);
		 $garagesid = mysql_insert_id();

Ok your values for the option group are 'yes', 'pending' and 'no'. I assume that these are the values set in the DB.

I'll give you a quick bit of code that I use from time to time, but it ain't great:

<?php
$opvalue = $r['option']; //assume that this is the DB value for the options ['yes','no','pending'];
?>

<input type="radio" id="yesop" name="garageopt"value="yes" <?php if($opvalue=='yes')echo 'checked="checked"';?> />
<label for="yesop" class="yesGreen">Yes</label>
<input type="radio" id="pendingop" name="garageopt"value="pending" <?php if($opvalue=='pending')echo 'checked="checked"';?> />
<label for="pendingop" class="pendingAmbur">Pending</label> 
<input type="radio" id="noop"name="garageopt"value="no" <?php if($opvalue=='no')echo 'checked="checked"';?> />
<label for="noop" class="noRed">No</label>

You can remove the strong tags as bold type can be applied via your css rules.

The example doesn't seem to work. Thanks for the help anyway.

Member Avatar for diafol

I'll try it. So nothing has to be loaded from the database when the edit page has been called or sessions? Thanks for your help

Yes, of course, the stored value has to be called. That was the reason behind the inclusion of:

$opvalue = $r['option']; //assume that this is the DB value for the options ['yes','no','pending'];

Where $r is taken from something like this:

$q = mysql_query("SELECT ... ");
$r = mysql_fetch_array($q);
//so if you have a field called option which has the yes/pending/no values:
$opvalue = $r['option'];
//this places the value into the $opvalue variable

Yes, of course, the stored value has to be called. That was the reason behind the inclusion of:

$opvalue = $r['option']; //assume that this is the DB value for the options ['yes','no','pending'];

Where $r is taken from something like this:

$q = mysql_query("SELECT ... ");
$r = mysql_fetch_array($q);
//so if you have a field called option which has the yes/pending/no values:
$opvalue = $r['option'];
//this places the value into the $opvalue variable

this is what i have and it works on all the fields except the radio buttons
edit form //update row

$garagesql = "UPDATE garage SET  garageopt= '" . $_POST['garageopt'] . "',Length = " . $_POST['Length'].   WHERE id = " . $validid . ";";
		 mysql_query($garagesql);
		 $garagesid = mysql_insert_id();

// select row

$dataAsql = "SELECT * FROM garage WHERE id = " . $validid . ";";
	$dataAresult = mysql_query($dataAsql);
	$dataAnumrows = mysql_num_rows($dataAresult);

	if($dataAnumrows == 0) {
		header("Location: " . $config_basedir);
	}

// check row

if($dataAnumrows == 0) {
		header("Location: " . $config_basedir);
	}
else {
		$dataArow = mysql_fetch_assoc($dataAresult);

<input type="radio" id="yesop" name="garageopt" value="yes"<?php if($garageopt=='yes')echo 'checked="checked"';> /><strong><label for="yesop" class="yesGreen">Yes</label></strong>
 <input type="radio" id="pendingop" name="garageopt" value="pending" <?php if($garageopt=='pending')echo 'checked="checked"';?> /><strong><label for="pendingop" class="pendingAmbur">Pending</label></strong>
 <input type="radio" id="noop" name="garageopt" value="no" <?php if($garageopt=='no')echo 'checked="checked"';?> checked=checked/><strong><label for="noop" class="noRed">No</label></strong>

		        <tr>    
	                <td>Length </td> 
	   			    <td><input type="text" name="Length" id="Length"value="<?php echo $dataArow['Length']; ?>" onfocus="clearMe(this)"></td> 

//rest of code goes here...

The above code works fine with the input fields, but not on the radio button nor does the code you provided, sorry but it doesn't seem to work on the radio buttons.

Member Avatar for diafol

Does your form processing script actually save the data to the DB in the first place?

If so, just echo out your 'garageopt' field to see what it gives.

If it spits out 'yes','pending' or 'no', you should be able to use the code I supplied to fill the edit form.

Does your form processing script actually save the data to the DB in the first place?

If so, just echo out your 'garageopt' field to see what it gives.

If it spits out 'yes','pending' or 'no', you should be able to use the code I supplied to fill the edit form.

$garagesql = "INSERT INTO garage(garageopt,Length) VALUES('" . addslashes($_POST['garageopt']). "'," . $_POST['Length']. ");";
mysql_query($garagesql);
$garageid = mysql_insert_id();

Yes the data is saved in the DB, but the radio button doesn't show the selected value held in the DB 'yes','pending', or 'no' when the edit page has finished loading.

Member Avatar for diafol

I think there's a mistake in your variable - it hasn't been initiated/set:

$dataArow = mysql_fetch_assoc($dataAresult);
 
<input type="radio" id="yesop" name="garageopt" value="yes"<?php if([B]$garageopt[/B]=='yes')echo 'checked="checked"';> /><strong><label for="yesop" class="yesGreen">Yes</label></strong>
 <input type="radio" id="pendingop" name="garageopt" value="pending" <?php if($garageopt=='pending')echo 'checked="checked"';?> /><strong><label for="pendingop" class="pendingAmbur">Pending</label></strong>
 <input type="radio" id="noop" name="garageopt" value="no" <?php if($garageopt=='no')echo 'checked="checked"';?> checked=checked/><strong><label for="noop" class="noRed">No</label></strong>

$garageopt is empty. You first need to set it thus:

$garageopt = $dataArow['garageopt'];

I think there's a mistake in your variable - it hasn't been initiated/set:

$dataArow = mysql_fetch_assoc($dataAresult);
 
<input type="radio" id="yesop" name="garageopt" value="yes"<?php if([B]$garageopt[/B]=='yes')echo 'checked="checked"';> /><strong><label for="yesop" class="yesGreen">Yes</label></strong>
 <input type="radio" id="pendingop" name="garageopt" value="pending" <?php if($garageopt=='pending')echo 'checked="checked"';?> /><strong><label for="pendingop" class="pendingAmbur">Pending</label></strong>
 <input type="radio" id="noop" name="garageopt" value="no" <?php if($garageopt=='no')echo 'checked="checked"';?> checked=checked/><strong><label for="noop" class="noRed">No</label></strong>

$garageopt is empty. You first need to set it thus:

$garageopt = $dataArow['garageopt'];

I came in to let you know i got it working and you post the same bit of code lol. Thanks for your time anyway.

<?php if ($dataArow['garageopt'] == "yes"){ echo "checked";}?> />
Member Avatar for diafol

I came in to let you know i got it working and you post the same bit of code lol. Thanks for your time anyway.

<?php if ($dataArow['garageopt'] == "yes"){ echo "checked";}?> />

This was the point I was trying to make:
If you do this:

$garageopt = $dataArow['garageopt'];

Then

<?php if ($dataArow['garageopt'] == "yes"){...}?>

is the same as:

<?php if ($garageopt == "yes"){...}?>

Just that the second is easier to write. Another thing: you're echoing "checked" only. To validate XHTML - you need to echo checked="checked" as all tag attributes values should be enclosed within quotes.

If your post is solved, please mark it so (you'll find a blue link on this page).

This was the point I was trying to make:
If you do this:

$garageopt = $dataArow['garageopt'];

Then

<?php if ($dataArow['garageopt'] == "yes"){...}?>

is the same as:

<?php if ($garageopt == "yes"){...}?>

Just that the second is easier to write. Another thing: you're echoing "checked" only. To validate XHTML - you need to echo checked="checked" as all tag attributes values should be enclosed within quotes.

If your post is solved, please mark it so (you'll find a blue link on this page).

When i do this

echo checked=="checked";

or

echo checked=='checked';

the radio button doesn't show what was selected.

when i do this

echo checked="checked";

i get "Parse error: syntax error, unexpected '=', expecting ',' or ';' on line 238
" only this works

<?php if ($dataArow['garageopt'] == "yes"){...}?>

Thanks anyway.

Member Avatar for diafol

Should be:

echo ' checked="checked"';

Should be:

echo ' checked="checked"';

Yeah that worked, thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.