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?
 
i don't quite understand why you have 6 radio buttons with alternating values of yes and no. they are all named the same. for yes-no values i would suggest that a checkbox is a better mechanism. and for the general structure instead of 3X 2 iterations you might think about this

Code:
$yes = $no = '';
if (strtolower($response) == "yes") { $yes == 'checked="checked"'; }
if (strtolower($response) == "no") { $no == 'checked="checked"'; }
echo <<<HTML
<span class="inline"><input type="radio" class="radio" name="response" value="Yes" $yes />Yes</span><span class="inline"><input type="radio" class="radio" name="response" value="No" $no />No</span>
HTML;


in any event the real issue, i suspect is that, in php, the equals sign is an assignment operator. the conditional
Code:
 if ($response = "Yes")
will ALWAYS evaluate to true. if you want to compare the values then you need to use == or ===.
 
jpadie - the update works when you select a radio option "Yes" or "No".

The problem is that it does not display the selected option first then allow you to change.

To test this I wrote:
Code:
<?php echo $response; ?>

This displayed the selected data in the database.

I need help on displaying the stored value and then have the option to change.

Thanks.
 
Wherever you're submitting this form should have something like
Code:
<?php 
        // create database connection
        include("includes/connection.php");
        
        $id= $_GET['id'];
        $query = "UPDATE tblIndividuals SET response = '" . $_GET['response'] . "' WHERE id = $id";
        
        // query the database
        $result=odbc_exec($conn, $query);    
        odbc_close($conn); 
?>

This is a bit oversimplified. You should probably escape the parameter to prevent SQL injection.

-----------------------------------------
I cannot be bought. Find leasing information at
 
sorry, my mistake change these lines as shown

Code:
if (strtolower($response) == "yes") { $yes = 'checked="checked"'; }
if (strtolower($response) == "no") { $no = 'checked="checked"'; }
 
This is what I have.

Code:
<?php
// create database connection
include("includes/connection.php");
			        
	$id= $_GET['id'];
	$query = "UPDATE tblIndividuals SET response = '" . $_GET['response'] . "' WHERE id = $id";
			        
// query the database
$result=odbc_exec($conn, $query);    
odbc_close($conn); 
					
$yes = $no = '';
if (strtolower($response) == "Yes") { $yes == 'checked="checked"'; }
if (strtolower($response) == "No") { $no == 'checked="checked"'; }
echo "<span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"Yes\" $yes />Yes</span>
<span class=\"inline\"><input type=\"radio\" class=\"radio\" name=\"response\" value=\"No\" $no />No</span>";
?>

This only works for updating the database. It still will not display what has been selected and then have the ability to make a different selection.
 
Thats true. Do you understand what the code you've written does? You're doing a lowercase function on a string, then comparing it to a mixed case sting. That'll never be equal. If you want to update and view from the same page, you'll need some amount of logic to see if you should try to update. You'll also need to add in the SELECT to get the current response value if its not an update. These are some very basic concepts. If you don't understand them, you should take the time to learn before you try to use them. I don't mean to be rude, but please at least try to understand what is happening here. Learn what each function does before you use it.

-----------------------------------------
I cannot be bought. Find leasing information at
 
jpadie - that worked.

I assume that the same can be done with a drop select box as well.

Meaning, display selected option with the ability to update.
 
yes. except you echo 'selected="selected" instead of the checked alternative.
 
Somehow not working. Here is the code.

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

$id= $_GET['id'];
$query = "UPDATE tblIndividuals SET companyName = '" . $_GET['companyName'] . "' WHERE id = $id";

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

$yes = $no = '';
if (strtolower($companyName) == "yes") { $yes = 'selected="selected"'; }
if (strtolower($companyName) == "no") { $no = 'selected="selected"'; }
	echo "<option selected value=\"$yes\">$yes</option>";
?>


 
right technique, wrong implementation. the code you've posted doesn't really appertain to a select box type control so here is another example.

Code:
<?php 
function getOptions(){
 $array  = array("oranges", "lemons", "kiwi", "guava", "Mango");
 $return = '';
 for each ($array as $item){
  $selected = (isset($_POST['selectBox']) && $_POST['selectBox'] == $item) ? 'selected="selected"' : '';
  $return .= "<option value=\"$item\" $selected>$item</option>";
 }
 return $return;
}
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="selectBox">
<?php echo getOptions(); ?>
</select>
<input type="submit" name="submit" value="submit" />
</form>
 
jpadie - this works like a charm, but how can I populate the array with values from my database?
 
Just sue the loop from your database instead of the foreach
array.

Code:
function getOptions(){
 $return = '';
mysql_query(...);
while ($row=mysql_fetch_array($results)){
....
}


This is all very basic stuff, perhaps reading some tutorials, or books may help you understand this better.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
something like this I guess
the variables are
the name of the database table
the column name for the value
the column name for the display text (or set to NULL if the same as the value)
the name of the form field to check for stickiness

Code:
<?php
function getOptions($table, $valueField, $displayField, $incomingField){
	if (empty($displayField)) $displayField = $valueField;
	$result = mysql_query ("Select $valueField as v, $displayField as d from $table") or die (mysql_error());
	$i = empty($_POST[$incomingField]) ? NULL : trim ($_POST[$incomingField]);
	$return = '';
	while ($row = mysql_fetch_assoc($result)){
		$selected = $i == $row['v'] ? 'selected="selected"' : '';
		$return .= "\t<option value=\"$row[v]\" $selected>$row[d]</option>\r\n";
	}
	return $return;
}
?>
 
All - I tried the update from 21-Nov by "jpadie" and it worked by allowing a user to select a different option.

The problem is when you go back to the update page to change the selection, it does not show the item previously selected.

Also I would like to be able to populate the array with values from my database table and don't know how. I tried for a total of about 15 hours and I am still confused.

I understand foreach and while loops better, but I still need help. This web application that I'm building would probably be close to finished besides the fact that my requirements are to use php with MS Accees database.

My goal for certain functions within this application is to be able to display selected values and have the ability to update values from a checkbox and select box. The radio option works just fine.

Here is the code to the form page and the include page:
Code:
==FORM==
<form id="ContactForm" action="edit_company-test.php" method="post">
	<div class="row">
		<label>Company Name:</label>
			<span><input type="text" class="text" name="companyName" value="<?php echo $companyName; ?>" />
			<label>Company Name</label></span>
	</div>
	
	<div class="row">
		<label>NS Information Detail:</label>
			<span><select name="minorGroup">
				[COLOR=red]<?php include("includes/dsp_minorGroupUpdate.php"); ?>[/color]						
			</select><label>Minor Group</label></span>
	</div>
	
	<input type="hidden" name="id" value="<?php echo $id; ?>" />
	<input type="submit" id="submitbutton130" class="button positive" value="Edit Profile" />
</form>


==INCLUDE "dsp_minorGroupUpdate.php"==
<?php
// create database connection
include("includes/connection.php");

$id = $_GET['id'];

// Get lists of minor groups for the select box.
$sql="SELECT * FROM tblminorGroup ORDER BY tblminorGroup.minorGroup ASC";


function getOptions(){
 $array  = array("oranges", "lemons", "kiwi", "guava", "Mango");
 $return = '';
 foreach ($array as $item){
  $selected = (isset($_POST['minorGroup']) && $_POST['minorGroup'] == $item) ? 'selected="selected"' : '';
  $return .= "<option value=\"$item\" $selected>$item</option>";
 }
 return $return;
}

echo getOptions();
?>

Please help deadline soon.
 
did you try my code?

to make a selection sticky beyond a form refresh use sessions.
 

jpadie - I made an attempt using your code. The difficulty is updating your code to work with PHP, MS Access via odbc.
 
why? two minutes with the manual would have sorted you out. i've never used odbc before but something like this looks right according to the manual.

Code:
<?php
function getOptions($table, $valueField, $displayField, $incomingField){
    global $conn; //the connection handle
    if (empty($displayField)) $displayField = $valueField;
    $result = odbc_exec ($conn, "Select $valueField as v, $displayField as d from $table") or die (mysql_error());
    if (odbc_error($conn) == FALSE) { die (odbc_errormsg($conn));}
    $i = empty($_POST[$incomingField]) ? NULL : trim ($_POST[$incomingField]);
    $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;
}
?>
 

Would I set my variables like this below?
$table = someTableName;
$valuefield = $_GET['fieldname'];
$displayfield = $_GET['someotherfieldname'];
$incomingfield = $_GET['anotherfieldname'];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top