Page 1 of 1

Chained Select - from personal message by ashrafzia

Posted: Thu Oct 25, 2007 5:00 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