Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit form on an external website and get generated HTML?

I would like make a script using PHP (probably need JS) to send POST data to another webpage and get the result back.

For example, Domain A will have a form with a textbox and submit button, and Domain B will have a script which will fill the textbox and press the submit button and return the generated HTML page.

like image 681
sczdavos Avatar asked Jun 14 '12 09:06

sczdavos


People also ask

Where do form submissions go HTML?

The visitor's web browser uses HTML code to display the form. When the form is submitted, the browser sends the information to the backend using the link mentioned in the "action" attribute of the form tag, sending the form data to that URL.

How do you link a submit button to another webpage using HTML?

In HTML, linking submit buttons using the Anchor Tag is a simple and dependable approach. Write/Declare a Submit button between the Anchor tag's Starting and Closing tags. Give a Path where you wish to link your Submit Button by using the href property of the Anchor element.

How an HTML form is submitted to a server?

In HTTP, there are two ways to submit HTML forms to the server using the application/x-www-form-urlencoded and multipart/form-data content types. HTML forms can be submitted from browsers and from code. Submitting HTML forms using the application/x-www-form-urlencoded media type.

How do you embed a form into a website?

Go to Formsand open your form. Click Send. Click the HTML and click Copy. Paste the HTML into your site or blog.


2 Answers

The following lines of code can be written on another php script,

//set POST variables
$url = 'the website from which you need data through post';
$fields = array(
            //post parameters to be sent to the other website
            'text'=>urlencode($_POST['text']), //the post request you send to this  script from your domain.
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Now $result will contain the text received from the other website.

like image 101
ppsreejith Avatar answered Nov 14 '22 16:11

ppsreejith


With JS: for security reasons not. Read on Same origin policy.

With PHP you can do what you want, including POSTing other servers. For example use CURL.

like image 34
Bergi Avatar answered Nov 14 '22 15:11

Bergi