Link to home
Start Free TrialLog in
Avatar of matthewdacruz
matthewdacruzFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Is there a way to prepopulate fields in a webfrom on another server?

Hi
I am trying to build a script that posts information from a database to another website I have.
Is there a way you can post the information without having to fill in the form each time.
Or is there a way you can prepopulate form fields in php on an external website?
I am using PHP
Avatar of profya
profya
Flag of Sudan image

1) Your data can be pushed to the remote website or the remote website can pull it. The first case uses forms for example, and you can use web services for example or rss feed to allow the web site to pull the data from your database, or it can access your database directly with some configurations.
2) Your form can post its data to a page in a website different from that it uses.
You can use fsockopen() to send a POST method call to the external site.  It uses key=value pairs to associate the form input field names with their values.  You would write a script that pulls the data out of the data base and creates the query string.  Obviously, it would be greatly simplified if the column names in the data base matched the form input names at the remote site.

Read over this "teaching script" and see if the code and comments make sense to you.  Please post back here with any questions.

best regards, ~Ray
<?php // RAY_post_example_fsockopen.php
error_reporting(E_ALL);
 
// THE SITE WE WANT TO TALK TO - IN THIS CASE, TO MY SITE
$server_url  = "www.your.org";
$server_url  = $_SERVER["HTTP_HOST"];
 
// THE PAGE INSIDE THE WEB SITE
$request_uri = "/RAY_dump_superglobals.php";
 
// THE POST DATA THAT WE WANT TO SEND
$dat = array(
    'name' => 'joe',
    'mail' => 'joe@your.org',
    'word' => 'secret'
   );
 
// CONSTRUCT THE POST STRING URLENCODED AND SEPARATED BY AMPERSANDS
$postdata  = '';
foreach ($dat as $key => $val)
{
    $postdata .= '&' . $key . '=' . urlencode($val);
}
$postdata = ltrim($postdata, '&');
 
// CONSTRUCT THE HEADERS
$header = '';
$header .= "POST $request_uri HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postdata) . "\r\n\r\n";
 
// SHOW WHAT WE ARE GOING TO DO
echo "<pre>\n";
echo "REQUEST DATA FOLLOWS:\n";
var_dump($server_url);
var_dump($request_uri);
var_dump($postdata);
var_dump($header);
echo "</pre>\n";
 
 
// OPEN THE SOCKET TO THE PAGE
$fp = fsockopen ($server_url, 80, $errno, $errstr, 30);
 
// TEST FOR VERIFICATION
if (!$fp)
{
	die("HTTP ERROR $server_url $request_uri fsockopen() FAILED ERRNO=$errno ERRSTR=$errstr");
}
 
// HTTP OPEN - WRITE HEADER AND REQUEST
fputs ($fp, $header . $postdata);
 
// HTTP OPEN - READ RESPONSE
$reply = '';
while (!feof($fp))
{
	$reply	.= fgets($fp, 1024);
}
 
// SEE WHAT THE FOREIGN SITE SAID
echo "<pre>\n";
echo htmlentities($reply);
echo "</pre>\n";
 
// FINIS
echo 'Done!';
?>

Open in new window

Another way to look at this is called a "RESTful" interface.  In that case you would provide the data as part of the URL GET string.  Very simple and easy to debug, because you can simply type the URL with all the arguments into the browser address bar.

HTH, ~Ray
Avatar of matthewdacruz

ASKER

Thanks Guys, i am looking at what you have said. I will comment soon as all the above is new to me
Hi Ray,
I have tested your script and it works great.
I am assumming the receiving page will have to have code in the text fields to receive the POST data. eg, <?php echo $_POST['name']; ?> for instance.

I have two questions
Is there a way to have it actually submit the post so the form does the insert, delete or update.
The output is in raw html. How would you see the actual page from the output. Would you use an iframe? So I was wondering how you would click the button. How does one actually submit the form?

The second question is how do you post the information to a form like say yahoo for example.
https://edit.yahoo.com/registration?.intl=uk&new=1&.done=http://uk.mail.yahoo.com?.src=ym
This website obviouls is not listening for post data.
How does one handle this kind of task with what you have presented.

If there where sites I wanted to send information to that where external and I was allowed to do it. What would one have to do.

Thanks

Matt
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for the great advice. Learnt a lot Ray