Post to form via php with an array

I am using sirportly for my support and I have the ability to submit forms remotely via html, but I am trying to integrate this into wordpress and so I want to post this form from a plugin via curl / php. Task i I need to send array objects:

for example, the basic HTML source 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 basic form elements like 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 this given that the items should be posted as "ticket [name]" and not just "name"

EDIT / UPDATE - ANSWER

thanks to the answers below I came up with a solution, my problem was not accessing the data from the array as I was accessing it in a different way (but from the array anyway), but encoded it correctly in the post request, this is what I ended up with (note that I I use gravity forms to get data:

//numbers in here should match the field ID 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 to send the message

+3


source to share


3 answers


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

      



+3


source


The form above will create this structure on the server:

$_POST["ticket"] = Array(
   "name" => "some name",
   "email" => "some@thing",
   "subject" => "subject of something",
   "message" => "message text"
   );

      



So $_POST["ticket"]

really yours$post_data

+2


source


The variable is accessed as follows: $_POST['ticket']['name']

so if you want to see the value you should: echo $_POST['ticket']['name'];

to loop:

foreach($_POST['ticket'] as $key => $val){
    echo "Key =>  " . $key. ", Value => " . $val . "<br />";
}

      

0


source







All Articles