ajax problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kagiso boikanyo
    New Member
    • Mar 2011
    • 6

    ajax problem

    Hi I'm new to ajax, I've already created a drop down menu using php and loads my provinces dynamically from my database, now what is supposed to do is, if you click on any province in the drop down menu the cities have to appear on another page which are also in my database

    heres my php code that loads my drop down menu.

    Code:
    [<html>]
    
    [<head>][<title>]<?php echo $title;?>[</title>]
    
    [<h1>][<?php echo $heading;?>][</h1>]
    
    
    
    [</head>]
    
    [<body>]
    
    
    [<?]
    	function writeCities($id)
    	{		
    		$con = mysql_connect("localhost","root","");
    		if (!$con) die('Could not connect: ' . mysql_error());
    	  	mysql_select_db("msansi", $con);
    		$query  = "SELECT cities FROM provinces WHERE id =";
    		$query .= $id;
    		$result = mysql_query($query);			
    				
    		$row = mysql_fetch_array($result);
    		echo $row[0];		
    	}
    	
    
    
    	function populateDropBox()
    	{
    		$con = mysql_connect("localhost","root","");
    		if (!$con) die('Could not connect: ' . mysql_error());
    	  	mysql_select_db("msansi", $con);
    		$result = mysql_query("SELECT id,title,cities FROM provinces");
    		
    		while($row = mysql_fetch_array($result))
    		{	
    		    echo "<option value=$row[0]>" . $row['1']."</option>";
    		}
    	}
    [?>]
     
    	
    function onChangeDropBox()
    
    	{
    		var selected =0;
    		selected = document.myform.province.value;					
    		
    		var t = "<? writeCities(9);?>";
    		document.myform.submit.value = t;
    				
    		}
    
            [<form name="myform">]
    	[<script type="text/javascript">]
    		[<select name = "province" onChange="onChangeDropBox();"/>] 
    		[<? populateDropBox(); ?>] 	
    		[<input type ="submit" value="submit" onClick="showcities";/>]    
    		[</form>]	  	
    
    	  
    [</script>] 	  
    	  
    [</body>]
    {</html>]
    would you please shed some light on how I'm going to go about tackling this problem using ajax because I cant seem to find anything helpful over the net.

    thanxxx in advance!!!!
    Last edited by Niheel; Mar 8 '11, 05:07 PM.
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    If you are sending the user to another page, just post your province id variable and it will be available to your next page.

    You can do this by setting the action attribute of your form to the page you want the user sent to and set the method attribute to post.
    Code:
    <form name="your_form_name" action="your_next_page.php" method="post">
    Then on your next page call your writeCities function but pass in the province id for your parameter.

    It will be almost the exact same as the provinces form you created but instead with your writeCities function in place of your populateDropBox function.

    Comment

    • kagiso boikanyo
      New Member
      • Mar 2011
      • 6

      #3
      k thanxx jking it worked wonders i was using a wrong concept of trying to open a new window, now I've got this php parameter inside a script which requires me to put the id no of the province in order to show the cities which is not what i want, i need an effective way to pass php variables in javascript, heres the code


      function onChangeDropBox ()

      {
      var selected =0;
      selected = document.myform .province.value ;

      var t = ["<? writeCities(9); ?>"];
      document.myform .submit.value = t;

      }

      Comment

      Working...