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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Passing Drop Down Menu Selected Value to PHP

    Hi, all

    New here and hoping you can help. I'm trying to figure out the correct event handler and syntax (and whether or not to use a header script) to pass the selected value from a dropdown menu back to PHP.

    In a nutshell, here's my process: PHP creates an HTML form containing a drop down menu populated with names and email addresses, email being the option value. This is followed by an input box where the user will select a name from the list, enter an amount into the input box, then submit the whole form with an image button, where this data will be passed back to PHP for insertion into the database.

    The amount portion works beautifully. I can hit the database every time. On the other hand, I cannot get the name/email selection from the drop down menu to be recognized no matter what I do. It tells me nothing is selected even when it is. Well, that isn't entirely true - the closest I've come was apparently screwing up a portion of the PHP code which generates the form and somehow received a Forbidden error when the form tried to link me to the email address. I've never been so happy to be forbidden - or receive an error - in my life. LOL At least that meant I was finally being heard, even if not in the way I want or need.

    I've tried onChange, onClick, onSubmit and can't seem to hit the right combo. Here's a look at one scenario of many that I've tried. I'd greatly appreciate it if you could steer me in the right direction!

    PHP to create form:

    PHP Code:
    $data =   array('form' => draw_form('customer_dropdown'LINK_TO_ACTION'page=' $_GET['page'] . '&action=newrecord'). html_entity_decode(draw_drop_down_menu('customers_email_address',$customers$_GET['customer'], 'onSubmit="return dropdown(this.customers_email_address)"') .'<input type="hidden" name="selected_entry" value=""><input type="submit" value="" style="display: none;">')); 
    HTML & Java:

    Code:
    <html>
    <head>
    <SCRIPT TYPE="text/javascript">
    <!--
    function dropdown(mySel)
    {
    var myWin, myVal;
    myVal = mySel.options[mySel.selectedIndex].value;
    if(myVal)
       {
       if(mySel.form.target)myWin = parent[mySel.form.target];
       else myWin = window;
       if (! myWin) return true;
       myWin.location = myVal;
       }
    return false;
    }
    //-->
    </SCRIPT>
    </head>
    
    
    
    <form name="customer_dropdown" action="http://site.com/action.php?page=1&action=newrecord" method="post"><input type="hidden" name="securityToken" value="randomSequence01234567890">
    <select rel="dropdown" name="customers_email_address" onSubmit="return dropdown(this.customers_email_address)">
    <option value="" selected="selected">Please Select</option>
    <option value="email1@email.com">Lastname, Firstname (email1@email.com)</option>
    <option value="email2@email.com">Lastname, Firstname (email2@email.com)</option>
    <option value="email3@email.com">Lastname, Firstname (email3@email.com)</option>
    <option value="email4@email.com">Lastname, Firstname (email4@email.com)</option>
    <option value="email5@email.com">Lastname, Firstname (email5@email.com)</option>
    </select>
    <input type="hidden" name="selected_entry" value=""><input type="submit" value="" style="display: none;">
    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td align="left" class="formContent"><br>Enter Amount:<br><input type="text" name="amount" value="0.00" /></td>
      </tr>
      <tr>
        <td align="center" class="formContent"><br><input type="image" src="http://site.com/images/buttons/button_insert.gif" border="0" alt="Insert" title=" Insert ">&nbsp;<a href="http://site.com/action.php?page=1&gid="><img src="http://site.com/images/buttons/button_cancel.gif" border="0" alt="Cancel" title=" Cancel "></a></td>
      </tr>
    </table>
    
    </form>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    There is no such thing as an ONSUBMIT for a <select>. ONCHANGE would usually make the most sense, but I really don't understand why you need *ANY* event handler for it.

    I don't pretend to understand what your dropdown() function is trying to do.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow, and I thought I might even get extra credit for the very clear way I asked my question.

    As I tried to explain apparently not so clearly, I'm trying to pass the value of the drop down menu selection and the value of the input box back to PHP for insertion into the database. I'm doing fine with the amount. I'm getting nowhere with the drop down menu.

    Not sure what code I do need, since as you said and I'd already pointed out, nothing I've tried is working properly. Any code suggestions would be greatly appreciated.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    ??? You mean you are trying to pass back the TEXT of the <option>, as well as the value=???

    That is, in your example, if the first <option> is selected you want to pass back *BOTH*
    email1@email.com
    *AND*
    Lastname, Firstname (email1@email.com)

    ???

    If so, the easy way to do this is to simply OMIT the value= from the <option>s.

    Code:
    <form action="http://site.com/action.php?page=1&action=newrecord" method="post"><input type="hidden" name="securityToken" value="randomSequence01234567890">
    <select name="customers_email_address">
        <option value="" selected="selected">Please Select</option>
        <option>Lastname, Firstname (email1@email.com)</option>
        <option>Lastname, Firstname (email2@email.com)</option>
        ...
    </select>
    ...
    Enter Amount:<br><input type="text" name="amount" value="0.00" />
    <input type="image" src="http://site.com/images/buttons/button_insert.gif" border="0" alt="Insert" title=" Insert ">
    ...
    </form>
    Then, in your PHP code, you can simply do
    Code:
    $temp = preg_split( "[\\(|\\)]", $_POST["customers_email_address"] );
    $name = $temp[0];
    $email = $temp[1];
    I don't use PHP, so I'm not sure that's the right regular express for preg_split, but it should be close.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    But I still don't understand what the dropdown() function was supposed to do.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No, sir, that isn't quite what I mean, although I appreciate the suggestion.

    The option value is tied to the email address. The name/text is merely window dressing. All I need is the correct JavaScript code to grab the email value and send it to PHP along with the amount when the image Insert button is clicked.

    I, too, thought onChange would be correct, hence setting up the selected_entry hidden input value, but that results in no selection being recognized, whereas the Forbidden error came from using onSubmit.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    In that case, you really do need nothing at all.
    Code:
    <form action="http://site.com/action.php?page=1&action=newrecord" method="post">
    <input type="hidden" name="securityToken" value="randomSequence01234567890">
    <select name="customers_email_address">
        <option value="" selected="selected">Please Select</option>
        <option value="email1@email.com">Lastname, Firstname (email1@email.com)</option>
        <option value="email2@email.com">Lastname, Firstname (email2@email.com)</option>
        ....
    </select>
    ...
    Enter Amount:<br><input type="text" name="amount" value="0.00" />
    <input type="image" src="http://site.com/images/buttons/button_insert.gif" border="0" alt="Insert" title=" Insert ">
    ...
    </form>
    And your PHP code is simply
    Code:
    $email = $_POST["customers_email_address"];
    Honest, nothing more is needed.

    You don't *WANT* to submit the form when the <select> is used, because the person may not have yet entered a value for the amount. So you only want it submitted when the submit button (in your case, an image button) is pressed. Keep it simple.

    I should point out that because your URL in the action= has "?page=1&action=newrecord", you will *NOT* be able to get those values from $_POST in PHP. I assume you knew that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    DiamondCiara (12-20-2010)

  • #8
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Typical me, trying to overthink things. Thank you so much, Old Pedant. I'm fairly new to coding and didn't know the extra parameters in the action link would prevent me from accomplishing this. I know you said you don't use PHP, so I'm not sure if you would know how I could get around that. Of course, the action link directs to the PHP switch case where the database insertion is done. Ugh.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Oh, you can leave the parameters there! It's just that you'll have to use $_GET["page"] and $_GET["action"] for those, instead of $_POST[]. If I recall correctly, you can also simply use $_REQUEST["xxx"] if you don't *care* whether a parameter was passed via the query string or via POST. Let me check...

    Yep... http://php.net/manual/en/reserved.variables.request.php

    I don't use PHP, but after reading tons of posts here and in the MySQL forum, I can at least read it and I sure know how to RTFM. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    You *could* pass those as hidden form fields, and thus read them via $_POST:
    Code:
    <form action="http://site.com/action.php" method="post">
    <input type="hidden" name="page" value="1" />
    <input type="hidden" name="action" value="insert" />
    ...
    Then you can treat those the same as any other posted field. $_POST["page"], etc.

    There is a potential problem with using a form field named "action" in that it might interfere with the <form> property of that name. But should be okay if you never use JavaScript code to refer to the form.action property.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    LOL, Old Pedant! Luckily I can too or I wouldn't have made it this far.

    Many, many thanks again for helping me. You did scare me - a couple of times hehe! - but I'm glad to know I can easily work around this.

    Have a wonderful day. Back to the drawing board I go!

    Edited to say thanks for the additional suggestion. You're fast - and I love it!
    Last edited by DiamondCiara; 12-20-2010 at 08:47 PM.

  • #12
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi again,

    Been playing with this all day and I'm still not getting anywhere. I've tried both no-JavaScript suggestions (with hidden inputs and without) as well as changing $_POST and $_GET in both the form and PHP retrieval (I also tried $_REQUEST) yet the same thing is happening, it tells me no customer is selected.

    What can I be doing wrong? The amount goes through perfectly if I eliminate the pre-check for both fields to contain data before it goes to the database. The only thing I can think is that I really do need JavaScript, because it really isn't one form - it's two forms with one submit button.

    If that's the case, then I'm back to square one, needing the proper code for my header and my form. Thank you again!

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    OOPS!!! I did *NOT* see that it was two forms!!!

    WHY is it two forms????

    ***********

    I went back and looked at your post #1, and there is only one <form> in there. Where do you see another <form>???

    Anyway, why would you need/want it to be two <form>s??

    Yes, you would need some JS if it's two forms, but I can't see a reason for two forms. And you certainly only showed one <form> in that first post.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Registered User
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It's one form for HTML output purposes, but if you'll look at the PHP code I posted which creates it, it's actually a form-within-a-form. For some reason, the environment this script will go into won't let me separate them. Believe me, I tried. Several ways!

    Again, I'm not the most experienced coder, so there could easily be a better way I don't yet know. I'm all ears and a giant sponge at this point.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    You mean this code:
    Code:
    $data = 
     array('form' => draw_form(
         'customer_dropdown', 
         LINK_TO_ACTION, 
         'page=' . $_GET['page'] . '&action=newrecord')
             . html_entity_decode(draw_drop_down_menu(
                    'customers_email_address',$customers, $_GET['customer'], 
                    'onSubmit="return dropdown(this.customers_email_address)"' ) 
            . '<input type="hidden" name="selected_entry" value="">'
            . '<input type="submit" value="" style="display: none;">')
          );
    I only see one form there. Just one call to draw_form.

    Ahhh...wait...your amount form field isn't there!

    So you must be doing something else to create it, and that's the problem??

    draw_form and draw_drop_down_menu are *NOT* standard PHP functions, so you must be using somebody's library. I just googled for it and can't figure out where it's coming from. You maybe need to point us to what resource library you are using? And/or ask these questions of the person who provided the library.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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