Work Based Project - I'm Stuck : (

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zooropa
Forum Newbie
Posts: 6
Joined: Wed May 26, 2010 1:40 pm

Work Based Project - I'm Stuck : (

Post by zooropa »

I have 2 pages, namely main.html and list.php

main.html contains the following code and consists of a dropdown that i want to pull some database records into by calling the function fillCategory() contained in list.php


Content on main.html
**************************
**************************

Code: Select all

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Page Title</title>
<script language="javascript" src="list.php"></script>
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fillCategory();">
<FORM name="drop_list" action="yourpage.php" method="POST" >
<SELECT NAME="Category" >
<Option value="">Category</option>
</SELECT> 
</form>
</body>
</html>
 
Content of list.php
**********************
**********************

Code: Select all

<?php
 
require_once('mysqli_connect.php');
echo "
function fillCategory( 
";
 
$q="select * from category";
 
echo mysqli_error($dbc);
 
$r=@mysqli_query($dbc,$q);
 
while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){
echo "addOption(document.drop_list.Category, '$row[cat_id]', '$row[category]');";
}
 
?>
 
I know that records are being pulled from the MySQL database since when i view 127.0.0.1/list.php i can see the following diplayed on the page:

Code: Select all

function fillCategory( addOption(document.drop_list.Category, '1', 'Sport');addOption(document.drop_list.Category, '2', 'Music');addOption(document.drop_list.Category, '3', 'Art');
 
i.e. the contents (Sport, Music, Art) that i'd like to appear in my dropdown. However, i suspect that i shouldn't see the above being displayed in list.php Instead, it should probably be passed to main.html "unseen".

Can anyone help by pointing out the probable error in my syntax.

Thanks,
Mark
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Work Based Project - I'm Stuck : (

Post by requinix »

The error is that

Code: Select all

function fillCategory( addOption(document.drop_list.Category, '1', 'Sport');addOption(document.drop_list.Category, '2', 'Music');addOption(document.drop_list.Category, '3', 'Art');
is not valid JavaScript code.
zooropa
Forum Newbie
Posts: 6
Joined: Wed May 26, 2010 1:40 pm

Re: Work Based Project - I'm Stuck : (

Post by zooropa »

Thanks for your reply Tasairis. I've been stuck on this cascading Dropdown Box problem for weeks.
My 600 page PHP/ MySQL book doesn't cover it and i can't find much in the way of tutorials (and the ones i find aren't well explained)
Does anyone know how or where i can go to find the answer. This should be so easy but it's so difficult. Argggghhh!
Any pointers greatly appreciated.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Work Based Project - I'm Stuck : (

Post by internet-solution »

Why not ditch the javascript and build the select element from PHP?

rename main.html as main.php and revise like this -

Code: Select all

<?php
$optionlist="";
require_once('mysqli_connect.php');
$q="select * from category"; 
echo mysqli_error($dbc);
$r=@mysqli_query($dbc,$q);
while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){
$optionlist.="<option id='".$row[cat_id]."'>".$row[category]."</option>";

}
 
?>

?>

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Page Title</title>
<script language="javascript" src="list.php"></script>
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fillCategory();">
<FORM name="drop_list" action="yourpage.php" method="POST" >
<SELECT NAME="Category" >
<Option value="">Category</option>
<?php echo $optionlist ?>
</SELECT>
</form>
</body>
</html>
zooropa
Forum Newbie
Posts: 6
Joined: Wed May 26, 2010 1:40 pm

Re: Work Based Project - I'm Stuck : (

Post by zooropa »

Thanks internet-solution. I've tried going down your suggested route and have got it working at long last so thanks for your help. I've got some tidying up to do on the code but once i've done that i'll post everything (PHP code) and Database Table structure, Fields and Data for anyone else that might need this in the future. Thanks again.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Work Based Project - I'm Stuck : (

Post by internet-solution »

You are welcome. Glad to know that you got it working.

My principle is to do most things on server side with PHP and use javascript as the last resort, as javascripts can be blocked by users.
Post Reply