Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

JS & Ajax works, but do not display correct

mysql, php, javascript, ajax, drop down list, textarea, select, div, reload

1:49 pm on May 24, 2012 (gmt 0)

New User

joined:May 24, 2012
posts: 8
votes: 0

I have used ajax scripts to load catagories out of mysql on a onChange event using a select box. The script will then get another file and display it on the same page inside a div without reloading the page. It all works fine, accept that it grabs part of the top menu and display it in the div as well. I have spend hours trying to figure out why but cannot get it to work. The only logical reason is that the scripts might clash with the jquery used for the menu at the top of the page.

Here is the code:


<SCRIPT language=JavaScript>
function reload(form){
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='update.php?cat=' + val ;

//ajax script to load subcat values
function showUser(str)
if (str=="")
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp.readyState==4 && xmlhttp.status==200)





$sql="SELECT * FROM subcat WHERE subcat = '".$q."'";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
echo $q"
<b>SUB</b> Category to be inserted under the following <b>MAIN</b> Category:<br>
<select name=industry value=industry class='textfield'>
<option value='select'>Please select...</option>
<input name='price' type='text' class='textfield' id='price' value=".$row['price']." /><br>
Edit or Add Description:<br>
<textarea name='desc' id='desc' />" .$row['desc']."</textarea><br>
<input name='update_sub' type='submit' value='Update Sub Category' class='button'><br>
<input name='update_main' type='submit' value='Delete Record' class='button'>



<div class="rightcontent">
<form action="./edit_comp.php" method="post" class="style">
Select a <b>SUB</b> Category to edit:<br>
////////// Starting of second drop downlist /////////
echo "<select name='users' onchange='showUser(this.value)'><option value=''>Select SUB Category</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[subcat]'>$noticia[subcat]</option>";
echo "</select>";
////////////////// This will end the second drop down list ///////////
<div class="rightcontent" id="subcatdetails">

You can view online at: [bapple.co.za...]
2:47 pm on May 24, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4966
votes: 10

Welcome to WebmasterWorld!
Note, the code you posted is subject to SQL Injection attacks, so I would advise you look into that.

Also, your script tag has a language attribute, which is invalid. Replace that with the type attribute, as in:

<script type="text/javascript">

I took a look at your page, and used Chrome's developer tools to inspect the XHR request and response. The request to getsc.php sends a response back that includes the entire document, including the DOCTYPE, <html>, <head>, scripts, etc., which contradicts the bit of PHP code you show above. Is there more code in getsc.php that you did not show above? Alternatively, is there some .htaccess stuff that happens that redirects requests to some site template code? "PSI'S MANAGEMENT SYSTEM"... seems to be the template/wrapper.
9:32 am on May 25, 2012 (gmt 0)

New User

joined:May 24, 2012
posts: 8
votes: 0

Hello Fotiman, thank you for the welcome message!
I am new to php mysql and js, therefore not understanding the following two comments from your post: SQL Injection and .htaccess - I will Google these.

I have changed the <script type="text/javascript"> - thank you.

Above is now the complete code for getsc.php - I removed the require for the header. You will notice that I do not get any results if I make a selection from the Subcat after removing the reuire function from getsc.php

PSI's MANAGEMENT SYSTEM is a template that I am busy building in css.
12:14 pm on May 25, 2012 (gmt 0)

New User

joined:May 24, 2012
posts: 8
votes: 0

SOLVED - There was code in the header which was conflicting with the ajax script when it load getsc.php into the div. I have renamed the file to header2.php and deleted the conflicting code. Now it seems to work like a charm. I Still have to include header2.php into getsc.php