Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit HTML form array to PHP using CFHTTP

I am trying to submit a form using CFHTTP to a PHP page - everything is working properly except for one form field that is using arrays.

The HTML code in the form is

<select name="groupselect[]" id ="groupselect" size="10" multiple="multiple">
<option value="26713">X-Men</option>
</select>

When I submit using CFHTTP all of the other form fields are submitted and recognised, but not this one.

I have tried

<cfhttpparam type="formfield" name="groupSelect[]" value='26713'>
<cfhttpparam type="formfield" name="groupSelect" value='26713'>
<cfhttpparam type="formfield" name="groupSelect[0]" value='26713'>
<cfhttpparam type="formfield" name="groupSelect[]" value='Array ( [0] => 26713 )'>
<cfhttpparam type="formfield" name="groupSelect" value='Array ( [0] => 126713 )'>

I have tried serialising, I have tried WDDX, I have tried everything I can think of and have had no luck. What am I overlooking? At the moment I only need to submit one value using this field, but in the future I may need to submit multiple values.

Any help would be greatly appreciated.

EDIT AFTER FIRST COMMENT

I did some more debugging and now I am more puzzled! When I submit the form manually $_Post returns:

Array
(
    [groupselect] => Array
        (
            [0] => 26713
        )

)

Then I submitted the form using cfhttpparam type="formfield" name="groupSelect[]" value='26713' and the $_Post returned the exact same response! When I take the square brackets out, it shows me just a variable, not an array...

So, if that is working I guess I need to test some more with the live site and see why it isnt working?

like image 749
JDot Avatar asked Sep 11 '26 06:09

JDot


2 Answers

I think you need to debug this a bit to actually find out what is going on.

It might be that cfhttpparam is not able to use the same name multiple times or is ignoring the HTML standard and disallows characters like [ or ] (this is an assumption only, I haven't used cfhttp lately).

To debug this you can inspect which post-data is actually transferred to your PHP script by reading it:

$postdata = file_get_contents("php://input");

This will return the whole post-data that has been submitted to your script. If you dump it, you might find out more what is going on.

Alternatively you can debug this by using a network sniffer and inspecting the data that goes over the wire.

like image 112
hakre Avatar answered Sep 13 '26 18:09

hakre


OK - it turns out that the way to do this in ColdFusion is the most obvious way:

<cfhttpparam type="formfield" name="groupSelect[]" value='26713'>

However, for the ColdFusion programmers out there, PHP is case sensitive when it comes to variable names...

So while my name="groupSelect[]" wasnt working, name="groupselect[]" works perfectly!

Hakre, thanks for pointing me in the right direction with the suggestion of debug, and hopefully this helps someone else in the future!

like image 21
JDot Avatar answered Sep 13 '26 20:09

JDot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!