Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-16
7
Medium Priority
?
238 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:matthewdacruz
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:profya
ID: 24869686
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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24874097
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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24874110
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:matthewdacruz
ID: 24885266
Thanks Guys, i am looking at what you have said. I will comment soon as all the above is new to me
0
 

Author Comment

by:matthewdacruz
ID: 24885449
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
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 24889794
@matthewdacruz: I think we answered the original question.  You might want to post a separate question if these answers are not sufficient...

Is there a way to have it actually submit the post so the form does the insert, delete or update?
-->Yes, the script I posted does actually submit the form.  What the foreign host does with the form is out of our control - the foreign host decides all of that.

The output is in raw html. How would you see the actual page from the output.
--> That was illustrated at lines 61, et seq of the code snippet I posted.

Would you use an iframe?
--> Not if I can avoid it.

So I was wondering how you would click the button. How does one actually submit the form?
--> The fsockopen() post "actually" submits the form.  No button needs to be clicked, unless the foreign site has deliberately set up its action script to try to prevent automated posts.

The second question is how do you post the information to a form like say yahoo for example.
--> This is not a form, it is a "RESTful" web serivce.  You put the arguments into the URL (exactly as you have shown in your post).

This website obviouls is not listening for post data. How does one handle this kind of task with what you have presented.
--> Check the Yahoo documentation on the use of RESTful web services - this is a different topic entirely.

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.
--> You would do what we have stepped through here.  Getting all of this sort of thing to work is sometimes tedious and requires a lot of testing.  Not all sites have good documentation or clear API setups.  Best of luck with it, ~Ray
0
 

Author Closing Comment

by:matthewdacruz
ID: 31604250
Thank you for the great advice. Learnt a lot Ray
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question