Page 1 of 1

Chained Select - from personal message by ashrafzia

Posted: Thu Oct 25, 2007 5:01 am
by CoderGoblin
Sent to me as private message but moved here for discussion...
ashrafzia wrote:Hello Sir:

i am new to ajax and found your post Dynamic/Chained Selects using Ajax Prototype/JQuery very helpful.
as you have mentioned you will not answer questions in that post anymore and have to open a new post in php-code with a reference to the previous one. I really don't know how to refer to a post thats why disturbing you here.

I am having some problem related with that post, i hope you will answer my question. If don't want to answer here then it will be very kind of you to let me know how to open a new post with reference.

In your "index.php" file you have included the sublist file like this and its working perfect:

Code: Select all

<body>
<span id="firstlist">
<select name="first" onchange="getSecond(this.value);">
<option value="1">Names starting with A</option>
<option value="2">Names starting with B</option>
<option value="3">Names starting with C</option>
<option value="4">Names starting with D</option>
<option value="5">Names starting with E</option>
</select>
</span>
<span id="sublist"><?php include 'sublist.php'; ?></span>
I have a page"subject.php" which has a list box having some values inside it, i want whenever a value is selected , a new list box should appear with some values.
I have been successful so far, but i am unable to perform the last step which is to include 'sublist.php'.
Heres the code, i think you may point out the problem:

Code: Select all

$form = "<head><title>:::Subjects:::</title></head>
<script src='ajax_files/prototype.js'></script>
<script src='ajax_files/getSecond.js'></script>
<body>
<form action='subjects.php' method='get' enctype='multipart/form-data'>
<table width='394' border='1' align='center' cellpadding='5' cellspacing='5'>
<tr>
<td>Programe Name:</td>";

$sql = "select programe_name from programmes";
$result = mysql_query($sql, $conn) or die (mysql_error());
while ($row = mysql_fetch_array($result)){
$add .="<option value='$row[programe_name]'>$row[programe_name]</option>";
}

$form .= "<td>
<select name='prog_name' id='prog_name' onchange='getSecond(this.value)'>
<option value=''>--Select--</option>
$add
</select>
</td>
</tr>
<tr>
<td>Semester:</td>
<td><span id="comboHint">";

$include = include('ajax_files/sublist.php'); //here i don't know wat to do? how to include this file?
// include "ajax_files/sublist.php";

$form .= "$include</span>
</td></tr></table></form></body></html>";
I am stucked with how to include the sublist.php file between the <span></span> tags.
I have tried, above you can see but its printing only the integer value 1 inside it, nothing else.

Would you kindly guide me, what to do?

Hope to hear!
Bye
Couple of comments on this to begin with,

Code: Select all

while ($row = mysql_fetch_array($result)) {
  $add .="<option value='$row[programe_name]'>$row[programe_name]</option>";
}
If you are always looking at getting the column names you may want to use mysql_fetch_assoc. Simply a matter of why get additional data you never need (numeric indexes).

Another problem I can see is the way you concatenate text together. You cannot simple place arrays into a double quoted string. (look at strings in php manual). Try the following...

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
  $add .="<option value="{$row['programe_name']}">{$row['programe_name']}</option>";
}
Your include problem:
In the original code I simply echoed it the result. This is because the sublist.php must be used on it's own when using the AJAX to return just the changed text. The structure of the files allowed me to do this easly. To solve your problem you should investigate how to recognise if the sublist.php has been called via the include or as part of the AJAX call. A lot here depends on the library you are using and you should potentially look up the libraries documentation. If all else fails you could pass another parameter as part of the AJAX call. Once you know if it it an AJAX call you determine what happens next. If called via AJAX/Javascript echo the result. IF not set the variable ($add).

Hope that helps

Posted: Thu Oct 25, 2007 11:26 pm
by ashrafzia
I have done with what i was trying to do. I found no other way to solve my problem thats why starting from the scratch again :).

Test.php:

Code: Select all

<?php
include "connection.php";
$tbname = "subjects";

$sql = "select programe_name from programmes";
	$result = mysql_query($sql, $conn) or die (mysql_error());
	while ($row = mysql_fetch_array($result)){
		$add .="<option value='$row[programe_name]'>$row[programe_name]</option>";
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="prototype.js" type="text/javascript"></script>
 <script type="text/javascript">
  function getSecond(value) {
    var url = 'sublist.php';
    var myAjax = new Ajax.Request
      (
        url,
        {
          method: "post",
          parameters : "query="+value,           
          onSuccess: function transResult (response) {
            document.getElementById('sublist').innerHTML=response.responseText;                       
          },
          onFailure: function transResult (response) {
            alert ('Failure'+response.responseText);
          }
        }
      );
      return false;
  }
  </script>
</head>

<body>
<form name="form1" method="post" action="">
  <table width="308" border="1" align="center">
    <tr>
      <td width="128">Programe Name : </td>
      <td width="164"><select name="prog_name" id="prog_name" onChange="getSecond(this.value);">
	  					<option value="">--Select--</option>
						<?php echo "$add"; ?>
	  					</select></td>
    </tr>
    <tr>
	<td>Semester:</td>
	<td><span id="sublist"><?php include 'sublist.php';   ?></span>	</td>
    </tr>
  </table>
</form>
</body>
</html>
sublist.php:

Code: Select all

<?php
// Define the result list possibilities. This could easily be retrieved from a database
include "connection.php";
$prog_name = $_POST['query'];

$select =  "<select id=\"semester\" name=\"semester\">
			<option value=\"\">--Select--</option>	";


$sql = "SELECT	programmes.no_of_semesters
		FROM programmes
		WHERE	programmes.programe_name = '$prog_name' ";
		
$result = mysql_query($sql, $conn) or die (mysql_error());

	while ($row = mysql_fetch_array($result)){
		$sem = $row['no_of_semesters'];
	}
	for ($i=1;$i<=$sem;$i++)
	{
		$select .= "<option value='$i'>$i</option>";
	}
		$select .="</select>";
		echo "$select";

?>
Its working fine.
About your comments on mysql_fetch_assoc, i feel uncomfortable dealing with it, thts why using mysql_fetch_array.

Now I am just wondering how can i achieve this goal:

Suppose i have a list box in which i have programe names.
And the programe name values are coming from a table.

Now whenever a programe name is selected, for example. BBA then a new list box should appear with a query from a table, with no of semesters.

Now when a semester is selected, for example. for the programe BBA, 8th semester is selected, then a new list box should appear with a query from a table, with subject names

And in the last when the Insert Values button is clicked all values from the list boxes should go in the database table.

So summary is: 1 list box is dependent on another and the root list box is Programe Name.

Hierarchy of list boxes is :
Programe Name > No of Semesters > Subject Names

I have completed the 1st step i-e whenever a programe name is selected the no_of_semesters list box is appearing with values.
Now i am stucked and wondering how to move forward.....

Can i move forward by adding same script and new sub_sublist.php files??

Any idea! How can i accomplish this task? or Any code example from which i can take help?

Thanks in Advance.