PDA

View Full Version : Help with a radio button


MJS
01-12-2008, 07:20 PM
Hi
I have a set of radio buttons that you populate one at a time then submit is there away of keeping them checked till the end of the session.

MJS

davidj
01-12-2008, 09:26 PM
need more info

senario etc

MJS
01-12-2008, 09:50 PM
Hi David
I have a page with that had radio buttons but changed them for checkbox there are 11 boxes each box has 3 checkboxs to make easy for you this is a screen grab of the page.
and here is part of the code
<input name="available" type="checkbox" id="available" value="1" <?php if(!(strcmp($_POST['available'],1))){echo "checked";}?>/>
Yes
<input name="available" type="checkbox" id="available" value="2" <?php if(!(strcmp($_POST['available'],2))){echo "checked";}?>/>
No
<input name="available" type="checkbox" id="available" value="3" <?php if(!(strcmp($_POST['available'],3))){echo "checked";}?>/>
Maybe

<label><span class="style9">
<input type="submit" name="wat1" value="Submit" />
<input name="eventID" type="hidden" id="eventID" value="1" />
</span> </label>

the tick will stay in the box till i go to the next box then it come unticked I was trying to get it to work so all event could be ticked one in each box.

davidj
01-13-2008, 08:13 AM
your trying to make check boxes behave like radio buttons???


why?
will have to be done using javascript
please confirm this is what your wanting

MJS
01-13-2008, 09:37 AM
Lets start again bad idea changing radio buttons to checkboxs gone back to radio buttons. I have a page with radio buttons when someone log in and set his event to yes or no or maybe for one of the events I was tyring to get it to keep the button highlighted till they had finish the page. so if you put a dot in yes on the first event and submitted then go to the second event and put a dot in no and submitted and so on, so when you have finished the page you could see what you have done and could make a change if needed.

davidj
01-14-2008, 06:44 AM
what values are you putting in the db for these radio buttons? (1, yes, true)

MJS
01-14-2008, 07:10 PM
yes: 1 = yes: 2 = no: 3 = maybe: it shows on the db as yes,no,maybe.
do you remember you help me to set it up and get it working.

davidj
01-14-2008, 08:36 PM
just use the same strcmp function to dynamicaly set the radio buttons

<?php if(!(strcmp($db_var,'yes'))){echo "checked";}?>

you need 3 of them for each radio button

$db_var could be the $row['var'] variable from the database

yes value is what your trying to compare to the $db_var

so you need a value for each radio button....

hope this makes sense

MJS
01-17-2008, 09:20 PM
can you explain how this works more, I have tried to get it to work but with no success, I can get it to keep the maybe button checked all the time but have got stuck.

domedia
01-17-2008, 10:02 PM
That's the nature of radio buttons. They are exclusive. Like DJ said, of you have a form where more than one option can be selected, use checkboxes.

davidj
01-18-2008, 06:35 AM
let me see your code

where you have your radio buttons

MJS
01-19-2008, 09:58 PM
this is for the first set of buttons
<th width="337" class="mistake" scope="col"> <form id= "form1" name= "form1" method= "post" action= "<?php $_SERVER['PHP_SELF']; ?>" >
<div align="center"><span class="style9">
<label> </label>
</span>
<label>
<span class="text">
<input name="available" type="radio" id= "available1" value="1" />
Yes</span></label>
<span class="text">
<label>
<input name="available" type="radio" id= "available2" value="2" />
No</label>
<label>
<input name="available" type="radio" id= "available3" value="3" />
Maybe</label>
</span>
<label><span class="style9">
<input type="submit" name="wat1" value="Submit" />
<input name="eventID" type="hidden" id="eventID" value="1" />

would it be easer to use checkbox and then submitt at the end..

davidj
01-19-2008, 10:03 PM
<input name="available" type="radio" id= "available1" value="1" <?php if(!(strcmp($row['db_value'], 1))){echo "selected";} ?> />

<input name="available" type="radio" id= "available2" value="2" <?php if(!(strcmp($row['db_value'], 2))){echo "selected";} ?> />

<input name="available" type="radio" id= "available3" value="3" <?php if(!(strcmp($row['db_value'], 3))){echo "selected";} ?> />



look at how im comparing them by value

MJS
01-20-2008, 02:45 PM
got it to put selected after the yes,no,maybe but when I make the next selection the first one disappears is there away of keeping the selectiion till you refresh the page, this is the code I have used.

<input name="available" type="radio" id= "available1" value="1" />Yes <?php if(!(strcmp($available['wat1'], 1))){echo "selected";}?> <input name="available" type="radio" id= "available2" value="2" />No <?php if(!(strcmp($available['wat1'], 2))){echo "selected";}?>
<input name="available" type="radio" id= "available3" value="3" />Maybe <?php if(!(strcmp($available['wat1'], 3))){echo "selected";}?>

davidj
01-20-2008, 05:54 PM
replace $available['wat1'] with the $_POST var which catches the value previously. This will retain the setting from the previous selection

MJS
01-21-2008, 04:46 PM
if I am reading this right to replace $available['wat1'] with the $_POST var which catches the value previously.
the $_POST var is $available = $_POST['available'];
so putting it as <?php if(!(strcmp(available , 1))){echo "selected";}?> is this right ? but when I do this it does not work

davidj
01-21-2008, 05:58 PM
have you forgot to add the $ to the available variable????

ya have to be like a fighter pilot and have a keen eye

MJS
01-21-2008, 06:53 PM
OK that works I got it to work with that but when I go to the next selection the first one you have selected the selected go off how can I keep the first one and so on selected till you have finished the page thats eleven selections.
the code looks like this now
<input name="available" type="radio" id= "available1" value="1" />Yes <?php if(!(strcmp($available , 1))){echo "selected";}?>

davidj
01-21-2008, 07:04 PM
this is not how i wrote it!

it is wrong


<input name="available" type="radio" id= "available1" value="1" />Yes <?php if(!(strcmp($available , 1))){echo "selected";}?>


you have to understand this. its pointless just copying (would be better if you did copy)

this is how it should be written...


<input name="available" type="radio" id= "available1" value="1" <?php if(!(strcmp($available , 1))){echo "selected";}?> />

all your example would do is echo the string "selected" as a label????

MJS
01-21-2008, 08:15 PM
Ok David I can see that and yes that what is happening but writing it as you have it, I do not get the word selected OK but also the radio button does not stay on when submitted.

davidj
01-21-2008, 08:54 PM
do you have skype installed

MJS
01-22-2008, 04:02 PM
yes see private message

MJS
01-24-2008, 11:30 AM
Hi David have put the code as you put it and still it will not work right the radio button will not stay on when it is submitted..

davidj
01-24-2008, 02:16 PM
post code please

MJS
01-24-2008, 05:29 PM
this is the code for the first radio button

<th width="210" height="30" class="text" scope="col"><div align="center"><span class="style18">WATFORD
HALF</span> <span class="style9">03/02/2008</span> </div></th>
<th width="337" class="mistake"> <form id= "form1" name= "form1" method= "post" action= "<?php $_SERVER['PHP_SELF']; ?>">
<div align="center"><span class="style9">
<label></label>
</span>
<label>
<span class="text">
<input name="available" type="radio" id= "available1" value="1" <?php if(!(strcmp($available , 1))){echo "selected";}?>/>Yes
<input name="available" type="radio" id= "available2" value="2" <?php if(!(strcmp($available , 2))){echo "selected";}?>/>No
<input name="available" type="radio" id= "available3" value="3" <?php if(!(strcmp($available , 3))){echo "selected";}?>/>Maybe
</span>
<input type="submit" name="wat1" value="Submit" />
<input name="eventID" type="hidden" id="eventID" value="1" />
</span> </label>
</div>
</form></th>

davidj
01-24-2008, 05:34 PM
try the following

remove this...

<?php $_SERVER['PHP_SELF']; ?>

and replace with the actual page name

mypage.php

MJS
01-26-2008, 07:24 PM
No good still will not work would it be any good if you had a look at the full page.

davidj
01-26-2008, 08:17 PM
post code please

MJS
01-30-2008, 03:34 PM
Davidj did you receive my Skye file i sent you and if so have you had time to have a look at it .

davidj
01-30-2008, 03:41 PM
checking it now

davidj
01-30-2008, 04:40 PM
when you press the submit the values are stored in the db

you need a select statement in the page which queries the table

then you need to set a strcmp function to set the radio buttons

write your select statement and come back if you need guidance

MJS
01-31-2008, 07:58 PM
is this right
$query = sprintf ("SELECT * FROM events WHERE eventID = '2' ");
$result = @mysql_query ($query);
$rowReturn = mysql_fetch_array ($result);

davidj
02-01-2008, 07:41 AM
this isnt working

we arnt getting anywhere with this

did someone help you write what you have already?

be honest as this gives me a better understanding of the level your at as i may be teaching at the wrong level

MJS
02-01-2008, 10:33 AM
No I wrote that page myself with your help, but I have not done any more since and I just could not get my head round it. when you echo it to the page it give you a return of what was submitted to the db, if you select yes and submit it echoes yes then submit no it still echoes yes then you submit maybe and it echoes no and so on so it's showing what was on the db, I can't work out why this works like that.

davidj
02-01-2008, 11:01 AM
all you need to do is query the db when the page loads in order to read back the results that were selected previously

in your db you will have references to the radio button data which was inserted

you need to access this information using a select

you then check the info against the row and then you will be able to identify which redio needs selecting

MJS
02-01-2008, 10:18 PM
How is this
$query = sprintf("SELECT wat1,chi2,kee3,wat4,bor5,bus6,mar7,big8,lin9,pea10 ,tri11 FROM events WHERE eventID = 2");
$result = @mysql_query($query);
$tvd = mysql_fetch_array($result);

MJS
02-02-2008, 02:04 PM
This code will print the results of the DB for wat1 to the page

$query = sprintf("SELECT wat1,chi2,kee3,wat4,bor5,bus6,mar7,big8,lin9,pea10 ,tri11 FROM events WHERE eventID = 2");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
echo $row ['wat1'] ;

davidj
02-02-2008, 02:26 PM
and $row ['wat1'] has 3 options yes

or could be one of three flags

MJS
02-02-2008, 07:41 PM
that right Maybe No Yes, so if I can get it to print to the page the results from the DB I should be able to put into the strcmp ?

davidj
02-03-2008, 12:38 AM
ok

in the option tag

see if you can understand this...

<option value="yes" <?php if(!(strcmp($row ['wat1'], 'yes' ))){echo "selected";} ?>>yes</option>

MJS
02-04-2008, 05:39 PM
OK David I have not use the option value, but I can see that if you have a option value of yes which is grabbed by the $row from the DB then you can use it in some way. I have looked for a explanation of this and found it not to helpful it seems to work with the selected statement.

davidj
02-04-2008, 05:44 PM
sorry mate

i lost the objective there for abit

just add the same code but inside the radio button tag just like i did with the option tag

MJS
02-04-2008, 06:22 PM
So are saying put this code <?php if(!(strcmp($row ['wat1'], 'yes' ))){echo "selected";} ?>/>yes
inside the radio button tag as before and it should switch on the button that is held by the DB. but it wont work i can echo it to the page and see that it is picking up the right info.
could you explain the "selected" part and how this populated.

davidj
02-04-2008, 07:31 PM
when you use strcmp (string compare) you are comparing string values

$row ['wat1'], <-- and --> 'yes'

when those values match you are echoing "selected" which is the switch to select the radio button to on

with $row ['wat1'] being the value of the db and yes being the value of the radio button

MJS
02-05-2008, 10:46 AM
Ok thanks for that I thought it was that but was not sure so this should work then.
<input name="available" type="radio" id= "available1" value="1" <?php if(!(strcmp($row ['wat1'], 'Yes' ))){echo "selected";} ?>/>yes

$row getting the result from wat1 and in this case it yes this should select the radio button to on. But it does not work. as I have said it will echo it to the page with the result of $row ['wat1'] in this case Yes.

So have I got the query statement in the right place can it go any ware on the page with in the <?PHP ?> brackets.

davidj
02-05-2008, 10:50 AM
<input name="available" type="radio" id= "available1" value="1" <?php if(!(strcmp($row ['wat1'], 'Yes' ))){echo "selected";} ?>/>yes


how will that work when you have the value of that radio button set to 1 so this tells me you have 1 going into your db yet you are performing a strcmp on the db value (which is 1 as above) and the string 'yes'

This comparison check will always be false if you have the above in your code

MJS
02-05-2008, 04:57 PM
Ok so I changed the value of the radio button to Yes and checked that it works sending the right info to the DB and that it returns it to the echo statment on the page that works. the new code

<input name="available" type="radio" id= "available1" value="Yes" <?php if(!(strcmp($row ['wat1'], 'Yes' ))){echo "selected";} ?>/>Yes

I had to change this part of the code from.
switch($available){
case '1' : $event = "Yes"; break;
case '2' : $event = "No"; break;
case '3' : $event = "Maybe"; break;
to this
switch($available){
case 'Yes' : $event = "Yes"; break;
case 'No' : $event = "No"; break;
case 'Maybe' : $event = "Maybe"; break;

So by doing this it should work but it still will not run echo selected it wont work.

davidj
02-05-2008, 05:51 PM
i dont know where your going wrong with this

i use this method all the time

davidj
02-05-2008, 05:53 PM
your going to have to debug this

check the value of the db and that of the db var then look at the source to see if the selected is returning

davidj
02-05-2008, 06:03 PM
i suggest you start with a blank page

create a recordset querying a table with values 1,2,3

then include my example of strcmp in a set of radio buttons which updates the db

once you get it working you can then port the example across

MJS
02-05-2008, 09:00 PM
thanks David will give that a go and get back to you.

MJS
02-06-2008, 05:48 PM
I have had a go at debugging this and can not fine why it will not work.
I have just had a blank page with just the code to login to the DB and a row of buttons still no joy if i add a echo to the page it shows the info thats on the DB ( Yes No Maybe ) which ever is on the DB with this code . $query = sprintf("SELECT * FROM events WHERE user_name = '$id' ");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
echo $row['wat1'];
and thyat returns the right info.
Now if I try to get the info with this which send it to the DB
switch($available){
case 'Yes' : $event = "Yes"; break;
case 'No' : $event = "No"; break;
case 'Maybe' : $event = "Maybe"; break;
}
if($eventID){
switch($eventID){
case '1': $field = "wat1"; $avail = $available; break;

}
////////////////////////////
echo $event ['wat1'];
it just returns a Y N M which ever is selected and I can not find out why this is happerning.

davidj
02-06-2008, 07:43 PM
ok

i have written an example

here is the db Script


# SQL Manager 2007 for MySQL 4.1.2.1
# ---------------------------------------
# Host : localhost
# Port : 3306
# Database : new_db

SET FOREIGN_KEY_CHECKS=0;
DROP DATABASE IF EXISTS `new_db`;
CREATE DATABASE `new_db`
CHARACTER SET 'latin1'
COLLATE 'latin1_swedish_ci';
USE `new_db`;
#
# Structure for the `table1` table :
#
CREATE TABLE `table1` (
`field_id` varchar(40) NOT NULL,
`choice` int(11) default NULL,
PRIMARY KEY (`field_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#
# Data for the `table1` table (LIMIT 0,500)
#
INSERT INTO `table1` (`field_id`, `choice`) VALUES
('field1',1),
('field2',2),
('field3',3);
COMMIT;


now the PHP


<?php
/// connection ///////////////////////////
//////////////////////////////////////////
$hostname = "localhost";
$username = "username"; // << change this to suit
$password = "password"; // << change this to suit
$database = "new_db";
$connect = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
$dbase = mysql_select_db($database, $connect);
//////////////////////////////////////////

/// query ////////////////////////////////
$Query = "select * from table1";
$Recordset = mysql_query($Query, $connect) or die(mysql_error());
$Row = mysql_fetch_array($Recordset);
//////////////////////////////////////////

//////////////////////////////////////////
/// start loop
do{

/// assign Row to array
$result[] = $Row['choice'];

}while($Row = mysql_fetch_array($Recordset));
//////////////////////////////////////////
//////////////////////////////////////////

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table width="444" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle">row1</td>
<td align="center" valign="middle"><input name="radio1" type="radio" id="radio" value="1" <?php if(!(strcmp($result[0], 1))){echo "checked"; } ?> /></td>
<td align="center" valign="middle"><input type="radio" name="radio1" id="radio" value="2" <?php if(!(strcmp($result[0], 2))){echo "checked"; } ?> /></td>
<td align="center" valign="middle"><input type="radio" name="radio1" id="radio" value="3" <?php if(!(strcmp($result[0], 3))){echo "checked"; } ?>/></td>
</tr>
<tr>
<td align="center" valign="middle">row2</td>
<td align="center" valign="middle"><input type="radio" name="radio2" id="radio5" value="1" <?php if(!(strcmp($result[1], 1))){echo "checked"; } ?>/></td>
<td align="center" valign="middle"><input type="radio" name="radio2" id="radio6" value="2" <?php if(!(strcmp($result[1], 2))){echo "checked"; } ?>/></td>
<td align="center" valign="middle"><input type="radio" name="radio2" id="radio7" value="3" <?php if(!(strcmp($result[1], 3))){echo "checked"; } ?>/></td>
</tr>
<tr>
<td align="center" valign="middle">row3</td>
<td align="center" valign="middle"><input type="radio" name="radio3" id="radio9" value="1" <?php if(!(strcmp($result[2], 1))){echo "checked"; } ?>/></td>
<td align="center" valign="middle"><input type="radio" name="radio3" id="radio10" value="2" <?php if(!(strcmp($result[2], 2))){echo "checked"; } ?>/></td>
<td align="center" valign="middle"><input type="radio" name="radio3" id="radio11" value="3" <?php if(!(strcmp($result[2], 3))){echo "checked"; } ?>/></td>
</tr>
</table>
</body>
</html>


try changing the value in the db from the choices field from 1,2 3

let me know how you get on

MJS
02-07-2008, 12:14 PM
Thank you David
run your page all worked fine had a good look at the code and then at my code and come up with one error and this was it <?php if(!(strcmp($row ['wat1'],' Yes' ))){echo "selectd";} can you spot it one space in the wrong place I have change it to this <?php if(!(strcmp($row ['wat1'],'Yes' ))){echo "checked";} ?> also changed the selected to checked and all is fine
Thanks agian for all your help.

davidj
02-08-2008, 04:37 PM
i think it could have been my fault

i gave you this


echo "selectd";


but should of been this


echo "checked";

MJS
02-09-2008, 07:32 PM
OK David It works fine now.
One thing you may be able to help with is that when you use that page in Mozilla Firefox the radio buttons do not work you can not select any thing.
It works OK in all the other web browsers.

davidj
02-10-2008, 10:55 AM
post the code what you have

the entire page

MJS
02-10-2008, 04:13 PM
You have the page it was the one I sent you on Skype.

davidj
02-10-2008, 05:51 PM
you have changed it since you sent it yes?

so what would be the point of debuging a script you sent me before you added the changes??

MJS
02-10-2008, 07:59 PM
Are well it did the same thing before the changes.

davidj
02-10-2008, 08:03 PM
did you terminate your radio buttons

MJS
02-10-2008, 09:26 PM
I am not sure how to terminate radio button.

davidj
02-11-2008, 06:46 AM
if your using xhtml then at the end of the tag (before the closing > angle bracket) add /


<input name="radio1" type="radio" id="radio" value="1" />

MJS
02-11-2008, 10:53 AM
this is how it is on the page.
< input name= "available" type= "radio" id= "available1" value= "1" <?php if(!(strcmp($row ['wat1'],'Yes' ))){echo "checked" ;} ?> />Yes

davidj
02-11-2008, 10:56 AM
ok

if you view source through firefox paste what you have there

im trying to troubleshoot why its not working in FF

davidj
02-11-2008, 10:58 AM
also make sure your name attribute is the same as the ID attribute on each button

MJS
02-11-2008, 12:00 PM
here is the first two events from FF

<th width="210" height="30" class="text" scope="col"><div align="center"><span class="style18">WATFORD HALF</span> <span class="style9">03/02/2008</span> </div></th> <th width="337" class="mistake">
<form id= "form1" name= "form1" method= "post" action= > <div align="center"><span class="style9"> <label></label> </span> <label> <span class="text">
<input name="available" type="radio" id= "available1" value="1" checked/> Yes
<input name="available" type="radio" id= "available2" value="2" /> No
<input name="available" type="radio" id= "available3" value="3" /> Maybe
<input type="submit" name="wat1" value="Submit"/>
<input name="eventID" type="hidden" id="eventID" value="1"/>
</span> </label> </div> </form></th> </tr> <tr>
<th height="30" class="text" scope="row"><div align="center"><span class="style18">CHILTERN CHALLENGE</span> <span class="style9">23/24/2/2008</span></div></th> <td class="mistake">
<form id="form2" name="form2" method="post" action= > <div align="center"><span class="style9"> <label></label> </span> <label> <span class="text">
<input name="available2" type="radio" id="available1" value="1" /> Yes
<input name="available2" type="radio" id="available2" value="2" checked /> No
<input name="available2" type="radio" id="available3" value="3" /> Maybe
<input type="submit" name="chi2" value="Submit" /> </span> <span class="style9">
<input name="eventID" type="hidden" id="eventID" value="2" />
</span> </label> </div> </form></td> </tr>

davidj
02-11-2008, 01:35 PM
you have an action attribute in your form tag without any quotes!

davidj
02-11-2008, 01:38 PM
also

stuff like this...

<span class="style9"><label></label></span>

Is bad programming

the symptom of using dreamweavers drag-n-drop features but not checking afterwards. This kind of oversite can lead to problems later on

always validate code

you are coding at a level where this should not happen.

davidj
02-11-2008, 01:46 PM
i have also noticed that you have a form tag wrapped round each row

would one form not work?

MJS
02-13-2008, 09:18 PM
I have tried to get just one form to work on this page but I am having trouble with it updating the DB it wont. can I have ten submit buttons in one from or is this a problem it will work with just one with just one event.

davidj
02-14-2008, 06:50 AM
sorry again mate

my fault again

yes you can use a seperate form on each row but you have to name them uniquely so you can identify which one to post

MJS
02-14-2008, 08:05 PM
OK David thats alright the page needed a tidy up in the code department and a make over, so all is well and it works in firefox ok.

davidj
02-14-2008, 08:09 PM
have you sorted it?

is it resolved?

MJS
02-14-2008, 08:35 PM
Yes all is fine and working ok thanks David.
Till the next time.