/    Sign up×
Community /Pin to ProfileBookmark

php and ajax problem nid help….

sorry for posting this here i was looking for a ajax index,

i really need some help here,i was using the php and ajax example at w3schools.com “http://w3schools.com/php/php_ajax_database.asp

i used a database to populate the list box which worked fine with me,and when i change the list box, i call the getuser.php and then it shows the result in a listbox in the <div> txtHint which also works fine,here’s my problem… i want to add another <div> tag to show another listbox which also has the list from the previous list..

ill show it like this as the output from a page

[CODE]
<—LIST BOX—> // here is the main list box
//when i change this,it will show the result in sub list box 1
//i want to do is at the same time that the main box is
//changed the sub list box 2 is also updated…

<—SUB LIST BOX 1—> //get result from getresult.php

<—SUB LIST BOX 2—> //get result from another getresult_2.php page

[/CODE]

i hope someone call help me on this asap im really in a pinch…thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@SheldonJul 13.2008 — Can you post your current code so we can see where you are at and then can offer more help.
Copy linkTweet thisAlerts:
@nebchill26authorJul 13.2008 — here's my code

the html form for the main page
[CODE]
<script src="scripts.js"></script>
<table>
<tr>
<td width="120" class="type1">Department:</td>
<td class="type1">
<select name="Dept" onChange="CallAll(this.value)">
<option value=""><---Select Here---></option>
<?php
$GetDepartment = mysql_query("SELECT *department from tb_mcomputer_header")
or die ('MySQL Error:'.mysql_error());

while($newArray = mysql_fetch_array($GetDepartment)) {
$Department = $newArray['department'];
//$Location = $newArray['Location'];
?>
<option value="<?php echo $Department;?>"><?php echo $Department;?></option>

<?php } ?>
</select>
</td>
</tr>

<tr>
<td width="120" class="type1">List 1:</td>
<td class="type1"> <!--DIV TAG FOR AJAX--><div id="txtHint">here</div></td>
</tr>


<tr>
<td width="120" class="type1">List 2:</td>
<td class="type1"> <!--DIV TAG FOR AJAX--><div id="txtHint_2nd">here</div></td>
</tr>
</table>
[/CODE]



now here's the code for the scripts that will be called:

scripts.js
[CODE]
////AJAX FOR DEPARTMENT AND LOCATION
var xmlHttp
function CallAll(str){
showDeptLoc(str);
showDept_2nd(str);
}


function showDeptLoc(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

///////////////////// ---tried to add this code and it works but sometimes it //doesn't
function showDept_2nd(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser_2nd.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged_2nd
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged_2nd()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint_2nd").innerHTML=xmlHttp.responseText
}
}

/////////////////////

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

[/CODE]



now for the getuser.php page
[CODE]
<?php
include ("db_conn.php");

$q=$_GET["q"];

$sql=("SELECT distinct * FROM tb_mcomputer_header where department='$q'")
or die ('MySQL Error:Cant connect to host'.mysql_error());

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
$dept = $row['department'];

}

$sql=("SELECT * FROM tb_mcomputer_header WHERE department = '".$dept."'")
or die ('MySQL Error:Cant connect to host'.mysql_error());

$result = mysql_query($sql);
?>
<select multiple name="PC_APP1[]">
<?
while($row = mysql_fetch_array($result))
{ ?>
<option value="<? echo $row['computerserial'];?>"><? echo $row['computername'];?></option>
<? } ?>
</select>

[/CODE]

the code for the getuser_2nd.php is pretty much the same as getuser.php the only difference is the name of the select tag which is PC_APP2[]..




i really hope anyone can help me with this....
×

Success!

Help @nebchill26 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...