473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. [<html>]
  2.  
  3. [<head>][<title>]<?php echo $title;?>[</title>]
  4.  
  5. [<h1>][<?php echo $heading;?>][</h1>]
  6.  
  7.  
  8.  
  9. [</head>]
  10.  
  11. [<body>]
  12.  
  13.  
  14. [<?]
  15.     function writeCities($id)
  16.     {        
  17.         $con = mysql_connect("localhost","root","");
  18.         if (!$con) die('Could not connect: ' . mysql_error());
  19.           mysql_select_db("msansi", $con);
  20.         $query  = "SELECT cities FROM provinces WHERE id =";
  21.         $query .= $id;
  22.         $result = mysql_query($query);            
  23.  
  24.         $row = mysql_fetch_array($result);
  25.         echo $row[0];        
  26.     }
  27.  
  28.  
  29.  
  30.     function populateDropBox()
  31.     {
  32.         $con = mysql_connect("localhost","root","");
  33.         if (!$con) die('Could not connect: ' . mysql_error());
  34.           mysql_select_db("msansi", $con);
  35.         $result = mysql_query("SELECT id,title,cities FROM provinces");
  36.  
  37.         while($row = mysql_fetch_array($result))
  38.         {    
  39.             echo "<option value=$row[0]>" . $row['1']."</option>";
  40.         }
  41.     }
  42. [?>]
  43.  
  44.  
  45. function onChangeDropBox()
  46.  
  47.     {
  48.         var selected =0;
  49.         selected = document.myform.province.value;                    
  50.  
  51.         var t = "<? writeCities(9);?>";
  52.         document.myform.submit.value = t;
  53.  
  54.         }
  55.  
  56.         [<form name="myform">]
  57.     [<script type="text/javascript">]
  58.         [<select name = "province" onChange="onChangeDropBox();"/>] 
  59.         [<? populateDropBox(); ?>]     
  60.         [<input type ="submit" value="submit" onClick="showcities";/>]    
  61.         [</form>]          
  62.  
  63.  
  64. [</script>]       
  65.  
  66. [</body>]
  67. {</html>]
  68.  
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!!!!
Mar 8 '11 #1

✓ answered by JKing

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.
Expand|Select|Wrap|Line Numbers
  1. <form name="your_form_name" action="your_next_page.php" method="post">
  2.  
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.

2 954
JKing
1,206 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. <form name="your_form_name" action="your_next_page.php" method="post">
  2.  
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.
Mar 8 '11 #2
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;

}
Mar 9 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Joel Byrd | last post by:
I am making an AJAX call when any one of a number of inputs in a form (search criteria) are clicked, checked, changed, etc. When the user clicks, checks, whatever, I am trying to display a...
9
by: Eric Wallstedt | last post by:
I have a page that "logs" changes made to input fields using ajax to pass data to a cgi. I use POST and it works fine most of the time (all the time in IE). But it fails when I get the data from...
1
by: monudjn | last post by:
Hi I am implementing ajax in portal. Using ajax i am able to updating the content of portlets asynchronously. However i am facing a problem The Problem: While submitting the form i am getting...
2
by: K. | last post by:
Hello! I have the following problem. I have form div which is replaced by ajax event. Unofrtunately all the ajax inputs are null after posting the form (method="post") in Firefox, but on...
1
by: JohnnieTech | last post by:
I am using some javascript/ajax to load content into a main div. The problem I am running into is that it will work in IE but not in FF. In FF I don't get any sort of load at all. I have a 1...
5
by: Eyeinstyin | last post by:
Basic Problem is Every scroll of scrollbar makes an ajax call.So if user play with scrollbar say 100 times 100 ajax calls go and server starts processing 100 calls.So abort won't help.The last time...
1
by: mazdotnet | last post by:
Hi guys, On my work computer I had VisualStudio 2005, AJAX 1.0 and AJAX toolkit installed. 2 days ago I installed VisualStudio 2008 and every AJAX test that I do (IIS), does a postback. When I...
1
by: violinandviola | last post by:
I have just put 4 different ajax bits on this page: http://jimpix.co.uk/default-ajax.asp The ajax spits out chunks of images / news content, and users can view the chunks via next / prev links....
4
by: sheldonlg | last post by:
I haven't received an answer with my other post, so I am rephrasing it here. In php I have a 2D array which I "print". The headers force it to be a file on the user's system. The user receives...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.