dsaff82024 2016-01-27 11:31
浏览 129
已采纳

使用PHP cURL提交表单,多个具有相同名称的复选框

I am trying to submit an external form using PHP cURL. All form fields are working fine, except a problem I have with multiple checkboxes with the same name.

<input type="checkbox" name="same_name" value="value_1">
<input type="checkbox" name="same_name" value="value_2">
<input type="checkbox" name="same_name" value="value_3">

I have no problem with passing ONE of the checkboxes in the cURL request. In my POST string, I just do:

curl_setopt($ch, CURLOPT_POSTFIELDS, '...&same_name=value_1');

But now, I want to submit the form with multiple boxes checked. I tried the suggestion in the comments on this StackOverflow post:

curl_setopt($ch, CURLOPT_POSTFIELDS, '...&same_name[]=value_1&same_name[]=value_2');

But then I get a response which is based on no checked checkboxes at all, ergo it doesn't work.

Basically, how can I submit such an array correctly in this request? Who can point me in the right direction?

  • 写回答

3条回答 默认 最新

  • doulun1915 2016-01-27 12:06
    关注

    Hmm, it's really weird. I just tried to setup my own page (this one is external) with the following code, and it indeed seemed impossible to select more than one value:

    <form method="post">
     <input type="checkbox" name="same_name" value="1">Value 1<br />
     <input type="checkbox" name="same_name" value="2">Value 2<br />
     <input type="checkbox" name="same_name" value="3">Value 3<br />
     <input type="submit">
    </form>
    <?php
    if(isset($_POST))
    {
     print_r($_POST);
    }
    ?>
    

    But after some tweaking, I think I got it working anyhow with ..&same_name=value_1&same_name=value_2.

    I don't know HOW they do it on their serverside, but it appears to work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?