12 Replies - 1352 Views - Last Post: 02 December 2011 - 02:59 PM Rate Topic: -----

#1 warbie118  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 08:59 AM

I am having trouble populating my drop down box with data from my database, my database is called cocamp and the table i want to use within that is called district and within that table i want to show name. This is on a registration form where the user select what district they are from, when registered they are put into a table on the database called person which includes what district they are from in the form of a districtID, so when the user registers using the district name i need it to be turned into the relevent DistrictID and put into the person table.

I have tried everything and looked at help all over the net but everytime my drop down doesn't have anything within it. I will post the code i have at the moment.

<?php
mysql_connect("127.0.0.1", "root", "") or die ("Error , check your server connection.");
mysql_select_db("cocamp");

$dropquery = "SELECT Name FROM district ORDER BY DistrictID DESC";
$dropresult = mysql_query($dropquery) or die(mysql_error());
?>

<p>
<select name="vers">
<?php
while($row = mysql_fetch_array($dropresult)){
echo <"option value=\"".$row['Name']."\">".$row["DistrictID"]."</option>";
}
?>
</select>



Help getting this to finally work would be greatly appreciated as i am going out of my mind trying to get this to work!
Please help :helpsmilie: :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a drop down withing a HTML form using php and MYSql

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 09:10 AM

Hey.

First of all, you need to include every field you want to use in your code in your SELECT statement. In that snippet you are using both "Name" and "DistrictID", but your SELECT only fetches Name.

Second, the "value" attribute of the <option> tag is what PHP will receive when you submit the form. What is between the <opion> tag and the </option> end tag is only used as a label. So if you want "DistrictID" to be what PHP gets, you need to put that in the "value" attribute and "Name" in between the tags.
Was This Post Helpful? 1
  • +
  • -

#3 warbie118  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 09:15 AM

Thank You Atli for your help but i am still getting an empty drop down :(

<?php
mysql_connect("127.0.0.1", "root", "") or die ("Error , check your server connection.");
mysql_select_db("cocamp");

$dropquery = "SELECT Name, DistrictID FROM district ORDER BY DistrictID DESC";
$dropresult = mysql_query($dropquery) or die(mysql_error());
?>

<p>
<select name="vers">
<?php
while($row = mysql_fetch_array($dropresult)){
echo <"option value=\"".$row['DistrictID']."\">".$row["Name"]."</option>";
}
?>
</select>


Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 10:05 AM

Here's how I'd do it:
<?php
$form = file_get_contents("theform.html");
$data = read_data();
$with = "";
foreach($data as $value) {
	$with .= '<option value="'.$value.'">'.$value.'</option>';
}
$replace = "PHPOPTIONS";
echo str_replace($replace, $with, $form);


function read_data() {
	// A function to read data from somewhere. Possibly a database. I don't really care, and neither should you. Allyou care about is returning the correct data. Here I'm using a list of states as an example.
	$array = explode("|","AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NC|ND|NE|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY");
	return $array;
}
?>

theform.html:
<select name="vers">
PHPOPTIONS
</select>

Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 10:14 AM

View Postwarbie118, on 02 December 2011 - 04:15 PM, said:

Thank You Atli for your help but i am still getting an empty drop down :(

I see noting wrong with the code, so the only thing that would be causing that is if the SELECT is returning nothing. Try executing the query on the database directly, or using mysql_num_rows to print the number of rows retrieved into you page, just to make sure you are getting the result you are expecting.
Was This Post Helpful? 0
  • +
  • -

#6 warbie118  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 11:36 AM

Have tried the query directly into the database and it returns what i expected, i have no idea what is wrong with it
Was This Post Helpful? 0
  • +
  • -

#7 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 11:49 AM

Your echo is wrong. You are doing:

echo <"option value=\"".$row['DistrictID']."\">".$row["Name"]."</option>";



You have the < before the first "
Was This Post Helpful? 3
  • +
  • -

#8 warbie118  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 11:57 AM

It works now :bigsmile: Thank you all so much for your quick help!
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 02:15 PM

Nice catch Duckington. I completely missed that one! :)
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 02:22 PM

View PostCTphpnwb, on 02 December 2011 - 05:05 PM, said:

	$with .= '<option value="'.$value.'">'.$value.'</option>';


Strictly speaking you don't need to include the "value" attribute if it's value is the same as the contents of the option element. It will default to the contents if the value attribute is missing.

... not that it really matters though. Unless you're really starved for bandwidth :)
Was This Post Helpful? 0
  • +
  • -

#11 warbie118  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 11

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 02:24 PM

I thought it was working completely but have just checked the database and the District ID in the persons table is being set to "0" regardless of what i choose?

<?php
mysql_connect("127.0.0.1", "root", "") or die ("Error , check your server connection.");
mysql_select_db("cocamp");

$dropquery = "SELECT Name, DistrictID FROM district ORDER BY DistrictID";
$dropresult = mysql_query($dropquery) or die(mysql_error());
?>

<p>
<label for="strDistrict">Please select which District you are from</label>
<select name="district">
<?php
while($row = mysql_fetch_array($dropresult)){
echo "<option value=\"".$row['DistrictID']."\">".$row["Name"]."</option>";
}
?>
</select>



I also have a Php file which deals with the form, the code for this is:

<?php

// connect to database

$connect = mysql_connect("127.0.0.1", "root", "") or die ("Error , check your server connection.");

mysql_select_db("cocamp");

 

//Get data in local variable

$person_Forename=$_POST['strFirstname'];

$person_Surname=$_POST['strSurname'];

$person_Email=$_POST['strEmail'];

$person_Password2 =$_POST['strPassword'];

$person_DistrictID=$_POST['strDistrict'];

 

// check for null values

if ($_POST['strFirstname']==""  or $_POST['strPassword']=="")


print("<script language = 'javascript'>alert('All fields must be entered, hit back button and re-enter information');</script>");

else if ($_POST['strPassword'] != $_POST['strPasswordconfirm'])


print("<script language = 'javascript'>alert('The passwords you entered did not match');</script>");



else{

$query="insert into person(Forename,Surname,Email,DistrictID,Password2) values('$person_Forename','$person_Surname','$person_Email','$person_DistrictID','$person_Password2')";

mysql_query($query)  or die(mysql_error());

	?>
	
	print("<script language = 'javascript'>alert('You Have Been Registered');</script>") ;
	<?php
	header("Location: home.php");
	

}
?>



There are a few things i would like to do within that also,

to check the database if the email has already been registered and also at the end of the file i have a pop up box to say registration is complete then i want it to redirect to the homepage but with the code i have now it completely ignores the pop up window. Mainly though i need the DistrictID working

Thanks in advance for your help.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 02:40 PM

View PostAtli, on 02 December 2011 - 05:22 PM, said:

View PostCTphpnwb, on 02 December 2011 - 05:05 PM, said:

	$with .= '<option value="'.$value.'">'.$value.'</option>';


Strictly speaking you don't need to include the "value" attribute if it's value is the same as the contents of the option element. It will default to the contents if the value attribute is missing.

... not that it really matters though. Unless you're really starved for bandwidth :)

True.
I'd normally have an associative array where the value might be more descriptive than the key. In that case the replacement value would be like this:
	$with .= '<option value="'.$key.'">'.$value.'</option>';


Was This Post Helpful? 0
  • +
  • -

#13 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Creating a drop down withing a HTML form using php and MYSql

Posted 02 December 2011 - 02:59 PM

View Postwarbie118, on 02 December 2011 - 09:24 PM, said:

<select name="district">


$person_DistrictID=$_POST['strDistrict'];


See the problem? :)


View Postwarbie118, on 02 December 2011 - 09:24 PM, said:

and also at the end of the file i have a pop up box to say registration is complete then i want it to redirect to the homepage but with the code i have now it completely ignores the pop up window.

The print command that is meant to send the Javascript code to the page is not within a <?php ?>, so it will be put into the HTML as is without being actually printed by PHP.

Also, the header command that is meant to redirect will not function in this manner. All headers must be set before any output. Even a single white-space before the opening <?php block will cause it to fail. - Seeing as you are already using Javascript for the alert, consider using Javascript to do the redirecting instead. You can do: window.location = "..." to cause it to redirect.

This post has been edited by Atli: 02 December 2011 - 03:01 PM
Reason for edit:: Clarifying.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1