View Full Version : Radio button auto fill
lansing
05-29-2006, 09:00 PM
I am not sure if I need to use JavaScript or use php or what to make this work. I have a Radio button group & 2 text input boxes below the radio buttons. I have information that needs to be placed in the text boxes stored in MySQL db.
In my DB I have TEAM 1's name & then TEAM 2's name already stored. The 2 radio buttons lets you choose either TEAM 1 or TEAM 2. I need the user to be able to click TEAM 2 & the 2 text boxes fill with TEAM 2's Name & if he chooses TEAM 1 then it fill with TEAM 2's name.
Thanks!
vinyl-junkie
05-29-2006, 10:18 PM
Here's the correct HTML for having a radio button selected. Just write your code so this portion of page is rendered this way.
<input type="radio" name="some_name" value="some_value" checked="checked" />
lansing
05-30-2006, 09:58 PM
I am not sure if you just didn't read my question or what so try reading it again. You didn't help at all. I know how to make it automatically checked when page loads. I need input boxes to populate with data from DB depending on what button is chosen
lavinpj1
05-30-2006, 10:12 PM
Ehrmmmm, as far as I know, without using ajax techniques, that is not possible. The only thing you can do, is to use php to assign javascript arrays on page load, and just use parts of those arrays in JS functions to fill the boxes.
~Phil~
Qwayzer
05-30-2006, 10:18 PM
Just modify this code: http://www.javascriptkit.com/script/script2/combodescrip1.shtml
And where it says descriptions[0]= just retrieve the info from the DB and put it there for each one.
degsy
05-31-2006, 03:12 PM
You need a simple script to query the database and output the results.
e.g.
domain.com/page.php?uid=25
uid = $_GET['uid'];
//do validation
$sql = "SELECT * FROM table WHERE id=" . uid;
http://computer-helpforum.com/php/codingforums/select_populate_form.php
That is done by submitting the form onchange. Using Ajax you could do it onclick without submitting/refreshing the page.
http://computer-helpforum.com/asp/ajax/populate_form2.asp
Same idea and same type of basic query, but used with Ajax
http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm
lavinpj1
05-31-2006, 03:41 PM
uid = $_GET['uid'];
//do validation
$sql = "SELECT * FROM table WHERE id=" . uid;
May wanna put some security on that. That is MySQL injection heaven...
uid = mysql_real_escape_string($_GET['uid']);
//do validation
$sql = "SELECT * FROM table WHERE id=" . uid;
lansing
05-31-2006, 10:18 PM
May wanna put some security on that. That is MySQL injection heaven...
uid = mysql_real_escape_string($_GET['uid']);
//do validation
$sql = "SELECT * FROM table WHERE id=" . uid;Should I use the mysql_real_escape_string() everytime I use the $_GET['uid']? What about when I am pulling the uid from cookie...should I use it then too?
lansing
05-31-2006, 11:00 PM
You need a simple script to query the database and output the results.
e.g.
domain.com/page.php?uid=25
uid = $_GET['uid'];
//do validation
$sql = "SELECT * FROM table WHERE id=" . uid;
http://computer-helpforum.com/php/codingforums/select_populate_form.php
That is done by submitting the form onchange. Using Ajax you could do it onclick without submitting/refreshing the page.
http://computer-helpforum.com/asp/ajax/populate_form2.asp
Same idea and same type of basic query, but used with Ajax
http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htmThanks for you help so farr...I know how to make a DB query. I need help with making the boxes populate with data when a user clicks on the button.
I can't find any coding for that middle option you have. Do you got a link to that code?
degsy
06-01-2006, 01:37 PM
uid = $_GET['uid'];
//do validation
May wanna put some security on that. That is MySQL injection heaven...
That's that the //do validation part is for :rolleyes:
degsy
06-01-2006, 02:22 PM
Thanks for you help so farr...I know how to make a DB query. I need help with making the boxes populate with data when a user clicks on the button.
I can't find any coding for that middle option you have. Do you got a link to that code?
If you can do a query, then can you output the result to a page?
If so then all you have to do is put the results in form elements instead of simply echoing them.
List the radio buttons. The value is the ID. The lable is the name.
$query_list_seminars = "SELECT id, attend FROM seminar";
$list_seminars = mysql_query($query_list_seminars, $CHF_PHP) or die(mysql_error());
$row_list_seminars = mysql_fetch_assoc($list_seminars);
<?php do { ?><label>
<input type="radio" name="seminar_id" value="<?php echo $row_list_seminars['id']; ?>"<?php if (!(strcmp($row_list_seminars['id'], $_POST['seminar_id']))) {echo ' checked="checked"';} ?> onclick="this.form.submit()" /> <?php echo $row_list_seminars['attend']; ?></label><br />
<?php } while ($row_list_seminars = mysql_fetch_assoc($list_seminars)); ?>
When you click a radio the value will be submitted as $_POST['radio_name']
Out put the details of the selected record.
$colname_seminar_details = "-1";
if (isset($_POST['seminar_id'])) {
$colname_seminar_details = (get_magic_quotes_gpc()) ? $_POST['seminar_id'] : addslashes($_POST['seminar_id']);
}
mysql_select_db($database_CHF_PHP, $CHF_PHP);
$query_seminar_details = sprintf("SELECT * FROM seminar WHERE id = %s", $colname_seminar_details);
$seminar_details = mysql_query($query_seminar_details, $CHF_PHP) or die(mysql_error());
$row_seminar_details = mysql_fetch_assoc($seminar_details);
$totalRows_seminar_details = mysql_num_rows($seminar_details);
<?php if ($totalRows_seminar_details > 0) { // Show if recordset not empty ?>
<table width="200" border="1" cellspacing="2" cellpadding="2">
<tr>
<td><div align="right">ID</div></td>
<td><?php echo $row_seminar_details['id']; ?></td>
</tr>
<tr>
<td><div align="right">Location</div></td>
<td><?php echo $row_seminar_details['location']; ?></td>
</tr>
<tr>
<td><div align="right">Leader</div></td>
<td><?php echo $row_seminar_details['leadership']; ?></td>
</tr>
<tr>
<td><div align="right">Date</div></td>
<td><?php if(isset($row_seminar_details['from'])) { echo date("l jS F Y", strtotime($row_seminar_details['from'])); } ?></td>
</tr>
</table>
<?php } // Show if recordset not empty ?>
http://computer-helpforum.com/php/codingforums/radio_populate_form.php
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.