Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Splitting values Need help please

    I am trying to capture an id and name in a selection and post them to two different fields.
    Having a hell of a time...

    Here is my dropdown selection example

    Code:
    <option value="1278|NAME">TOY NAME</option>
    And the PHP I am trying to get to work to post both values (ID and TOY NAME, separated by the pipe) to two separate fields...

    Code:
    if ((isset($_POST["frm_toy"]) && !empty($_POST["frm_toy"])));
    $drop_down_field = explode("|", $frm_toy);
    $_POST["ID"] = $drop_down_field[0];
    $_POST["NAME"] = $drop_down_field[1];
    As you php gurus can see, its wrong. And since I am not a PHP guru, I cant see where it is wrong.

    Can anyone help? Would be greatly appreciated

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    You're really close ...

    <select name="frm_toy">
    <option value="1278|NAME">TOY NAME</option>
    .
    .
    .
    </select>

    test it out ....

    Code:
    if ((isset($_POST["frm_toy"]) && !empty($_POST["frm_toy"]))){
    $frm_toy=$_POST['frm_toy'];
    $drop_down_field = explode("|", $frm_toy);
    $id = $drop_down_field[0];
    $name = $drop_down_field[1];
    echo "ID: ".$id."<br>";
    echo "Name: ".$name;
    }
    Last edited by mlseim; 07-30-2013 at 10:38 PM.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    Thanks for that. I already have the select name set.

    Will try this out.
    Just for reference, what is the echo used for?

    Bill

    Quote Originally Posted by mlseim View Post
    You're really close ...

    <select name="frm_toy">
    <option value="1278|NAME">TOY NAME</option>
    .
    .
    .
    </select>

    test it out ....

    Code:
    if ((isset($_POST["frm_toy"]) && !empty($_POST["frm_toy"]))){
    $frm_toy=$_POST['frm_toy'];
    $drop_down_field = explode("|", $frm_toy);
    $id = $drop_down_field[0];
    $name = $drop_down_field[1];
    echo "ID: ".$id."<br>";
    echo "Name: ".$name;
    }

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Not working

    This isnt working. Nothing is coming through

    Any other ideas?

  • #5
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, I got it working. Had to add the $_POST

    Thanks for your help!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There's not enough information here.
    The offset field in the form that you'll get will be listed as the name of the SELECT tag blocking the option. The only result will be that of the option value field. There is no way to retrieve the "toy name" used in the option display.
    If I do as so:
    PHP Code:
    <select name="sel_toy">
        <
    option value="1278|NAME">TOY NAME</option>
    </
    select
    Select that option and submit the form, assuming that its sent via post, I can then do:
    PHP Code:
    if (isset($_POST['sel_toy']))
    {
        list(
    $optNumber$optName) = explode("|"$_POST['sel_toy']);

    Where $optNumber will be 1278 and $optName will be NAME. I'm not sure where $_POST['ID'] and $_POST['NAME'] come into play here; these individually have no direct relationship to the select option.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is what worked for me:

    Code:
    if ((isset($_POST["frm_toy"]) && !empty($_POST["frm_toy"]))){
    $frm_toy=$_POST['frm_toy'];
    $drop_down_field = explode("|", $frm_toy);
    $_POST["toy_id"] = $drop_down_field[0];
    $_POST["toy_name"] = $drop_down_field[1];
    }

  • #8
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And the select form example:
    Code:
    <select name="frm_toy" id="Toy">
    <option value="1278|NAME">TOY NAME</option>
    What I end up with is the toy id and toy name being separated based on the pipe (|), with the id going into toy_id field and Name going into toy_name field.
    Last edited by wslover; 07-31-2013 at 12:29 AM. Reason: Added result

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The only thing I would suggest is to avoid populating the superglobal variables. $_POST['toy_id'] logically belongs to input provided by the user, but this is generated from other data by the system.
    You can also give it a minor trim using the list.
    PHP Code:
    list($toy['id'], $toy['name']) = explode("|"$_POST['frm_toy']); 
    Would give you the 1278 and NAME for $toy['id'] and $toy['name'] respectively.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I will have to try that way also, thanks for the reply.

    Bill

    Quote Originally Posted by Fou-Lu View Post
    The only thing I would suggest is to avoid populating the superglobal variables. $_POST['toy_id'] logically belongs to input provided by the user, but this is generated from other data by the system.
    You can also give it a minor trim using the list.
    PHP Code:
    list($toy['id'], $toy['name']) = explode("|"$_POST['frm_toy']); 
    Would give you the 1278 and NAME for $toy['id'] and $toy['name'] respectively.

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Bill, you are 'over thinking' this ...

    $_POST['toy_id'] is used to get the value from the form ...

    Try this tiny example to see what it does ...

    <?PHP
    echo $_POST['toy_id'];
    ?>

    You don't need to use $_POST for anything else within your script other than to transfer the values from your form to your script.


  •  

    Tags for this Thread

    Posting Permissions

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