Dear All,
First I have a combo box populated from mysql db. Then onChange of the first combo box then I would like to populate my second combo box. I am trying out the jquery method as below. The first combo box id is $clientID. The problem I dont get the alert method shown that means is not working. What could be wrong here? Thank you.

$("#clientID").ready(function() 
{   
$("#clientID").onchange(function()
{     
  alert("getnow");
	var val = $(this).val();     
	// fire a POST request to populate.php     
	$.post('getHaulierNotInClient1.php', { value : val }, populateDropdown, 'html'); }     
}
); 
}
);

Recommended Answers

All 7 Replies

does dropping/removing the extra } at the end of line 8 help?

Can you use Firefox error console and see if there are any errors?

Dear Solid,
Is say missing ) after argument list point at line 9. So what must I include more of this ) is it? Thank you.

I guess you need to put in your entire code. That does not make sense to me for the code you've given.

Dear Solid,
Below is my full javascript and html code and the php code which it is calling?

<script type="text/javascript">
$("#clientID").ready(function() 
{   
 {
		$("#clientID").onchange(function()
		{     
		  alert("getnow");
			var val = $(this).val();     
			// fire a POST request to populate.php     
			$.post('getHaulierNotInClient1.php', { value : val }, populateDropdown, 'html');     
		}
  );
 } 
}
);
function populateDropdown(data) 
{     if (data != 'error') 
			{         
				$('#haulierID').html(data);     
			} 
} 
</script>
<body>

<table> 
<tr>
<form action="<?=$_SERVER['PHP_SELF']?>" method="get" name="form1" id=form1 >
<td width=700 valign="top">
	<table>
		
		
		<tr>
			<td>				
			  <label class=description for=element_1>Client Name<font color="red">*</font></label> 
			</td>
			<td>
			  <?php
        echo "<select class='select' id='clientID' name='clientID' > ";
        $link = mysql_connect(dbHost, dbUser, dbPassword);
				if(!$link)
				{
						die('Failed to connect to server: ' . mysql_error());
				}
					
				//Select database
				$db = mysql_select_db(dbDatabase);
        $query  = "SELECT clientID, clientName FROM tblClient Where clientStatus='a' ORDER BY clientName";
        $result = mysql_query($query);
        echo "<option value=0>-Select Client-</option>";
        while($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
	        if($clientID==$row[clientID])
		      {
		       echo "<option selected value=".$row['clientID'].">".$row['clientName']."</option>";
		      }
		      else
		      {
		       echo "<option value=".$row['clientID'].">".$row['clientName']."</option>";
		      }
	      }
        echo "</select>";
        mysql_close();
        ?> 
			</td>
		</tr>
		<tr>
			<td><p class=error id="clientIDError" ></p></td>
		  <td></td>
		</tr>
		
		<tr>
			<td>				
			  <label class=description for=element_1>Haulier<font color="red">*</font></label> 
			</td>
			<td>
				<div id="cbHaulier">
					<select name="haulierID" id="haulierID">
					<option value="122">122</option>  
				</div>
			  
			</td>
		</tr>
		<tr>
			<td><p class=error id="haulierIDError" ></p></td>
		  <td></td>
		</tr>
		
		
		<tr>
			<td>		
							
			  <input class="buttons" type="Submit" name="<?php echo $submitTag?>" value="<?php echo $submitTag?>" onClick="return validateForm();">
			</td>
			<td>
			  <input class="buttons" type="Reset" name="Reset" value="Reset" onclick="location.href='addHaulierUser.php'">			  
			</td>
		</tr>
		
		
		
				
		
	</table>
</td>
</form>

</body>

my php codes

<?php 
require_once('config.php');
if (!empty($_POST['value'])) 
{     
	 
	$link = mysql_connect(dbHost, dbUser, dbPassword);
	if(!$link)
	{
			die('Failed to connect to server: ' . mysql_error());
	}
		
	//Select database
	$db = mysql_select_db(dbDatabase);
	if(!$db) 
	{
			die("Unable to select database");
	}
	
	$selectQuery1 ="Select haulierID,haulierName From tblHaulier Order By tblHaulier.haulierName"; 
	$result1 = mysql_query($selectQuery1);
	$n1 = mysql_num_rows($result1);
  $html="<select class='select' id='haulierID' name='haulierID'>";
	$html .="<option value=0>-Select Haulier-</option>";
	while($row1 = mysql_fetch_array($result1, MYSQL_ASSOC))
	{
		$haulierID = $row1['haulierID'];
		//echo "\nHaulierID".$haulierID;
		$selectQuery2 ="Select haulierID,clientID From tblClientHaulier Where clientID='".mysql_real_escape_string($_POST['value'])."' And haulierID='".$haulierID."'"; 
	  $result2 = mysql_query($selectQuery2);
	  //$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	  $n2 = mysql_num_rows($result2);
	  //echo "<br>selectQuery2 ".$selectQuery2;
	  //echo "<br>n2 ".$n2;
	  $hm = $row1['haulierName'];
	  if($n2>0)
	  {
	  	
	  }
	  else
	  {
	   $html .="<option value=".$haulierID."><".$hm."></option>";
	 }
		
	}
	$html .="</select>";							 
	
	mysql_close($link);
	die($html); 
}
die('error'); 
?>

in your html code, line 5:
$("#clientID").onchange(function()

should be:
$("#clientID").change(function()

Dear Solid,
Upon changing it I get a new error "$ is not defined" which is pointing to $("#clientID").ready(function() ? I am quite lost what do you think I should change?

You can't use jquery commands if you have not included the jquery script. If you posted ALL your code above, then it appears to be missing.

You need to add a line of code in your <head> tag of the document that includes/calls the jquery script:

<script src="/includes/jquery_1.5.js" type="text/javascript"></script>

Your webpage would be:

<html>
<head>
include scripts here
<title>title here</title>
<script type="text/javascript">
scripts/functions here
</script>
<style type="text/css">
CSS here
</style>
</head>
<body>
Page content here
</body>
</html>

You can get the jquery script to put in your file here:
http://code.jquery.com/jquery-1.5.min.js

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.