php form submission

Tenant

New Member
Messages
13
Reaction score
0
Points
0
I have been working on the following code to automatally submit data to the next page. I do not want my visitors to have to click a next button. I cant find where I am going wrong. I have upgraded my php to moderate do I need to go higher or is it a fault in my coding.

Code:
        //create array of data to be posted
        $post_data['business'] = $business;
        $post_data['owner'] = $owner;
        $post_data['address'] = $address;
        $post_data['city'] = $city;
        $post_data['state'] = $state;
        $post_data['zip'] = $zip;
        $post_data['con'] = $number;
        $post_data['page_id'] = $page_id;
        
        //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://www.mysite.com/comments.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
        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);


The page is displaying

Array ( => http://www.mysite.com/comments....me] => 0.105818 [redirect_time] => 0 ) 0-
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
um.. how about something along the lines of the following?
Of course, it only works for JS browsers, others will actually have to click the button.

The example below will submit the data straight away. It could easily be changed to submit after a certain amount of time, or at another event.
The next page will need to have $post_data = unserialize($_POST['data']); and then continue with all the data as-was.
PHP:
<?php
//create array of data to be posted
$post_data['business'] = $business;
$post_data['owner'] = $owner;
$post_data['address'] = $address;
$post_data['city'] = $city;
$post_data['state'] = $state;
$post_data['zip'] = $zip;
$post_data['con'] = $number;
$post_data['page_id'] = $page_id;

$post_data = serialize($post_data);

echo '<form action="http://www.mysite.com/comments.php" method="post" name="form">
<input type="hidden" name="data" value="'.$post_data.'">
<noscript><input type="submit" value="Submit"></noscript>
</form>';
?>
<script type='text/javascript'>
document.form.submit();
</script>
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
@tenant: try to use cURL the less times possible. it may not be supported by all hosts.
 
Top