SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using php variable in html select forms name

    Here's a question for all you great php programmers.
    What I want to do is use a variable username as the name of a select input of a form.
    I.E
    Code:
    echo"<select name='$user'>";
    Of course this code doesn't work though I feel sure that there must be a way to do this.
    Anyone know it?

  2. #2
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want to display the username under select box items?
    or any text box value?

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The name of a <select> is just how you'll refer to that input in your code. The value must come from options.
    HTML Code:
    <select name="my_field">
       <option value="1">Helicopter</option>
       <option value="2">Bus</option>
       <option value="3">Unicycle</option>
    </select>
    $_POST['my_field'] will be 1, 2 or 3 depending on what was selected.

    I doubt you want a dynamically named select field. If you want to preselect a value in a list you need to give that option a selected attribute:

    PHP Code:
    $user 'bob';
    $users = array('helen''alan''bob''michelle''sarah''anna');

    $select "<select name='user'>\n";

    foreach(
    $users as $u) {
       
    $s = ($u == $user) ? 'selected' '';
       
    $select .= "<option value='$u$s>$u</option>\n";
    }
    echo 
    $select "</select>"

  4. #4
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    I doubt you want a dynamically named select field. If you want to preselect a value in a list you need to give that option a selected attribute:
    Actually that IS what I want! I am trying to use a dynamically created name for the select field!

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then what you had originally will work. What HTML does that output?

  6. #6
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is how I am trying to receive what I want
    Code:
    while ($i < $num) {
    $user=mysql_result($result,$i,"user");
    if (isset($_POST['$user'])){
    $level = ($_POST['$user']);
    echo "$level";
    }else{
    echo "$user not recieved";
    }
    $i++;
    }
    Though I get 'user not received'

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awkward looking code, there could be more problems with it, but one is that you're referring to $_POST['$user']

    Those single quotes around $user will cause it to be treated as a literal string, not a variable, so it'll never be set.

    PHP Code:
    $var 'Hello';
    echo 
    '$var'//outputs $var not Hello
    echo $var//outputs Hello 

  8. #8
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course that was the problem.
    Seems to work now.
    Thanks!
    And yes my code may look awkward. If you can suggest a better way I am always open to correction as I am quite new to php and have been learning it from books.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •