Author Topic: Populating a dropdown box  (Read 604 times)

Rudiger

  • New Member
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Populating a dropdown box
« on: February 01, 2012, 09:27:58 PM »
I have an HTML form containing a PHP section within it.

PHP Code: [Select]

<html><head><title>Connect to a database</title></head><body>

<
font size="4">Create Form</font>
<
br><br><font size="4">Choose a Module Mark Value</font><br><br>

<
form action="delete1.php" method="post">
<
select name="valuelist">;
<?
php

$con
=mysqli_connect('localhost''username''password') or die('No connection');
mysqli_select_db($con'labuser') or die ('test will not open');

$query="SELECT distinct studentnum from takes order by studentnum asc";

//Run a query
//result_variable=mysqli_query(connection_variable, query_variable)
$result=mysqli_query($con$query) or die (' test will not open');

//Fetch a result row as an associative array, a numeric array, or both
//-
	
If an html tag contains a double quote, a backslash is used 
while ($rows=mysqli_fetch_array($result)){
	
echo 
"<option value=\"".$rows[0]."\">".$rows[0]."</option>";
}
//echo "</select>";

// Close a connection
//mysqli_close(connection_variable)
mysqli_close($con);

?>
<input type="submit" value="Submit">
</form></body></html>


The table it is trying to use 'takes' table, to populate the dropdown box with the 'studentnum' column's values in ascending order.

But when I try to run it, the following occurs in the browser. The dropdown box does not populate, and inbetween the unpopulated dropdown box and the “Submit” button is the following:
Quote
"; // Close a connection //mysqli_close(connection_variable) mysqli_close($con); ?>

What have I missed that is causing no values to populate the dropdown box? And for PHP code to spill onto the HTML form?

Any help much appreciated. Thanks.

plintu

  • freelance programmer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 246
  • Karma: +5/-0
    • View Profile
    • plintu.com
Re: Populating a dropdown box
« Reply #1 on: February 02, 2012, 07:12:43 PM »
try changing :

PHP Code: [Select]
	

echo 
"<option value=\"".$rows[0]."\">".$rows[0]."</option>";


to:
PHP Code: [Select]
echo "<option value={$rows[0]}>{$rows[0]}</option>";

I believe there is an error in the quotation marks here that is messing with the query
Find an affordable programmer, contact me :D

Rudiger

  • New Member
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Populating a dropdown box
« Reply #2 on: February 04, 2012, 10:14:04 AM »
Tried it, still something wrong.

The dropdown box now contains the following inside it:
Quote
{$rows[0]}
and inbetween the unpopulated dropdown box and the “Submit” button is the following:

Quote
"; } // Close a connection //mysqli_close(connection_variable) mysqli_close($con); ?>

---
Thanks for trying plintu.

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: +5/-0
    • View Profile
Re: Populating a dropdown box
« Reply #3 on: February 04, 2012, 05:27:36 PM »
The option code is fine, but my preference would be to use single quotes to remove the need for escaping:
PHP Code: [Select]
echo '<option value="'.$rows[0].'">'.$rows[0].'</option>';

Not overly important though! Out of interest, what sort of output do you get if you uncomment this line:
PHP Code: [Select]
//echo "</select>";

Because that is the line that comes just before the PHP that gets output.

Rudiger

  • New Member
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Populating a dropdown box
« Reply #4 on: February 06, 2012, 03:17:07 PM »
Smokey,

Fair suggestion, but I get the same output as per my previous post.

And again I get the same output when I uncomment the line you mentioned:

PHP Code: [Select]
//echo "</select>";
I wrote this code two years ago and I actually can't remember why I uncommented the line at the time since it appears that it doesn't change the ouput.

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: +5/-0
    • View Profile
Re: Populating a dropdown box
« Reply #5 on: February 06, 2012, 04:45:46 PM »
Maybe the contents of $row would shed some light on the matter? Have you output the variable inside the while to see if anything strange is going on there?

PHP Code: [Select]
while ($rows=mysqli_fetch_array($result)){
echo 
'<pre>'.print_r($rows,true).'</pre>';
//echo "<option value=\"".$rows[0]."\">".$rows[0]."</option>";
}


You will want to comment the starting select tag as well for this.

Rudiger

  • New Member
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Populating a dropdown box
« Reply #6 on: February 07, 2012, 01:49:20 PM »
Smokey, just to go back to your previous post rergarding commenting out the  This may sound odd but, instead of commenting out the:

PHP Code: [Select]
//echo "</select>";

...I removed it which changed the output so I now see the following in the browser:

An unpopulated dropdown box with the submit button beside it. Progress? - I'm not sure.

Quote
Maybe the contents of $row would shed some light on the matter? Have you output the variable inside the while to see if anything strange is going on there?

PHP Code: [Select]
while ($rows=mysqli_fetch_array($result)){
echo '<pre>'.print_r($rows,true).'</pre>';
//echo "<option value=\"".$rows[0]."\">".$rows[0]."</option>";
}

You will want to comment the starting select tag as well for this.

Done that (with the Select tags removed as mentioned above ). The output I see is the unpopulated dropdown box with the submit button beside it (same as the output mentioned at the start of this post). The output I see is exactly the same as when the following code is used:

PHP Code: [Select]
while ($rows=mysqli_fetch_array($result)){
echo 
"<option value=\"".$rows[0]."\">".$rows[0]."</option>";
}


Hopefully I explained that well enough. Thanks.

Laffin

  • BCFH
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 112
  • Karma: +5/-0
    • View Profile
Re: Populating a dropdown box
« Reply #7 on: February 07, 2012, 08:29:19 PM »
besides this
Code: [Select]
<select name="valuelist">;should be
Code: [Select]
<select name="valuelist">
I think it's not processing the php code at all. My money is that you have this saved as htm or html

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1599
  • Karma: +26/-2
    • View Profile
Re: Populating a dropdown box
« Reply #8 on: February 07, 2012, 11:14:57 PM »
Silly...  The code <select name="valuelist">;   does NOT end with a semi-colon!!!!!!!

It is an HTML line, not a PHP line.

Also, YOU MUST end all <SELECT> with a </SELECT>  or it will not populate.  Uncomment this line.
( echo "</select>"; >

And, if it does not work, repost your code so we can see the latest version with these two lines fixed.

Lastly, use this trick...  RIGHT-CLICK on the output and VIEW-SOURCE.  Then, you can see exactly where your code is failing.  You can see if the select "block" is in place and what is missing from the code.  Wherever the code ends, that is where you error is.    Let us know the results...

Rudiger

  • New Member
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Populating a dropdown box
« Reply #9 on: February 09, 2012, 07:38:43 AM »
Thanks  ErnieAlex and Laffin. Problem solved.

Quote
I think it's not processing the php code at all. My money is that you have this saved as htm or html

That seems to have been the problem. Sorry for not being clearer about that in my first post. I thought I could do it from an html page with PHP code inside the page, but thanks again.

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1599
  • Karma: +26/-2
    • View Profile
Re: Populating a dropdown box
« Reply #10 on: February 09, 2012, 09:07:43 AM »
One further note on your last comment, PHP is run SERVER-SIDE.  But, it is handled before the browser sees it.  So, actually you CAN run your PHP on your HTML.  But, it is when it runs and how to understand.

   The PHP code is totally completed before the page is sent CLIENT-SIDE to the browser. So, any items pulled from the database or calculated by formulas are all handled and then added into the html. 

   Next, the code is send to the browser and rendered by the various parts of HTML and CSS, etc.

   Also, CLIENT-SIDE, Javascript kicks in to be used "LIVE" on the browser. 

I find that a lot of PHP programmers forget these facts.  Especially on this site.  So, I repeat them a lot whenever someone says "I can't" or "thought I could do..." .  Usually you can do anything!  Just takes a little thought and preliminary layout...