Page 1 of 1

Help with Forms: Passing a list of values

Posted: Wed Aug 15, 2012 6:04 am
by Live24x7
Assume i I have a function called calculate-answer() that takes unlimited number of arguments and then does some calculations.
The parameters must come through a user submitted html form - where the user submits all arguments ONLY from a single text field - all values separated by comma.

How to get this through the form and pass it to the function.

I am trying the below code but the function is not giving the correct answers.
When i pass the same arguments directly to the function - its returning the correct answer - so the function is correct

Code: Select all

$answer = "0";
if(isset($_POST['submit'])) {
include ('calculateanswer.php');
$numbers =  $_POST['numbers'];
$answers = calclulate-answers($numbers);
echo 'Answer:'. $answer;
} 
else { ?>
<form action="" method="post">
<input type="text" id="numbers" name="numbers">
<input type="submit" name="submit" value="Calculate Answer">
</form>
 }
My question:
a) How do i pass a list of comma separated list of numbers from a form to my function ?
b) Why is my above code not passing the correct arguments to my function ?

Thanks for your answer.

Re: Help with Forms: Passing a list of values

Posted: Wed Aug 15, 2012 7:21 pm
by requinix
Live24x7 wrote:Assume i I have a function called calculate-answer()
No you don't. But if that was an underscore, sure.
Live24x7 wrote:a) How do i pass a list of comma separated list of numbers from a form to my function ?
Your code is doing exactly that.
Live24x7 wrote:b) Why is my above code not passing the correct arguments to my function ?
Because your function probably doesn't want a list of comma separated numbers. It probably wants multiple numeric arguments.
Live24x7 wrote:When i pass the same arguments directly to the function
What does that look like?

Re: Help with Forms: Passing a list of values

Posted: Wed Aug 15, 2012 11:09 pm
by Live24x7
Thanks for answering. Lets say the function is calculate_answer() (with underscore)

so when i pass the arguments manually i do something like:
calculate_answer(10, 15, 20, 25) and so on - and this gives the correct answer
but when i pass the same comma separated values through the form it yields the wrong answer.

I guess the answer lies in this:
Because your function probably doesn't want a list of comma separated numbers. It probably wants multiple numeric arguments.
I will try this - will explode the comma separated values and pass each as arguments. Hopefully This should work. Thanks..

Re: Help with Forms: Passing a list of values

Posted: Thu Aug 16, 2012 1:22 am
by requinix
Live24x7 wrote:I guess the answer lies in this:
Because your function probably doesn't want a list of comma separated numbers. It probably wants multiple numeric arguments.
I will try this - will explode the comma separated values and pass each as arguments. Hopefully This should work. Thanks..
That would be it, yes.

explode() the list out into an array, verify that each value is valid (ie, an integer), and then you can

Code: Select all

$result = call_user_func_array("calculate_answer", $array);
It's equivalent to

Code: Select all

$result = calculate_answer($array[0], $array[1], $array[2]...);
but without you having to know how many arguments there are.

Re: Help with Forms: Passing a list of values

Posted: Thu Aug 16, 2012 1:43 am
by Live24x7
thanks a lot - it worked :)