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
foreach ( $post_data['ticket'] as $key => $value) {
$post_items[] = $key . '=' . $value;
}
The form you listed above will result in this structure on server:
$_POST["ticket"] = Array(
"name" => "some name",
"email" => "some#thing",
"subject" => "subject of something",
"message" => "message text"
);
So, $_POST["ticket"] is really your $post_data
You access the variable by this: $_POST['ticket']['name']
so if you want to see the value you should: echo $_POST['ticket']['name'];
to loop you go:
foreach($_POST['ticket'] as $key => $val){
echo "Key => " . $key. ", Value => " . $val . "<br />";
}
Related
I converted an old cgi mailform form to PHP by simply doing the below and emailing $msg as the body of the email.
I'd like to extract the value of the field "email" as a seperate variable to use as the reply-to address.
foreach ($_POST as $field=>$value) {
if ($field != "submit") $msg .= $field . ": " . $value . "\n";
}
will something like $field[email] have the value of the email field in it?
This will do it if you're working with a single dimension array. If you have an associative array, then you'll need to add another search variable. An associative could look something like:
Array (1) (
Array (3) (
["email"] =>
[0] => jointtech#example.com
[1] => bob#example.com
[2] => tom#example.com
)
)
foreach ($_POST as $field=>$value) {
if ( strpos($field, 'submit') === false ) {
$msg .= $field['email'] . ": " . $value . "\n";
}
If you have the associative array above, then the $field['email'] will not work.
I wanted to ask, how can i modify array values when doing foreach - in my example, i want to add to every element, but this code produces error.
foreach ($logo as &$value) {
$value = '<div>' . $value . '</div>';
debug ($value);
}
?>
and the error: Notice (8): Array to string conversion
I'm using php 5.6
Thanks for reply.
I use
$serviceMe = ""; //OR use beginning html
foreach( $tmp as $t ) $serviceMe .= '<tr>' . $t . '</tr>';
$serviceMe .= ""; // Rest of the needed html
Make absolutely sure what type of is your $value. Is it integer,float,string,object,array... etc
I think your error comes from trying to convert array to string without proper handling.
$array = new array(
'key' => $value,
'key2' => $value2,
'key3' => $value3,
);
$string = "" . $array; //throws error!
Hi everybody i need help! I've starting to learn php some week ago.
i have a form that POST some text field for a contact, i need to split the array in some variables to put in a email. this is my code
$field = $_POST['input'];
if ( isset( $field ) === TRUE){
foreach ($field as $key => $value) {
echo '<pre>' , print_r( $value ) , '</pre>'; //to list the array
}
$to = $mail;
$sbj = "thanks to register to $site";
..//some headers
mail($to,sbj,$headers)
}
and this is the form
<form action="upload.php" method="POST">
<input type="text" name="input[]">
<input type="text" name="input[]">
<input type="text" name="input[]">
<input type="submit" value="invia">
</form>
any suggestion for retrive the variables on the array to include on the mail?
#John has given the right procedure . You can use another procedure by using array_push() function eg:
$field = $_POST['input'];
$info = array();
foreach ($field as $key => $value) {
array_push($info,$value);
}
// echo implode(",",$info);
$to = $mail;
$sbj = "thanks to register to $site";
$body = implode(",",$info);
..//some headers
mail($to,sbj,$body,$headers)
You can use the implode() function , which takes an array and a separator and joins the elements of the array together into a string, with each element separated by the separator string you pass it eg:
foreach($field as $key=>$value) {
$input[] = $value;
}
$body = implode("\n",$input); //implode here
$to = $mail;
$sbj = "thanks to register to $site";
..//some headers
mail($to,sbj,$body,$headers)
This would create your list of input each on a separate line in the email.
the easiest, but most ugly way to to this is just using:
mail($to,sbj,print_r($field, true),$headers);
print_r($field, true) will output the value to a variable and not print it out immediately.
I have to submit an array via cURL in PHP, and one of the item in the array must be a signature generated with MD5 on a concatenation of array items and a public key:
// public key
$key = '4d7894219f3d28a6fbb8c415779dfffc';
// payload; each item was initialized
$vars = array(
'id'=>$id,
'name'=>$name,
'phone'=>$phone,
'email'=>$email,
'subject'=>$subject,
'notes'=>$notes,
'time'=>date('Y-m-d H:m:s'),
);
// Now create a HTTP query with the items of the array
$caesar = '';
foreach ($vars as $key2 => $value) {
$caesar = $caesar . $key2 . '=' . $value . '&';
}
unset($key2);
unset($value);
$caesar = substr($caesar, 0, -1);
// Concatenate the string from step 2 and $key (string then $key)
$caesar = $caesar . $key;
//MD5
$signature = md5($caesar);
Later I add the sig to the array and use cURL to transfer the array. But the server keeps returning "invalid signature". I don't see what is wrong. Please help me to identify the problem. Thanks.
How do I split the URL and then get its value and store the value on each text input?
URL:
other.php?add_client_verify&f_name=test&l_name=testing&dob_day=03&dob_month=01&dob_year=2009&gender=0&house_no=&street_address=&city=&county=&postcode=&email=&telp=234&mobile=2342&newsletter=1&deposit=1
PHP:
$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url);
foreach($para as $key => $value){
echo '<input type="text" value="" name="">';
}
above code will return:
l_name=testing
dob_day=3
doby_month=01
....
and i tried another method:
$url = $_SERVER['QUERY_STRING'];
$para = explode("&", $url);
foreach($para as $key => $value){
$p = explode("&", $value);
foreach($p as $key => $val) {
echo '<input type="text" value="" name="">';
}
}
Why not using $_GET global variable?
foreach($_GET as $key => $value)
{
// do your thing.
}
$queryArray = array();
parse_str($_SERVER['QUERY_STRING'],$queryArray);
var_dump($queryArray);
php has a predefined variable that is an associative array of the query string ... so you can use $_GET['<name>'] to return a value, link to $_GET[] docs
You really need to have a look at the Code Injection page on wikipedia you need to ensure that any values sent to a PHP script are carefully checked for malicious code, PHP provides methods to assist in preventing this problem, htmlspecialchars
You should use the $_GET array.
If your query string looks like this:
?foo=bar&fuz=baz
Then you can access the values like this:
echo $_GET['foo']; // "bar"
echo $_GET['fuz']; // "baz"