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

使用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!

    打赏 评论
  • douan4347 2016-01-27 11:37

    If it's your form (you can change the code), change checkbox's name to 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">
    

    and call curl_setopt($ch, CURLOPT_POSTFIELDS, '...&same_name[]=value_1&same_name[]=value_2');, it's OK.

    If it's external - you can't have multiple choice.

    打赏 评论
  • duanming7961 2018-02-26 03:20

    It is totally fine to pass an array for multi-value fields. It is usually a checkbox, but it can be any field such as multi-select. CURLOPT_POSTFIELDS is capable to handle this correctly and in the backend you get it as an array - identical to the submission from

    <input type="checkbox" name="same_name" value="1">Value 1
    <input type="checkbox" name="same_name" value="2">Value 2
    <input type="checkbox" name="same_name" value="3">Value 3
    
    
    $fields['name']='Smith';
    $fields['email']='you@your-domain.com';
    ....
    $fields['same_name'][]=1;
    $fields['same_name'][]=2;
    $fields['same_name'][]=3;
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    
    打赏 评论

相关推荐 更多相似问题