Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP List Box in HTML Form help
Old 04-08-2009, 12:05 PM PHP List Box in HTML Form help
Average Talker

Posts: 17
Name: Gary
Trades: 0
I have PHP file that creates a List Box and populates it from a field in a MySQL database table. I need to place the PHP code for the List Box into an HTML file to do the same. I've been trying to figure this out for hours scouring the web. I must be missing something simple. I tried making the PHP code a function. The function worked, but when I included it and called it from the HTML file, nothing was displayed. Thanks in advance.

The codes follow.

This is the PHP code that works:

<?php

$dbc = mysql_connect('xxxxx', 'yyyyy',zzzzz')
or die('Error connecting to MySQL server.');

mysql_select_db('mydb');


$result = mysql_query("select distinct assn from compsin order by assn");

echo '<select name="compsin">';

while ($nt= mysql_fetch_assoc($result)) {

echo '<option value="' . $nt['id'] . '">' . $nt['assn'] . '</option>';


}

echo '</select>';

?>

==================================================

Here is the HTML code without PHP:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>List Box PHP in HTML</title>


</head>

<body>

<form method="post" action="1004mcout.php">

<p><center>List Box PHP in HTML</CENTER></P>


<p>-- FOR CONDOS ONLY -- If the subject is a condo, enter the name of the subject condo association (as it appears on the comparables spreadsheet). If the subject is not a condo, leave blank.</p>


<p></p>




<p>Click "SUBMIT" to display the calculation results</p>

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

<br />


</form>

</body>

</html>
gdaniels is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-08-2009, 02:30 PM Re: PHP List Box in HTML Form help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I think this accomplishes what you were going for:

PHP Code:
<?php
$dbc 
mysql_connect('xxxxx''yyyyy','zzzzz')
     or die(
'Error connecting to MySQL server.');

mysql_select_db('mydb');

$result mysql_query("select distinct assn from compsin order by assn");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>List Box PHP in HTML</title>


</head>

<body>

<form method="post" action="1004mcout.php">

<p><center>List Box PHP in HTML</CENTER></P>
<select name="compsin">
<?php
while ($ntmysql_fetch_assoc($result)) 
{
     echo 
'<option value="' $nt['id'] . '">' $nt['assn'] . '</option>';

?>
</select>'
<p>-- FOR CONDOS ONLY -- If the subject is a condo, enter the name of the subject condo association (as it appears on the comparables spreadsheet). If the subject is not a condo, leave blank.</p>


<p></p>




<p>Click "SUBMIT" to display the calculation results</p>

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

<br />


</form>

</body>

</html>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-08-2009, 03:12 PM Re: PHP List Box in HTML Form help
Average Talker

Posts: 17
Name: Gary
Trades: 0
Thanks, Nullpointer. Your suggestion had an extra ' at </select>'. However, the List Box did not work. A small List Box appears, but it was empty. Any other suggestions.
gdaniels is offline
Reply With Quote
View Public Profile
 
Old 04-08-2009, 03:25 PM Re: PHP List Box in HTML Form help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If the list is empty it is most likely an issue with your query. Verify that it returns the rows you expect, independently from this script.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-08-2009, 03:55 PM Re: PHP List Box in HTML Form help
Average Talker

Posts: 17
Name: Gary
Trades: 0
Nullpointer,

This code works as a PHP file. In the PHP file, if I remark out the line:

//echo '<option value="' . $nt['id'] . '">' . $nt['assn'] . '</option>';

I get the same error as the error from the code you provided for the HTML form. Makes me think that line in the HTML form may have the problem. I'm checking it out. Any help is welcomed.

Good code as PHP file:

<?php

$dbc = mysql_connect('xxxxx', 'yyyyy',zzzzz')
or die('Error connecting to MySQL server.');

mysql_select_db('mydb');


$result = mysql_query("select distinct assn from compsin order by assn");

echo '<select name="compsin">';

while ($nt= mysql_fetch_assoc($result)) {

echo '<option value="' . $nt['id'] . '">' . $nt['assn'] . '</option>';


}

echo '</select>';

?>
gdaniels is offline
Reply With Quote
View Public Profile
 
Old 04-08-2009, 04:06 PM Re: PHP List Box in HTML Form help
Average Talker

Posts: 17
Name: Gary
Trades: 0
Nullpointer,

Your code works. I first saved it as .html and I would get an empty list box. When I saved it as .php it worked fine. It still bugs me why it wouldn't work as an HTML file with embedded PHP. I'd prefer an HTML file, but can go with it being a PHP file.

Thanks.
gdaniels is offline
Reply With Quote
View Public Profile
 
Old 04-08-2009, 04:50 PM Re: PHP List Box in HTML Form help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The reason it didn't work is because PHP didn't know to parse your file. By default only files with the .php extension are parsed.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-09-2009, 09:36 AM Re: PHP List Box in HTML Form help
Average Talker

Posts: 28
Name: TJ Phippen
Trades: 0
Quote:
Originally Posted by gdaniels View Post
Nullpointer,

Your code works. I first saved it as .html and I would get an empty list box. When I saved it as .php it worked fine. It still bugs me why it wouldn't work as an HTML file with embedded PHP. I'd prefer an HTML file, but can go with it being a PHP file.

Thanks.
Keep it as .php unless you want to do a bit of htaccess work..
__________________
| | TJ PHIPPEN | |
Please login or register to view this content. Registration is FREE

| Definition Redefining Speed, Quality & Satisfaction!
| Call Me: (435) 225-6121 | Email Me:
Please login or register to view this content. Registration is FREE
siforek is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP List Box in HTML Form help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27442 seconds with 12 queries