...

View Full Version : Passing Drop Down Menu Selected Value to PHP



DiamondCiara
12-20-2010, 07:33 PM
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:


$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:



<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>

Old Pedant
12-20-2010, 07:40 PM
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.

DiamondCiara
12-20-2010, 07:47 PM
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.

Old Pedant
12-20-2010, 07:55 PM
??? 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.



<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

$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.

Old Pedant
12-20-2010, 07:57 PM
But I still don't understand what the dropdown() function was supposed to do.

DiamondCiara
12-20-2010, 08:03 PM
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.

Old Pedant
12-20-2010, 08:12 PM
In that case, you really do need nothing at all.


<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


$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.

DiamondCiara
12-20-2010, 08:25 PM
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.

Old Pedant
12-20-2010, 08:34 PM
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/>

Old Pedant
12-20-2010, 08:39 PM
You *could* pass those as hidden form fields, and thus read them via $_POST:


<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.

DiamondCiara
12-20-2010, 08:44 PM
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! ;)

DiamondCiara
12-21-2010, 12:13 AM
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!

Old Pedant
12-21-2010, 12:37 AM
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.

DiamondCiara
12-21-2010, 12:55 AM
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. :)

Old Pedant
12-21-2010, 01:14 AM
You mean this 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.

DiamondCiara
12-21-2010, 04:07 AM
You were right when you said it shouldn't be two forms.

Originally, it was two form functions combined into one array, followed by a text array for the input box. The first form function entered the HTML for the beginning of the form, as well as held the action, while the second was creating the select/dropdown menu portion.

Once I realized why I had been unsuccessful in prior attempts to rearrange them, I was able to break them apart and eliminate the need for the first function, so now there is indeed only one form for the dropdown menu both in PHP and HTML. The input box is still a text array, which has always functioned properly when activated by the image buttons, which are in a text array below that.

All seems well structurally, yet I'm still getting no customer selected. LOL!

Old Pedant
12-21-2010, 07:05 PM
Bring up the page in your browser.

Click on the VIEW menu of the browser.

Click on the SOURCE (or PAGE SOURCE, depending on browser) menu item.

That will show you the HTML view of the page, not the PHP code.

Copy/paste everything from the <form> to the </form> tag here, for us to see.

DiamondCiara
12-21-2010, 07:42 PM
You must be psychic, OP. I was just going to post again and beg. LOL!

Here's the code as it stands now. PHP to create, HTML that displays.


$data = array('form' =>'<form name="customer_dropdown" id="customer_dropdown" method="post" action="'. url_link(LINK_TO_ACTION, 'page=' . $_GET['page'] . '&action=newrecord"> ') . html_entity_decode(draw_drop_down_menu('customers_email_address', $customers, $_GET['customer']).'<input type="hidden" name="page" value="1"><input type="hidden" name="action" value="newrecord"><input type="submit" value="" style="display: none">'));

$data[] = array('align' => 'left', 'text' => '<br>' . TEXT_ADD_NEW_AMOUNT . '<br>' . draw_input_field('amount', number_format($gInfo->amount, 2, '.', '') ));

$data[] = array('align' => 'center', 'text' => '<br>' . image_submit('button_insert.gif', IMAGE_INSERT) . '&nbsp;<a href="' . url_link(LINK_TO_ACTION, 'page=' . $_GET['page'] . '&cid=' . $customers->customers_id) . '">' . image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');



<form name="customer_dropdown" id="customer_dropdown" method="post" action="http://site.com/somepage.php?page=1&action=newrecord"><select rel="dropdown" name="customers_email_address">
<option value="" selected="selected">Please Select</option>
<option value="email1@email.com">Last Name, First Name (email1@email.com)</option>
<option value="email2@email.com">Last Name, First Name (email2@email.com)</option>
<option value="email3@email.com">Last Name, First Name (email3@email.com)</option>
<option value="email4@email.com">Last Name, First Name (email4@email.com)</option>
<option value="email5@email.com">Last Name, First Name (email5@email.com)</option>
</select>
<input type="hidden" name="page" value="1"><input type="hidden" name="action" value="newrecord"><input type="submit" value="" style="display: none">
<table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr>
<td align="left" class="data"><br>Enter New Amount:<br><input type="text" name="amount" value="0.00" /></td>
</tr>
<tr>
<td align="center" class="data"><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/somepage.php?page=1&cid="><img src="http://site.com/images/buttons/button_cancel.gif" border="0" alt="Cancel" title="Cancel"></a></td>
</tr>
</table>
</form>

Thank you again for trying to help me. I really appreciate it!

Old Pedant
12-21-2010, 07:57 PM
Well, first of all, you should *NOT* have BOTH


action="http://site.com/somepage.php?page=1&action=newrecord"
in the <form> *and* have

<input type="hidden" name="page" value="1">
<input type="hidden" name="action" value="newrecord">

One or the other, not both. Though if the page uses $_POST or $_GET (and not $_REQUEST) it's not going to hurt to have both.

But no, I see absolutely nothing wrong with that code.

In fact, if I change that URL to point at a page of my own, I get the info just fine.

I don't suppose you can show the PHP code for that page in the URL:


http://site.com/somepage.php
???

I have to suspect that the problem is with whatever library it is that you are using. That code that processes your call to draw_menu and the rest. I don't suppose you can show us the URL of the providers of that library?? Or is it something home-grown?

DiamondCiara
12-21-2010, 08:29 PM
It's actually the same page. I'm just directing to another action, which will add this information to the database. Happy to post it if it will help.

I'm not convinced the library is the issue because, as I mentioned, I did get the customer selection once, when I'd apparently made a boo-boo in the PHP code to create the form. Clicking the submit button tried to link me to the customer's email address - the customer I'd selected from the menu.

I've tried to duplicate that mistake just for comparison, but I haven't been able to hit it. I recall it was something interfering with the beginning form HTML, which chopped off part of "method=post" and printed some of it as text below the menu. I had also used JavaScript in the code at that point, whereas now it's JS-free.

Any JavaScript ideas we can try, just to be sure?

Old Pedant
12-21-2010, 08:34 PM
I'm not convinced the library is the issue ...

I don't mean there's an error in the library. Just that you might not be using it as intended.

I say again: When I test that on my own system, it works like a charm.

Oh, heck. I'll prove it to you.

Hang on...

Old Pedant
12-21-2010, 08:45 PM
Here:
http://www.plopon.com/ciara.html

I removed your <a> link and used my own image for the insert button image.

And of course changed the action= of the <form>

But look at it; even do a VIEW==>>SOURCE on it.

Now select one of the dropdown values and enter an amount and click on the image.

See? A full dump of the GET and POST values.

'nuff said?

DiamondCiara
12-21-2010, 09:16 PM
Interesting! Thank you very much for setting that up. It's mystifying why I can't get it to work on my side. Also gratifying to know at least my code is correct. Thank you again, OP. I guess I'm going to have to ask them after all. There goes the surprise! ;)

Many thanks again for all of your help. I've learned a lot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum