Error on page
Moderator: General Moderators
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Error on page
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.
<!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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Error on page
Code: Select all
<input type="radio" name="RadioGroup1" value="building" id="RadioGroup1_0" onclick="LoadBuildings()";/>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
Do you have any suggestion? Thx.
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Error on page
Read simple tutorial at least.
http://www.php.net/manual/en/tutorial.php
http://www.php.net/manual/en/tutorial.php
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Error on page
@ 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 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Error on page
... and don't forget to forget to press
Code: Select all
:)-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
Hi. This is all the code i have. I am stuck here. Thx.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Error on page
Code: Select all
<input type="radio" name="RadioGroup1" value="building" id="RadioGroup1_0" onclick="LoadBuildings()";/>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
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>';
}
} ?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
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.
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
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.
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
Hi. I think that the code should look like this
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.
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> </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> </p>
</form>
';
if (isset($_POST['radio'])){
display_postArray();
}
?>
</body>
</html>
if (isset($_POST['radio'])){
display_postArray();
}
executes to quickly and nothing happens. How could i bypass this? Thank you.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Error on page
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
You equate the value to a variable (inside the function).
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
then change your php to
Code: Select all
<!-- other form code -->
<input type="radio" name="building" value="value_0" />
<input type="radio" name="building" value="value_1" />
<!-- -->Code: Select all
<?php
// the value for the radio
$radio_value = $_POST['building'];
?>Code: Select all
<input type="submit" value="Go" name="submit_btn " />Code: Select all
<?php
if (isset($_POST['submit_btn'])){
// run the function.
} ?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
alexia_net
- Forum Newbie
- Posts: 17
- Joined: Fri Oct 01, 2010 7:17 am
Re: Error on page
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!
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Error on page
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).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!
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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering