Page 1 of 2

Error on page

Posted: Fri Oct 01, 2010 7:19 am
by alexia_net
Hi. I have created a small form with a radio button, textbox and a list on it. When I click on the radio button I want the list to be populated with some records from a database. I do not know what is the problem with my code and I hope someone can help me. I AM VERY NEW WITH PHP.

<!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>

<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/ITA/DB/OpenDB.php');

function LoadBuildings(){
$strSQL = "SELECT internalCodeBuilding FROM building";
$result=mysql_query($strSQL) or die("Unable to select the information!");
echo'<select name="lstBuilding"><option value=""></option>';

while($rand=mysql_fetch_array($result)){
echo '<option value='.$rand['internalCodeBuilding'].'</option>';
}
echo'</select>';
}

echo '
<table width="80%" border="1">
<tr>
<td><p>
<label>
<input type="radio" name="RadioGroup1" value="building" id="RadioGroup1_0" onclick="LoadBuildings()";/>
Building</label>
<br />
</p></td>
<td>Building name:
<label>
<input type="text" name="txtBuildingName" id="txtBuildingName" />
</label></td>
<td>Select building:
<label>
<select name="lstBuilding" id="lstBuilding">
</select>
</label></td>
</tr>
</table>
';
?>

</body>
</html>

Thank you.

Re: Error on page

Posted: Fri Oct 01, 2010 8:14 am
by social_experiment

Code: Select all

<input type="radio" name="RadioGroup1" value="building" id="RadioGroup1_0" onclick="LoadBuildings()";/>
I don't think you can call a function (php) like a javascript function. [not 100% sure though].

Re: Error on page

Posted: Fri Oct 01, 2010 8:17 am
by alexia_net
Do you have any suggestion? Thx.

Re: Error on page

Posted: Fri Oct 01, 2010 8:25 am
by DigitalMind
Read simple tutorial at least.
http://www.php.net/manual/en/tutorial.php

Re: Error on page

Posted: Fri Oct 01, 2010 8:39 am
by alexia_net
Hi. I have read many tutorials, but they do not talk about specific things. I my case I want to populate a list by clicking on a radio button. How could I achieve this? Thank you.

Re: Error on page

Posted: Fri Oct 01, 2010 6:11 pm
by social_experiment
@ alexia_net : You have the right idea with your SQL code. You are using php which is server-side script so the page needs to submitted for the list to be created. Your radio button should contain a value that is unique to the values you wish to display from the database. If you have new code, submit that so we can teach you to fish instead of spoon feeding ;)

Re: Error on page

Posted: Fri Oct 01, 2010 6:35 pm
by DigitalMind
... and don't forget to forget to press

Code: Select all

   :)

Re: Error on page

Posted: Mon Oct 04, 2010 1:07 am
by alexia_net
Hi. This is all the code i have. I am stuck here. Thx.

Re: Error on page

Posted: Mon Oct 04, 2010 3:57 am
by social_experiment

Code: Select all

<input type="radio" name="RadioGroup1" value="building" id="RadioGroup1_0" onclick="LoadBuildings()";/>
The radio button in this instance is redundant because it allows the user only 1 option to choose from. If you only want a user to view one group of records from the database, hard code the value into the query you send to the database.

If you want a user to pick an option, using multiple radio buttons, your code could follow this pseudo code :

Code: Select all

 // form with radio buttons go here, 

 // check whether the button has been clicked 
 // and a option has been selected from the radio
 // buttons

 // run the function that displays the form depending
 // on the results of the radio button
I also re-wrote the function for you, it's a suggestion of how you could make the function a bit more functional, this would display the form containing the select element. If you are going to use radio buttons for the user to select a value, change the query ($strSQL) accordingly. Also note the comments i added :)

Code: Select all

<?php
function display_postArray() {
		$strSQL = "SELECT internalCodeBuilding FROM building";
		// instead of having an error echo inside the function, check
		// elsewhere that a connection to the database has been made,
		// like in the external file making the database connection
		$result = @mysql_query($strSQL);
		// if the result is positive, start echoing to the browser.
		// IMO i would place the whole form inside the if statement so that
		// you don't end up with incomplete / invalid code should the
		// value of '$result' be false for whatever reason
		if ($result) {
			echo '<select name="lstBuilding"><option value=""></option>';
			//
			while ($rand = @mysql_fetch_array($result)) {
				echo '<option value="'. stripslashes($rand['internalCodeBuilding']) .
				'">'. stripslashes($rand['internalCodeBuilding']) .'</option>';
			}
			//
			echo '</select>';
		}
	} ?>

Re: Error on page

Posted: Mon Oct 04, 2010 6:06 am
by alexia_net
Thank you for your solution. My main problem is that I do not know how to bind the code to the radio button. I have been told that onclick event is just for JavaScript. I will try again and let you know. Thank you again.

Re: Error on page

Posted: Mon Oct 04, 2010 6:34 am
by alexia_net
Hi. the solution works fine, thank you. But how do I bind it to a radio button. How do I bind a piece of code to a control on the the form: radio button, group of radio buttons, drop down list, etc. This is my main problem now. I hope you people can en-light me. Thank you.

Re: Error on page

Posted: Mon Oct 04, 2010 6:49 am
by alexia_net
Hi. I think that the code should look like this

Code: Select all

<!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>

<?php 
require_once($_SERVER["DOCUMENT_ROOT"].'/ITA/DB/OpenDB.php');


function display_postArray() {
                $strSQL = "SELECT internalCodeBuilding FROM building";
                // instead of having an error echo inside the function, check
                // elsewhere that a connection to the database has been made,
                // like in the external file making the database connection
                $result = mysql_query($strSQL);
                // if the result is positive, start echoing to the browser.
                // IMO i would place the whole form inside the if statement so that
                // you don't end up with incomplete / invalid code should the
                // value of '$result' be false for whatever reason
                if ($result) {
                echo '<form name="PopulateBuildingsList">';        
                echo '<select name="lstBuilding"><option value=""></option>';
                        //
                        while ($rand = mysql_fetch_array($result)) {
                                echo '<option value="'. stripslashes($rand['internalCodeBuilding']).
                                '">'. stripslashes($rand['internalCodeBuilding']) .'</option>';
                        }
                        //
                        echo '</select>';
                echo'</form>';
                } 
}     


echo '
<form id="form1" name="form1" method="post" action="">
  <label></label>
  <p>&nbsp;</p>
  <table width="80%" border="1">
    <tr>
      <td width="22%"><input type="radio" name="radio" id="rdBuilding" value="rdBuilding" />
Building</td>
      <td width="20%"><label>
        <input type="text" name="txtBuilding" id="txtBuilding" />
      </label></td>
      <td width="58%"><label>
        <select name="lstBuiding" id="lstBuiding">
        </select>
      </label></td>
    </tr>
  </table>
  <p>&nbsp;</p>
</form>
';

if (isset($_POST['radio'])){
display_postArray();
}

?>

</body>
</html>

However, i think that the code
if (isset($_POST['radio'])){
display_postArray();
}
executes to quickly and nothing happens. How could i bypass this? Thank you.

Re: Error on page

Posted: Mon Oct 04, 2010 7:16 am
by social_experiment
The radio button is part of the $_POST array that is available when the form is submitted. Assuming that you are going to use multiple radio buttons your php code will be similar to the following

Code: Select all

<!-- other form code -->
<input type="radio" name="building" value="value_0" />
<input type="radio" name="building" value="value_1" />
<!-- -->
You equate the value to a variable (inside the function).

Code: Select all

<?php
 // the value for the radio
 $radio_value = $_POST['building'];
?>
You need to check if the submit button has been clicked, not if the radio button is set (this is done when you have established that the button has indeed been clicked). In html code you have to add this to your form

Code: Select all

<input type="submit" value="Go" name="submit_btn " />
then change your php to

Code: Select all

<?php
if (isset($_POST['submit_btn'])){
 // run the function. 
} ?>

Re: Error on page

Posted: Mon Oct 04, 2010 7:27 am
by alexia_net
Hello. Thank you for your answer. This is what I am trying to avoid, the button for submission. From your answer I understand that there is no way to populate/refresh a list if I click the radio button, right?I want to avoid the button because I would have to many of them on my form. The form will get more complex in time. So, there is no way to execute a code after clicking another object on the form beside a button? Thank you!

Re: Error on page

Posted: Mon Oct 04, 2010 7:36 am
by social_experiment
alexia_net wrote:Hello. Thank you for your answer. This is what I am trying to avoid, the button for submission. From your answer I understand that there is no way to populate/refresh a list if I click the radio button, right?I want to avoid the button because I would have to many of them on my form. The form will get more complex in time. So, there is no way to execute a code after clicking another object on the form beside a button? Thank you!
No, the radio button is not a 'clickable' element like a submit button. I am not sure whether the option (onclick) exists for the radio button through Javascript. This might be a long shot but try taking a look at using AJAX (again, im not sure of your level of expertise with any programming language).

On the aspect of a complex form : If you code correctly (HTML and php) I don't think there will be a problem. Keep each form seperate and it should work a treat.