HTML form Select Option display MySql database query

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
kosalayb
Forum Newbie
Posts: 3
Joined: Thu Aug 17, 2006 5:30 pm

HTML form Select Option display MySql database query

Post by kosalayb »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a database created in MySql, which has company information such as area code, address1, email and webaddress.
In the html form,  when the user select the area code from the pull down menu without hiting any other button( Form/Select /Option ) , company information in that area should be displayed. 

I wrote a php program (server.php)to read data from the database

Code: Select all

<?php

$user="root";
$password="";
$database="bombay";
mysql_connect("kosala",$user,$password);
@mysql_select_db($database) or die("Unable to select database");

$item = $_REQUEST['item'];
echo "Option Selected : $item <br> ";

$query= "SELECT * FROM Bombayt0 ";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i<$num){
	$CName=mysql_result($result,$i,"CName");
	$CAdd=mysql_result($result,$i,"CAdd");
	$CPCode=mysql_result($result,$i,"CPCode");
	$CAdd=mysql_result($result,$i,"CAdd");
	$CTel=mysql_result($result,$i,"CTel");
	$CFax=mysql_result($result,$i,"CFax");
	$CEmail=mysql_result($result,$i,"CEmail");
	$CWeb=mysql_result($result,$i,"CWeb");

                  $i++;
}

?>
I created a html program(client.html)

Code: Select all

<html>
<head>
<script language="javascript" src="server.php"> 
</script>

</head>

<body>

<form name="form" >
<select method="post" name="item" onChange="go()">
<option value="1" > 1 </option> 
<option value="2" > 2 </option> 
</select>


<script type="text/javascript">
var selectedItem = 0;

function go(){
	box=document.forms[0].item;
	selectedItem = document.forms[0].item.options[box.selectedIndex].value;
}
</script>

Company Name: <input type="text" name="CName" > <br>
Company Addr: <input type="text" name="CAdd" ><br>
Phone: <input type="text" name="CTel" ><br>
Fax: <input type="text" name="fax" ><br>
E-mail: <input type="text" name="CEmail" ><br>
Web: <input type="text" name="CWeb" ><br> 

</form>

</body>
</html>

how can i pass $result from server.php program to client.html? actually what is the best solution for this problem?

tx


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make server.php write client.html (to the browser.)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$i=0;
while ($i<$num){
        $CName=mysql_result($result,$i,"CName");
        $CAdd=mysql_result($result,$i,"CAdd");
        $CPCode=mysql_result($result,$i,"CPCode");
        $CAdd=mysql_result($result,$i,"CAdd");
        $CTel=mysql_result($result,$i,"CTel");
        $CFax=mysql_result($result,$i,"CFax");
        $CEmail=mysql_result($result,$i,"CEmail");
        $CWeb=mysql_result($result,$i,"CWeb");

                  $i++;
}
Somone has for phobia :P
In the html form, when the user select the area code from the pull down menu without hiting any other button( Form/Select /Option ) , company information in that area should be displayed.
Well you'll have to add some JavaScript to submit the form immediately after they make the choice then you'll be able to repopulate the form in anyway you like. onchange="this.form.submit()" should do it. In your PHP you can distingish between a user submission via <button> or <input type="submit"> and the one generated by JS by testing the existance of the button or input.
kosalayb
Forum Newbie
Posts: 3
Joined: Thu Aug 17, 2006 5:30 pm

Post by kosalayb »

feyd wrote:make server.php write client.html (to the browser.)
well, could you pl explain this more?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: HTML form Select Option display MySql database query

Post by RobertGonzalez »

kosalayb wrote:In the html form, when the user select the area code from the pull down menu without hiting any other button( Form/Select /Option ) , company information in that area should be displayed.
This implies client side activity. Is that what you are looking for? When a user selects something from drop down it will automatically populate the rest of the form (or parts of it)? please clarify because if this is the case I need to move this thread to Client Side.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Per PM:
kosalayb wrote:Yes, This is client side. When the user select an option from the pull down menue( ex: area code), server side php should populate, read data from the database and pass to the client side html.
This is being moved to client side.
kosalayb
Forum Newbie
Posts: 3
Joined: Thu Aug 17, 2006 5:30 pm

yep

Post by kosalayb »

Everah wrote:Per PM:
kosalayb wrote:Yes, This is client side. When the user select an option from the pull down menue( ex: area code), server side php should populate, read data from the database and pass to the client side html.
This is being moved to client side.
yep. When the pull down menue select the are code(ex:02), company names in that area should be displayed on the same page.company names are stored in Mysql database and php is used to read data.
Post Reply