简体   繁体   中英

How do I $_POST in loop

My current host doesn't allow remote mysql acces so I need to work around it by having script1 on server1 communicate with script2 on server2. I am trying to send post data to script2, which then takes that data and puts it into mysql. To keep my question simple I stripped the code down to this:

script1

for ( $counter = $counternumbernao; $counter <= $amountofcomments; $counter += 1) 
{
echo'
<form action="http://server2.x.com/form-receive.php" method="post">
<INPUT TYPE=HIDDEN NAME="comment_content" value=$comment_content>
<INPUT TYPE=HIDDEN NAME="comment_date" value=$comment_date">
<input type="submit" />
</form>
';
}

How can I alter this code, so that every time the loop occurs it automatically sends the $_POST data to script2, which then puts it into mysql? I didn't think it was necessary to include script2 as it's not important to this issue.

Why not use cURL to send the FORM data to server2. It allows you to send HTTP requests. Try out the following script:

$url = "http://server2.x.com/form-receive.php";
$postvars = "comment_content=$comment_content&comment_date=$comment_date";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1); 
curl_setopt($ch, CURLOPT_HEADER ,0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);  
$data = curl_exec($ch);

To have this automatically occur without the end user being aware of this behaviour, the best way would be to use CURL (http://php.net/manual/en/book.curl.php).

Here's an example of how it would look:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://server2.x.com/form-receive.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'foo' => 'bar',
    // Put data from $_POST here
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

Please remember that simply echoing a form will never cause the form data to be posted to some server. Whatever you echo from your php code will be sent only to the web browser(or any User Agent) that requested the php page.

I vote for answers suggesting cURL.

When you use cURL like in Jack Murdoch's example, the cURL command itself works as a HTTP User Agent and will send POST request to http://server2.x.com/form-receive.php. So in form-receive.php you will only need to write some normal php program to read data from $_POST array and read/write to database. This way you are actually going to build a webservice on server2.

Something along these lines:

echo '<form action="http://server2.x.com/form-receive.php" method="post">';
for ( $counter = $counternumbernao; $counter <= $amountofcomments; $counter += 1) 
{
    echo'
    <INPUT TYPE=HIDDEN NAME="comment_content[]" value=$comment_content>
    <INPUT TYPE=HIDDEN NAME="comment_date[]" value=$comment_date">
    <input type="submit" />
    ';
}
echo '</form>';

The form-receive script will then receive arrays of data in $_POST['comment_content'] and $_POST['comment_date'].

Try to use JQuery and post() method (http://api.jquery.com/jQuery.post/) then just generate a JS array with all records, then run it in a loop and ENJOY! Probably this solution will be faster than a looping/refreshing only in PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM