Jump to content

Recommended Posts

hey guys i got to the part were it populates my select box.but  i need to echo what i select?

 

<?
// Connect database
mysql_connect("localhost","techker_","o");
mysql_select_db("o_test");
?>
<select name="categoryID">
<?
$sql = "SELECT name FROM authuser ".
"ORDER BY name";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['name']."\">".$row['name']."\n  ";
}
?>
</select>

Link to comment
https://forums.phpfreaks.com/topic/128291-select-box-help/
Share on other sites

its good but i need the part were it gets the select name?

You need to be more clear on what it is you are trying to do.

 

a) are you trying to pass the variable from the page where you created the select box to the next page?

b) user selects option then in a random spot populates what they selected (alert w/ js) or what?

 

Link to comment
https://forums.phpfreaks.com/topic/128291-select-box-help/#findComment-664587
Share on other sites

Well, your select isn't in a form (that I can see from the code you posted) and you need a submit button.  Then the person selects the username and clicks submit to send it somewhere for processing.  During that processing you select the user info from the db.

 


<form name="myform" action="<?php echo $_SERVER['php_self']; ?>" method="post">
<?php
// Connect database
mysql_connect("localhost","techker_","o");
mysql_select_db("o_test");
$username = isset($_POST['categoryID']) ? mysql_real_escape_string($_POST['categoryID']) : "";
?>
<select name="categoryID">
<?php
$sql = "SELECT name FROM authuser ".
"ORDER BY name";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['name']."\">".$row['name']."\n  ";
}
?>
</select>
<input type="submit" />
</form>
<?php
if(!empty($username))
{
    $q = mysql_query("SELECT * FROM `users` WHERE `username`='".$username."'")
    $r = mysql_fetch_array($q);
    echo $r['username'];
    //rest of your user data....
}
?>

Didn't test it but hopefully you can see whats going on...

Also, if you are selecting the username, Im not sure why you are calling it categoryID in this line: <select name="categoryID">

Link to comment
https://forums.phpfreaks.com/topic/128291-select-box-help/#findComment-664600
Share on other sites

hey thanks dude i just got to debug it..the select box part works fine but the las part

 

<?php

if(!empty($username))

{

    $q = mysql_query("SELECT * FROM `authuser` WHERE `username`='".$username."'")

    $r = mysql_fetch_array($q);

    echo $r['username'];

    //rest of your user data....

}

?>

 

gives me errors

 

 

Link to comment
https://forums.phpfreaks.com/topic/128291-select-box-help/#findComment-664908
Share on other sites

<?php

if(!empty($username))

{

    $q = mysql_query("SELECT * FROM `authuser` WHERE `username`='".$username."'")

    $r = mysql_fetch_array($q);

    echo $r['username'];

    //rest of your user data....

}

?>

why would you want to echo $r['username'] from the db when you already have the usersname from you select box ('categoryID')?

Link to comment
https://forums.phpfreaks.com/topic/128291-select-box-help/#findComment-665002
Share on other sites

not sure if this will help, but if you want to see the value you are getting from the select box put in a javascript function & call like this:

 

<form name="myform" action="<?php echo $_SERVER['php_self']; ?>" method="post">
<?php
// Connect database
mysql_connect("localhost","techker_","o");
mysql_select_db("o_test");
$username = isset($_POST['categoryID']) ? mysql_real_escape_string($_POST['categoryID']) : "";
?>
<select name="categoryID" id="categoryID" onChange="testSelect();">
<?php
$sql = "SELECT name FROM authuser ".
"ORDER BY name";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['name']."\">".$row['name']."\n  ";
}
?>
</select>
<input type="submit" />
</form>
<script>
function testSelect() {
  alert(document.getElementById('categoryID').value);
}
</script>

 

Notice I added id="categoryID" to you select and onChange="testSelect();"  I use js to help debug a lot to make sure I am getting the output I want

Link to comment
https://forums.phpfreaks.com/topic/128291-select-box-help/#findComment-665017
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.