Jump to content

[SOLVED] PHP / JS Confusion


bluebyyou

Recommended Posts

So I am ussing PHP and two while loops to populate two separate drop down menus in my from from information stored in my DB. That works no problem. However I want to be able to change which options are desplayed in the second menu based on what is selected in the first. I have definately seen this done on websites before.  Anyways, can someone point me in a direction for me to start figuring this out, my JavaScript for dummies book isnt helping me.

 

Here is what I am working with...(ignore the js code, i just cut and pasted from another script I wrote to try and get started.)

<?php include("includes.php");

if (isset($_POST['submit'])) 
{ 
$today = date("Y-m-d");
$query = sprintf("INSERT INTO Posting (postdate,type,subtype,price,title,description,email,reply) 
                  VALUES ('%s','%s,'%s','%s','%s','%s','%s','%s')",
			  $today, $_POST['type'],$_POST['price'],$_POST['title'],$_POST['description'],
                  $_POST['email'],$_POST['reply']);

dbcon($query); // <-- database connection function located on includes.php
echo "upload success.";
unset($_POST['submit']);
}
else
{?>

<?php include("header.php"); ?>
<SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
<!-- Hide from browsers that do not support JavaScript

function update_subtype() 
	{
	var update=yes
	document.writeln("hi")
	}

function display_subtype()
  		{
    	//var formSalary = document.form1.salary;
   		 var type = document.getElementById("subtype");
    	type.style.visibility=(document.form1.comp[0].checked)? "visible" : "hidden";
  		}

 -->
</script>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Posting Type<select name="type" onChange="update_subtype()">
<option value="0">Select One..</option>
<?php 
$query = "Select * FROM PostingType";
dbcon($query);

while ($row = mysql_fetch_array($result))
{
extract($row); ?>
<option value="<?php echo $key; ?>"><?php echo $name; ?></option>
<?php } ?>
</select><br>


<?php
$script = 1;
$query2 = "Select * FROM PostingSubType WHERE ptype= $script";
dbcon($query2); ?>

Posting Sub Type<select name="subtype" >
<option value="0">Select One..</option>
<?php

while ($row = mysql_fetch_array($result))
{
extract($row); ?>
<option value="<?php echo $psubtype; ?>"><?php echo $postingsubtype; ?></option>
<?php } ?>

</select><br><br>
Title<input name="title" type="text"><br>
Price<input name="price" type="text"><br><br>
Description<br>
<textarea name="description" cols="40" rows="4"></textarea><br><br>

Email<input name="email" type="text"><br>
Verify<input name="vemail" type="text"><br><br>

<input name="reply" type="radio" value="0" checked>Reply to my E-Mail<br>
<input name="reply" type="radio" value="1">Do not show my e-mail<br><br>

Upload image  (Coming Soon..)<br>
<input name="h" type="text" disabled><input name="browse" type="button" disabled value="Browse..."><br><br>

<input name="submit" type="submit" value="Submit">
</form>

</body>
</html>
<?php } ?>

 

//includes.php
<?php

/* function dbcon stores the database connection information and
   the connection functions */

/* $result must be handled on each individual page.
   for example: $row = mysql_fetch_array($result) .. */
function dbcon($sql) 
{
$host = "*";
$user = "*";
$password = "*";
$database = "*";

global $result;

$connection = mysql_connect($host,$user,$password) or die ("couldnt connect to rockypages server.");
$db = mysql_select_db($database, $connection) or die ("couldnt select rockypages database.");
$result = mysql_query($sql) or die ("Couldn't execute query.");

return $result;
}

function dbcon2($sql2) 
{
$host = "*";
$user = "*";
$password = "*";
$database = "*";

global $result2;

$connection = mysql_connect($host,$user,$password) or die ("couldnt connect to rockypages server.");
$db = mysql_select_db($database, $connection) or die ("couldnt select rockypages database.");
$result2 = mysql_query($sql2) or die ("Couldn't execute query.");

return $result2;
}

/* This Function takes the numerica data from the PostingType Table 
   and converts it to the corresponding text. */ 
   
/* $input is the $key field in the database */
   
function posting_type($input)
{
$switch = $input;
switch ($switch)
{
case "1":
	$output = "For Sale";
	break;
case "2":
	$output = "Jobs";
	break;
case "3":
	$output = "Community";
	break;
case "4":
	$output = "Housing";
	break;
case "5":
	$output = "Services";
	break;
}
echo $output;
}

function posting_subtype($input)
{
$switch = $input;
switch ($switch)
{
case "1":
	$output = "Barter";
	break;
case "2":
	$output = "Bikes";
	break;
case "3":
	$output = "Cars/Trucks/Motorcycles";
	break;
case "4":
	$output = "Books";
	break;
case "5":
	$output = "CD/DVD/VHS";
	break;
case "6":
	$output = "Free";
	break;
case "7":
	$output = "Clothing & Accessories";
	break;
case "8":
	$output = "Furniture";
	break;
case "9":
	$output = "General";
	break;
case "10":
	$output = "Computer";
	break;
case "11":
	$output = "Jewelry";
	break;
case "12":
	$output = "Electronics";
	break;
case "13":
	$output = "Sporting";
	break;
case "14":
	$output = "Household";
	break;
case "15":
	$output = "Tools";
	break;
case "16":
	$output = "Musical Instruments";
	break;
case "17":
	$output = "Wanted";
	break;
case "18":
	$output = "Accounting/Finance";
	break;
case "19":
	$output = "Administrative/Office";
	break;
case "20":
	$output = "Art/Design";
	break;
case "21":
	$output = "Buisness/Management";
	break;
case "22":
	$output = "Education";
	break;
case "23":
	$output = "Government";
	break;
case "24":
	$output = "Medical/Health";
	break;
case "25":
	$output = "Non-Profit";
	break;
case "26":
	$output = "Retail/Food/Hospitality";
	break;
case "27":
	$output = "Sales/Customer Service";
	break;
case "28":
	$output = "Skilled Trade/Craft";
	break;
case "29":
	$output = "IT/Web Design";
	break;
case "30":
	$output = "TV/Film/Video";
	break;
case "31":
	$output = "Writing/Editing";
	break;
case "32":
	$output = "Other";
	break;
case "33":
	$output = "Lost & Found";
	break;
case "34":
	$output = "Rideshare";
	break;
case "35":
	$output = "Pets";
	break;
case "36":
	$output = "Volunteers";
	break;
case "37":
	$output = "Events";
	break;
case "38":
	$output = "Apartments/Housing";
	break;
case "39":
	$output = "Rooms/Shared";
	break;
case "40":
	$output = "Sublets/Temporary";
	break;
case "41":
	$output = "Housing Wanted";
	break;
case "42":
	$output = "Automotive";
	break;
case "43":
	$output = "Computer";
	break;
case "44":
	$output = "Creative";
	break;
case "45":
	$output = "Skilled Trade/Craft";
	break;
case "46":
	$output = "Financial";
	break;
case "47":
	$output = "Lessons";
	break;
case "48":
	$output = "Musicians";
	break;
}
echo $output;
}
?>

Link to comment
Share on other sites

So I am ussing PHP and two while loops to populate two separate drop down menus in my from from information stored in my DB. That works no problem. However I want to be able to change which options are desplayed in the second menu based on what is selected in the first. I have definately seen this done on websites before.  Anyways, can someone point me in a direction for me to start figuring this out, my JavaScript for dummies book isnt helping me.

 

 

This should get you going:

 

  <url: http://www.javascripttoolbox.com/lib/dynamicoptionlist/ >

 

Or search comp.lang.javascript for 'dynamic option list', a link is in my signature.

Link to comment
Share on other sites

The NEW  method is to use an AJAX call to populate the second box after a selection has been made in the first. The OLD method used javascript arrays to make the appropriate adjustments. I'd strongly recommend the AJAX approach.

Unless it's a small static list, and there's no need to go back to the server constantly.

Link to comment
Share on other sites

Basically...its a classified page... with 5 main categories. and about 40 subcategories.

so I have two tables in my DB. So my first dropdown box uses a while loop to grab the 5 main categories.   my sub category table has three fields the subcategorynumber(primary key), its corresponding main category, and the sub category name. When a user selects a main category I want the sub category box to display only those that correspond to the main category selected....I read over this and its a long explanation about what you probably already get. But I want to set it up like this so when I have an admin page, I can add categories and sub categories but just updating the database. So heres the page im working on...http://www.rockypages.com/insert.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.