dtx9931 2012-03-29 10:18
浏览 50

通过带有数组的php发布到表单

i'm using sirportly for my support and i have the ability to post forms remotely via html, however i'm trying to integrate this into wordpress and so want to post this form from a plugin via curl/php the challenge i am having is to post into array objects:

eg the basic original HTML form generated by sirportly contains the following:

<input type='text' name='ticket[name]' id='form26-name' />
<input type='text' name='ticket[email]' id='form26-email' />
<input type='text' name='ticket[subject]' id='form26-subject' />
<textarea name='ticket[message]' id='form26-message'></textarea>

i know for basic form elements eg name=name, name=email etc i can do the following:

//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

how do i do similar given that the elements need to be posted as 'ticket[name]' rather than just 'name'

EDIT/UPDATE - ANSWER

thanks to the answers below i came up with the solution, my issue wasn't getting access to the data from the array as i was recieving it a different way (but from an array all the same), but correctly encoding it in the post request, here is what i ended up with(note that i'm using gravity forms to get the data:

//numbers in here should match the field ID's in your gravity form
$post_data['name'] = $entry["1"];
$post_data['email'] = $entry["2"];
$post_data['subject'] = $entry["3"];
$post_data['message']= $entry["4"];

foreach ( $post_data as $key => $value) {
    $post_items[] = 'ticket[' . $key . ']' . '=' . $value;
}

$post_string = implode ('&', $post_items);

i just had to change the for loop to wrap the extra ticket[ and ] parts around the key for the post submission

  • 写回答

3条回答 默认 最新

  • douliu1092 2012-03-29 10:21
    关注
    foreach ( $post_data['ticket'] as $key => $value) {
        $post_items[] = $key . '=' . $value;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题