Jump to content

Help using Variables in html form.


Xdega

Recommended Posts

Basically atm I am trying to accomplish a way to $_POST a selection from my dropdown box in to a PHP variable for use elsewhere. The eventual goal being a form that will allow you to select a user from the dropdown box; then via a HTML form with some additional text fields, add/update database table records for that person.

 

I have been searching and searching, and I cant find anything that addresses using a dropdown box that pulls names from my SQL in a html web form. All I can find are tons of guides on simple html only forms.

 

At the moment, the dropdown box is working, It is pulling names from my database/table. But when I select a name from the list and then click on my submit button the $_POST does not seem to be pulling the value for "username".

 

I have the following for the index page:

<?php
include('/nav.php');
include('/config.php');
include('/dbopen.php');
echo "Select a name: <select name='$name'>";
$query="select * from players";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$name=$row['PLAYER_NAME'];
echo "<option name=username value='$name'>$name</option>";
}
echo "</select>";
?>
<html>
<body>
<form action="moduser.php" method="post" />
<input type="submit" value="CLICK!" />
</form>
</body>
</html>

 

and the following for a test page to see if my form is being submitted correctly:

<?php
include('/nav.php');
include('/config.php');
include('/dbopen.php');
$username="$_POST[username]";
echo "Hello $username";
?>

 

NOTE:

My includes are tested and working default scripts to connect to database, define some global vars etc.

Should not be relevant to my particular problem

 

Thanks Much,

Dega.

Link to comment
Share on other sites

edit it to your liking :)

 

<? 
... 
mysql cnx code 
... 

$sql="SELECT id, thing FROM table"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $id=$row["id"]; 
    $thing=$row["thing"]; 
    $options.="<OPTION VALUE=\"$id\">".$thing; 
} 
?> 
... 
html code 
... 

<SELECT NAME=thing> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELECT> 
...

Link to comment
Share on other sites

edit it to your liking :)

 

<? 
... 
mysql cnx code 
... 

$sql="SELECT id, thing FROM table"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $id=$row["id"]; 
    $thing=$row["thing"]; 
    $options.="<OPTION VALUE=\"$id\">".$thing; 
} 
?> 
... 
html code 
... 

<SELECT NAME=thing> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELECT> 
...

 

Just be sure to add the 'php' to the <?php or else there are consequences when you use this software on a different server, just means that it's more compatible - just thought I should point that out.

 

In your original example you are building the select box OUTSIDE of the form <> so no wonder it's not getting posted, the only value currently set is $_POST['submit'] and this will have the value of "CLICK!"

 

So as WatsonN points out, build your selection box outside of the form, then echo that var out WITHIN the form tags, then run print_r on the receiver script to see that your DB values are getting through.

 

Rw

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.