I have searched and cannot get my form to post and then redirect to my own url. I can get it to one or the other but not both. if I put the api in the form action it writes to the db. Im trying to allow a user to submit a form and the data is passed to a DB on another server via API and the user is redirected to a page of my choice. The following Q&A is the closest I've come Using both form submission and redirection On the form submit the user is sent to the "sendthedata.php" which has the following code
Now when I submit the form it does redirect to the page specified but does not post any data. When I try to use the javascript (from the example link) the form doesnt even submit, the page refreshes and the data is in the url from the submitted form.Any Ideas what im doing wrong? I never used curl before.
**updated code but now get a page with an output that I don't understand
//create an array of data to be posted
$post_data['token'] = 'token';
$post_data['email'] = 'email';
$post_data['first_name'] = 'first_name';
$post_data['last_name'] = 'last_name';
$post_data['edu'] = 'edu';
$post_data['eduint'] = 'edu_int';
//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);
//create cURL connection
$curl_connection =
curl_init('http://DATABASE.com/listener.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
$post_string = http_build_query($post_data);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
header("location: mypage.php");
Array ( [url] => http://DATABASE.com/listener.php [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 387 [request_size] => 292 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.156171 [namelookup_time] => 7.6E-5 [connect_time] => 0.060235 [pretransfer_time] => 0.060291 [size_upload] => 88 [size_download] => 27 [speed_download] => 172 [speed_upload] => 563 [download_content_length] => 27 [upload_content_length] => 88 [starttransfer_time] => 0.15614 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => xxxxxxxxxxx [primary_port] => 80 [local_ip] => xxxxxxxxxxx [local_port] => 35623 [redirect_url] => ) 0- Warning: Cannot modify header information - headers already sent by (output started at /home/xxxxx/public_html/xxxxx/tst/poster.php:42) in /home/xxxxx/public_html/xxx/tst/poster.php on line 52