# [web] PHP function question

## Recommended Posts

Third    121
I am having trouble coming up with a solution to a problem. I have a function in PHP that builds a select drop down box. The problem is I need to get the select name so that I have the item that the user selects. Would I just go like this: echo "<select name = '$data'>"; and then have the function return$data? Or would that not even assign anything to data since its an echo statement?

##### Share on other sites
Coward    210
<select name=options><option name=1>foo</option><option name=2>bar>/option></select>

When you submit this to a page, you'll be able to read the name (1 or 2) of the option that is selected on submit, through PHP's HTTP_GET_VARS[options] (or post, depending on your form method)

##### Share on other sites
Third    121
Quote:
 Original post by CowardWhen you submit this to a page, you'll be able to read the name (1 or 2) of the option that is selected on submit, through PHP's HTTP_GET_VARS[options] (or post, depending on your form method)

Well what I need to do is return the value through a function in PHP. I'm stuck because I don't know how I could get the value since the way the form is built is I would just pull the value by "$_POST['selectname']. However, since the select box is in a function I don't know how I would do this. #### Share this post ##### Link to post ##### Share on other sites Coward 210 Quote: Original post by Third Quote:  Original post by CowardWhen you submit this to a page, you'll be able to read the name (1 or 2) of the option that is selected on submit, through PHP's HTTP_GET_VARS[options] (or post, depending on your form method) Well what I need to do is return the value through a function in PHP. I'm stuck because I don't know how I could get the value since the way the form is built is I would just pull the value by "$_POST['selectname']. However, since the select box is in a function I don't know how I would do this.

Could you provide a code example?

##### Share on other sites
VerMan    116
I usually do this:

<?php$userOption =$_POST['options'];?><select name=options><option name=1 <?=($userOption == 1)?"selected":""?>>foo</option><option name=2 <?=($userOption == 2)?"selected":""?>>bar</option>...</select>

##### Share on other sites
Third    121
Quote:
Original post by Coward
Quote:
Original post by Third
Quote:
 Original post by CowardWhen you submit this to a page, you'll be able to read the name (1 or 2) of the option that is selected on submit, through PHP's HTTP_GET_VARS[options] (or post, depending on your form method)

Well what I need to do is return the value through a function in PHP. I'm stuck because I don't know how I could get the value since the way the form is built is I would just pull the value by "$_POST['selectname']. However, since the select box is in a function I don't know how I would do this. Could you provide a code example? Sure, here's the function that I created the builds the select box function DisplayCourses($department, $schoolID,$connection, $pad){ echo "<td>"; //This is the part that I need,$data	echo "<select name = '$data'>"; echo "<option value = '1'>$pad</option>";	        ...	echo "</select></td>";        //This doesn't work	return $data;} The part that I need is$data, which is the select name, since this will contain what the user selects.

May I ask what you mean by "return $data" not working? Doesn't it return the string correctly? #### Share this post ##### Link to post ##### Share on other sites Third 121 Quote:  May I ask what you mean by "return$data" not working? Doesn't it return the string correctly?

It doesn't return it because the function is called before the user hits submit which sends the value they selected.

I'll try rephrasing what I want to do. I have a function that builds a select drop down box. I want this function to return the value that the user selects. However this value will not be available until the page after the page on which the function is used. This is because the value is submitted upon the user hitting a submit button which takes them to the next page. My question then is how can I get this value or if its even possible to do it this way.

BTW, thanks for the help so far.

##### Share on other sites
Coward    210
Quote:
 Original post by ThirdIt doesn't return it because the function is called before the user hits submit which sends the value they selected.I'll try rephrasing what I want to do. I have a function that builds a select drop down box. I want this function to return the value that the user selects. However this value will not be available until the page after the page on which the function is used. This is because the value is submitted upon the user hitting a submit button which takes them to the next page. My question then is how can I get this value or if its even possible to do it this way.

Ahh... So what you want to do is dynamicly change the page depending on what the user chooses in the dropdown box? (Show different options depending on what course the user chooses?)

If it is, then PHP isn't realy the way to go. If you realy want to do it like this, you have to submit the page and check if $HTTP_POST_VARS[$data] exists, and if it does write the individual course options. You cannot do all this doing a single function in PHP.

The obvious take on this would be to divide the registration into steps at the places where the next choice would be dependant on the former.

Alternatively, you can look into AJAX.

##### Share on other sites
Third    121
Quote:
Original post by Coward
Quote:
 Original post by ThirdIt doesn't return it because the function is called before the user hits submit which sends the value they selected.I'll try rephrasing what I want to do. I have a function that builds a select drop down box. I want this function to return the value that the user selects. However this value will not be available until the page after the page on which the function is used. This is because the value is submitted upon the user hitting a submit button which takes them to the next page. My question then is how can I get this value or if its even possible to do it this way.

Ahh... So what you want to do is dynamicly change the page depending on what the user chooses in the dropdown box? (Show different options depending on what course the user chooses?)

If it is, then PHP isn't realy the way to go. If you realy want to do it like this, you have to submit the page and check if $HTTP_POST_VARS[$data] exists, and if it does write the individual course options. You cannot do all this doing a single function in PHP.

The obvious take on this would be to divide the registration into steps at the places where the next choice would be dependant on the former.

Alternatively, you can look into AJAX.

No, I don't want it to be dynamic. I'll try generalizing my question more. Is it possible to create a function in PHP that builds something in HTML, such as a radio box or a select box in my case. This function would then need to return the value that is selected by the user. The form is a POST form and the action upon the user hitting the submit button is it goes to another page.

##### Share on other sites
Coward    210
Quote:
 Original post by ThirdNo, I don't want it to be dynamic. I'll try generalizing my question more. Is it possible to create a function in PHP that builds something in HTML, such as a radio box or a select box in my case. This function would then need to return the value that is selected by the user. The form is a POST form and the action upon the user hitting the submit button is it goes to another page.

No, not to my knowledge.

##### Share on other sites
Kylotan    10012
Quote:
 Original post by ThirdI'll try rephrasing what I want to do. I have a function that builds a select drop down box. I want this function to return the value that the user selects.

Nope, can't happen. The PHP just builds the HTML to send to the browser. It's packed up and gone home by the time the end user even sees the form, and doesn't come into play again until they submit the form, in which case it'll come up in the $_POST variables on whatever page you submit to. PHP function can't return user input - they only ever get it from arrays like$_GET, $_POST, and$_COOKIES, which are populated before a script starts running.

I am not entirely sure what you're trying to achieve. There is hardly ever a good reason to dynamically change the 'name' of an HTML form element as you need to know what it is called in order to process it when it is submitted. Instead you will check the value associated with that name in the \$_POST array in whichever file handles the submitted form, as opposed to sending the form to the user. (Although many people combine both functions into one page.)

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account