Jump to content

Database table to be incorporated into HTML form value=""


genzedu777

Recommended Posts

Hi,

 

I realised that it is possible to populate SQL tables VALUES out into html form?

 

Example

1) I have a table call 'country' in my database

2) In html, I have a

 

<form>

<input name="country" type="checkbox" id="Germany" value="Germany">

      <label for="Germany">Germany</label>

</form>

 

3) If you realised, my value is currently value="Germany"

 

4) My question is, how can I literally pull out the value of "Germany" from my 'location' table and place it in my html form automatically, without me typing out the value for every single country.

 

5) Just imagine I have 50+ countries, I will require to type out 50 times the countries' name personally in the value="", is there a way to populate the 'location' table into the value="", thereafter user selects the country through what was populated from the database 'location' table and saved it under the user_id.

 

 

I'm not sure if you guys know what I am talking about, but will appreciate any help. Thanks

 

Wilson

Link to comment
Share on other sites

Find code

//I hope you connect with Mysql
$sql = mysql_query("SELECT * FROM country ORDER BY country_name ASC");
echo "<form>";
while($data = mysql_fetch_array($sql))
{
echo '<input name="country" type="checkbox" id="'.$data['country_name'].'" value="'.$data['country_name'].'">
      <label for="'.$data['country_name'].'">'.$data['country_name'].'</label>';
}
echo "</form>";

Please let me know if you have any error.

Link to comment
Share on other sites

Hi, I have a question regarding about this,

 

I may call forth the country name to appear in my HTML form, however the value which I would like them to capture is the country_id, instead of the country_name. Will it complicate the code, is it feasible? Thanks

 

 

$sql = mysql_query("SELECT * FROM country ORDER BY country_name ASC");

echo "<form>";

while($data = mysql_fetch_array($sql))

{

echo '<input name="country" type="checkbox" id="'.$data['country_name'].'" value="'.$data['country_id'].'">

      <label for="'.$data['country_name'].'">'.$data['country_name'].'</label>';

}

echo "</form>";

Link to comment
Share on other sites

oh okay,

 

Allow me to explain it properly.

 

In the database there is a "country" table. It has coulums of country_id and country_name.

 

<input name="country" type="checkbox" id="'.$data['country_name'].'" value="'.$data['country_id'].'">

<label for="'.$data['country_name'].'">'.$data['country_name'].'</label>

 

"country" table is a many-many relationship with "user" table, hence I thought of grabbing the values in numeric form, and place it in a new table called "country_user", that is the reason why I would like the return value to be in numbers instead of words.

 

Do you roughly understand what I am saying? Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.