Solved

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

Posted on 2009-07-16
7
225 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 108

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 108

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 108

Accepted Solution

by:
Ray Paseur earned 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now