HTML and PHP question (not assinging any value)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amit

    HTML and PHP question (not assinging any value)


    Hello group,

    I have switched to linux and PHP so totally new to this field. Please
    help me with my question. I have an HTML code which contains a
    drop-down control on it using
    <select name=selected_g roup>
    <option> Gropu 1</option>
    <option> Group 2</option>
    and ...
    </select>
    <?php
    $group = $_POST['selected_group '];
    ?>

    meantime, I'm my php code using $group = $_POST['selected_group '] but
    nothing will assinged to the $group variable. I tried different html
    codes which were on web for learning purpose but didn't work out!
    please let me know if you have any suggestions.

    Thanks,
    Amit

  • Roy W. Andersen

    #2
    Re: HTML and PHP question (not assinging any value)

    amit wrote:[color=blue]
    > <select name=selected_g roup>
    > <option> Gropu 1</option>
    > <option> Group 2</option>
    > and ...
    > </select>[/color]

    Each option needs to have a value assigned. The text between <option>
    and </option> is only visual and is not used for the actual form-data
    when submitting it to the server. Hence, the code quoted above will
    submit an empty value for "selected_group " regardless of what's actually
    selected.

    This is how you should do it:

    <select name="selected_ group">
    <option value="Group 1">Group 1</option>
    <option value="Group 2">Group 2</option>
    </select>

    This way, when the form is submitted, what you've defined in the
    value-parameter is what is actually sent to the server.


    Roy W. Andersen
    --
    ra at broadpark dot no / http://roy.skyggenesdal.org/

    "Hey! What kind of party is this? There's no booze
    and only one hooker!" - Bender, Futurama

    Comment

    • David Haynes

      #3
      Re: HTML and PHP question (not assinging any value)

      amit wrote:[color=blue]
      > Hello group,
      >
      > I have switched to linux and PHP so totally new to this field. Please
      > help me with my question. I have an HTML code which contains a
      > drop-down control on it using
      > <select name=selected_g roup>
      > <option> Gropu 1</option>
      > <option> Group 2</option>
      > and ...
      > </select>
      > <?php
      > $group = $_POST['selected_group '];
      > ?>
      >
      > meantime, I'm my php code using $group = $_POST['selected_group '] but
      > nothing will assinged to the $group variable. I tried different html
      > codes which were on web for learning purpose but didn't work out!
      > please let me know if you have any suggestions.
      >
      > Thanks,
      > Amit
      >[/color]

      The value of 'selected_group ' is only assigned to the $_POST value when
      the form submit action is invoked and the form is using a POST method.

      So:
      1. is your <select> within a <form>?
      2. does the <form> have method="POST"?
      3. does the <form> also contain an <input type="submit"> or onchange= to
      the select?

      If not, $_POST['selected_group '] will not be set and $group will be empty.

      -david-

      Comment

      • Tim Roberts

        #4
        Re: HTML and PHP question (not assinging any value)

        "Roy W. Andersen" <roy-news@netgoth.or g> wrote:
        [color=blue]
        >amit wrote:[color=green]
        >> <select name=selected_g roup>
        >> <option> Gropu 1</option>
        >> <option> Group 2</option>
        >> and ...
        >> </select>[/color]
        >
        >Each option needs to have a value assigned. The text between <option>
        >and </option> is only visual and is not used for the actual form-data
        >when submitting it to the server. Hence, the code quoted above will
        >submit an empty value for "selected_group " regardless of what's actually
        >selected.[/color]

        Bzzzt, sorry, this is incorrect. If no "value" clause is specified, the
        text between <option> and </option> becomes the value for that item.
        [color=blue]
        >This is how you should do it:
        >
        ><select name="selected_ group">
        > <option value="Group 1">Group 1</option>
        > <option value="Group 2">Group 2</option>
        ></select>
        >
        >This way, when the form is submitted, what you've defined in the
        >value-parameter is what is actually sent to the server.[/color]

        Nope. The "value" clauses in your example are entirely unnecessary.
        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        Working...