[web] PHP function question

Started by
10 comments, last by Kylotan 18 years, 1 month ago
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?
Advertisement
<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)
Don't Temp Fate..
Quote:Original post by Coward
<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)


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.
Quote:Original post by Third
Quote:Original post by Coward
<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)


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?
Don't Temp Fate..
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>
Quote:Original post by Coward
Quote:Original post by Third
Quote:Original post by Coward
<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)


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.

I use this function on a registration page and I need it to somehow return $data or I need someway to get to it. Is it even possible to do it this way?
Quote:Original post by Third
Sure, here's the function that I created the builds the select box
*** Source Snippet Removed ***

The part that I need is $data, which is the select name, since this will contain what the user selects.

I use this function on a registration page and I need it to somehow return $data or I need someway to get to it. Is it even possible to do it this way?

I'm still not sure about what you want do. The string $data contains the name of the select box you're building, so if you want to reference you just have to $_POST[$data].

May I ask what you mean by "return $data" not working? Doesn't it return the string correctly?
Don't Temp Fate..
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.
Quote:Original post by Third
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.

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.
Don't Temp Fate..
Quote:Original post by Coward
Quote:Original post by Third
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.

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.

This topic is closed to new replies.

Advertisement