Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP: Display and update radio btn values in MS Access

Status
Not open for further replies.

bigcat48

Programmer
Aug 27, 2008
72
US
All - I need to display "yes or no" selected values for a radio button option and have the ability to update an option back to the database.

Here is the code:

Code:
<?php 
		// create database connection
		include("includes/connection.php");
		
		$id= $_GET['id'];
		$query = "SELECT * FROM tblIndividuals WHERE id = $id";
		
		// query the database
		$queryID=odbc_exec($conn, $query);
		$response = odbc_result($queryID,"response");		
		odbc_close($conn);
		;?>


			<div class="row">
				<label>Response:</label>
					<?php 
					if ($response = "Yes")
						echo "<span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"Yes\" checked />Yes</span>
							  <span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"No\" />No</span>";
					elseif ($response = "No")
						echo "<span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"Yes\" />Yes</span>
							  <span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"No\" checked />No</span>";
					else
						echo "<span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"Yes\" />Yes</span>
							  <span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"No\" />No</span>";							  
					?>
			</div>
			
			<input type="hidden" name="id" value="<?php echo $id; ?>" />
			<input type="submit" id="submitbutton130" class="button positive" value="Edit Profile" />
		</form>

Could someone help?
 
Hate to be a pain but this is what I got.

The form name is the same as the valuefield name.

Code:
<?php
// create database connection
include("includes/connection.php");

$id = $_GET['id'];
$query = "UPDATE tblCompanies SET minorGroup = '" . $_GET['minorGroup'] . "' WHERE id = $id"; // Get lists of minor groups for the select box.

// query the database
$result=odbc_exec($conn, $query);
odbc_close($conn);

function getOptions($tblCompanies, $minorGroup, NULL, $minorGroup){
    global $conn; //the connection handle
    if (empty(NULL)) NULL = $minorGroup;
    $result = odbc_exec($conn, "Select $minorGroup as v, NULL as d from $tblCompanies");
    if (odbc_error($conn) == FALSE) { die (odbc_errormsg($conn));}
    $i = empty($_POST[$minorGroup]) ? NULL : trim ($_POST[$minorGroup]);
    $return = '';
    while ($row = odbc_fetch_array($result)){
        $selected = $i == $row['v'] ? 'selected="selected"' : '';
        $return .= "\t<option value=\"$row[v]\" $selected>$row[d]</option>\r\n";
    }
    return $return;
} 
?>
 
this
Code:
$query = "UPDATE tblCompanies SET minorGroup = '" . $_GET['minorGroup'] . "' WHERE id = $id"; // Get lists of minor groups for the select box.

cannot be right. this is an UPDATE statement. It does not return a value.

and you seem not to have grasped the concept of function arguments. you cannot have NULL as an argument name, nor can you use it as a variable.

My code is so simple it's difficult to understand why it is going wrong for you.

consider a select option
Code:
<option value="VALUE">TEXT</option>
that means that any database application that automatically generates the <options> must know the value of VALUE and of TEXT for each <option>.
If these are derived from a database, the builder also needs to know which table to query and which fields it should get the VALUE and TEXT values from.

you also asked for the control to be sticky. For that the function needs to know what incoming field to compare against.

It's that easy.

These are the arguments:
$table - the name of the table in which the data is stored
$valueField - the name of the field in the table in which the script should grab the VALUE values
$displayField - the name of the field for the TEXT values
$incomingField - the name of the select control to make sticky.

so you would use the code like this (for, say, a contacts form)

Code:
<select name="myselectBox">
<?php echo getOptions('myTable', 'userid', 'username', 'myselectBox'); ?>
</select>
 

Could someone tell me what's wrong with this.

What I am trying to do is select a chosen value from the database and also have the option to change the selection?

Here's the code.
Code:
<?php
include("includes/connection.php");
$id = $_GET['id'];
$sql="SELECT * FROM tblCompanies WHERE id=$id";

function getOptions(){
 $return = '';
 $rs=odbc_exec($conn,$sql);
	while ($row = odbc_fetch_array($rs)){
		foreach ($row as $item){
			$selected = (isset($_POST['minorGroup']) && $_POST['minorGroup'] == $item) ? 'selected="selected"' : '';
			$return .= "<option value=\"$item\" $selected>$item</option>";
		}
	return $return;
	}
}
echo getOptions();

Thanks in advance.
 
two things:

1. $sql is not available to the function. if you look at the code i have provided you the query is performed inside the function. move the $id and $sql lines within the function.

2. you are select *. i.e. all the fields in the table. this is really not going to work with the code that i have provided. the simple getOptions() function assumes that you have only ONE field being returned. so it should be SELECT SOMECOLUMN ...

the other code i provided is more flexible.
 

Ok. Now what's wrong?

The Select box will not load any data from my database table.
Why?

Code:
<?php
function getOptions('tblMinorGroup', 'ID', 'minorGroupName', 'minorGroup');{
	global $conn; //the connection handle
	$id = $_GET['ID'];
	$minorGroupName = $_GET['minorGroupName'];
	if (empty('minorGroupName')) 'minorGroupName' = 'ID';
	$result = odbc_exec($conn, "SELECT ID as v, minorGroupName as d FROM tblMinorGroup") or die (odbc_error());
	if (odbc_error($conn) == FALSE) { die (odbc_errormsg($conn));}
	$i = empty($_POST['minorGroup']) ? NULL : trim ($_POST['minorGroup']);
	$return = '';
	while ($row = odbc_fetch_array($result)){
		$selected = $i == $row['v'] ? 'selected="selected"' : '';
		$return .= "\t<option value=\"$row[v]\" $selected>$row[d]</option>\r\n";
	}
return $return;
}
echo getOptions('tblMinorGroup', 'ID', 'minorGroup', 'minorGroup');
?>

Thanks in advance
 
what's wrong is that you have not grasped the very basics of passing arguments to functions. go read up on this in the manual and then post back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top