1

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

Community
  • 1
  • 1
Semon
  • 33
  • 1
  • 6

1 Answers1

0

From your code, I can see you have commented out the data posting with curl. Enable that.

Also, use http_build_query() to prepare your post data instead of manually preparing it to avoid any issue regarding special characters. So overall it will look like below:

$post_string = http_build_query($post_data);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • I un commented out the code and all it does is display on the next page, doesnt allow to redirect any longer. It still does not post the data to the db via the api. – Semon Feb 09 '16 at 11:08
  • It is not uncommenting, use the code from above and if it doesn't work, then update your question with the code so that anyone else can help you. – Sabuj Hassan Feb 09 '16 at 11:11
  • From your error msg, another point.. You are doing `print_r` before the `header("...")` call. Which is problematic. In HTTP the headers should be printed first before any output. You can look around with `ob_start()` function. A detail about this similar issue can be found at: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Sabuj Hassan Feb 09 '16 at 11:53
  • great link. That did fix the header error and it redirects when I put the header redirect just before the "print_r" curl code. but nothing is being posted to the database. Looking at the curl code to try to figure it out – Semon Feb 09 '16 at 15:40