user1621488
user1621488

Reputation: 1

Sending a request to another URL for a PHP script?

So I have a contact form on one website, but I want to use the mailing function on another. I can make forms and get the text and send them in the PHP mail function just fine, I'm just not sure how to do this for another website without opening another tab.

Also, I'm not even sure what to call this.

For example: I have text fields and a submit button on one website, and I want to send that data to another URL like so:

http://myurl.com?act=phptools&[email protected]&subject=Hello&message=How are you?

How would I do this without opening another tab in the browser?

Upvotes: 0

Views: 489

Answers (3)

Ravi Soni
Ravi Soni

Reputation: 2250

As far as i understand your question You can use CURL php functions for this

<?php
    $urltopost = "http://somewebsite.com/script.php";
    $datatopost = array (
                            "firstname" => "ex_name",
                            "lastname" => "ex_lastname",
                            "email" => "[email protected]",
                        );`enter code here`
    <span id="more-40"></span>`enter code here

    $ch = curl_init ($urltopost);
    curl_setopt ($ch, CURLOPT_POST, true);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

    $returndata = curl_exec ($ch);
?>

Upvotes: 0

user1437328
user1437328

Reputation: 15846

Set the action attribute of the form to the other website url. The user on submit will be taken there, and on success/failure you can redirect the user to the old website url or wherever you want.

Even better use XHR/Ajax, so that the request is made behind the scenes and all you need to do is show the error msg/success msg based on the json response you sent from the other website url. However with this method there are limitations as browsers do not allow cross origin requests. So check if you can enable CORs!

Another thing you can do is send request to current website url only and that would interact with the other website url server-size, hope it makes sense.

Upvotes: 0

futuregeek
futuregeek

Reputation: 284

Just set the action attribute of your form to the URL of the processing script on the other domain.

But make sure this is secure, I mean, if you control the second domain, then its okay. Else, you may have to be concerned about sharing data with another domain. But then, I don't know what your requirement is :)

Upvotes: 1

Related Questions