This is a forum where members new and old can come to ask questions and get info and opinions. It is not a place to advertise your business or have other forms of advertising, whether it be in your posts or signature.

All links in the forum will not be indexed by Search Engines and any unapproved forms of advertising or spam will be dealt with accordingly, deleted, and that member's account banned.

Forums

Overview » Ajax & JavaScript » Request for Help - I'm Stuck!
Reply

Request for Help - I'm Stuck!

Mark Taylor
Member



Since: 06 Feb 2003
Posts: 24
Posted 25 May 2010 00:57:03

I have 2 pages, namely main.html and list.php The Main Pageincludes 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
********************

<!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>&nbsp;
</form>
</body>
</html>

Content of list.php
*******************

<?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:

function fillCategory( addOption(document.drop_list.Category, '1', 'Sport');addOption(document.drop_list.Category, '2', 'Music');addOption(document.drop_list.Category, '3', 'Art');

which are 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 point out the probable error in my syntax. I can provide more detail on the database structure if it will help.

Thanks,
Mark

I have 2 pages, namely main.html and list.php The Main Pageincludes 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
********************

<!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>&nbsp;
</form>
</body>
</html>

Content of list.php
*******************

<?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:

function fillCategory( addOption(document.drop_list.Category, '1', 'Sport');addOption(document.drop_list.Category, '2', 'Music');addOption(document.drop_list.Category, '3', 'Art');

which are 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 point out the probable error in my syntax. I can provide more detail on the database structure if it will help.

Thanks,
Mark
Alan C
Total freaking Member



Since: 04 May 2006
Posts: 473
Replied 28 May 2010 12:12:10
I think there's a mismatch here in the technologies, php is serverside, so everything it does happens on the server before the html is served, whereas javascript is clientside and doesn't begin doing anything until after the html/javascript has been delivered to the browser. You would use javascript if you wanted to modify the dropdown AFTER it had been rendered in the browser.

I don't think you need the script tag, what you're saying with that is that there's some javascript to execute and it's in the file list.php, but list.php contains php not javascript. The two technologies can't be mixed like this.

You can pull the records from the database and write them directly into the dropdown without any reference to javascript.

Hope that helps.
Miroslav Zografski
Official Representative



Since: 04 Apr 2008
Posts: 3,044
Replied 06 Jan 2011 11:21:05
Hello ,

Mark can use both files. However he needs to include the list.php in the main.php. But really that does not worth it.
better to have the sql select on the main.php and populate a <option></option> tags with it.

Or if Mark uses DW he can always have a dynamically generated Select created by the program.

Regards,

Reply to this topic

Message
Reply
Follow us on Facebook Follow us on twitter Subscribe to the RSS feed
Activate your free membership today | Login | Currency