Submit a form and email it with PHP - php

I'm currently trying to get a script to submit a form to a page that is external to my site but will also e-mail the answers given by the customer to me. The mail() function has worked fine for the mail... but how do I then take these values and also submit them to the external page?
Thanks for your help!

If you get the form to submit to your script, can could first send the email and then use cURL to make a HTTP request to the external page, POSTing the values you want to send. This won't work though if the external site is relying on any cookies the user has, because the request is made from your web server.
e.g.
<?php
//data to post
$data = array( 'name' => 'tom', 'another_form_field'=>'a' );
//external site url (this should be the 'action' of the remote form you are submitting to)
$url = "http://example.com/some/url";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//make curl return the content returned rather than printing it straight out
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if ($result === false) {
//curl error
}
curl_close($curl);
//this is what the webserver sent back when you submitted the form
echo $result;

You're going to have to dig through the source of the external form to determine the HTML name's of the relevant fields and whether the form is submitted using GET or POST.
If the form uses the GET method, you can easily generate a query-string that follows the same form as the actual form: http://example.com/form.php?name1=value1&name2=value2 ...
If, on the other hand, the form uses the POST method, you'll have to generate a HTTP POST request using something like the cURL library (http://us2.php.net/curl).

You could send a custom HTTP POST request from the script that you're using to send the email. Try fsockopen to establish the connection and then send your own HTTP request containing the data you just received from the form.
Edit:
A bit more specific. There's this example that shows you how to send a simple HTTP POST request. Just seed it with your $_POST array like this:
do_post_request(your_url, $_POST);
and that should do the trick. Afterwards, you could optionally evaluate the response to check whether everything went OK.

For POST, you'll need to set the external page as the processing action:
<form action="http://external-page.com/processor.php" method="POST">
<!-- Form fields go here --->
</form>
If it's GET, you can either change the form method to GET, or create a custom query string:
submit
Edit: I just realized you probably want to send these from within your PHP processing class. In that case, you could use set the location header with the custom query string:
header("Location: http://external-page.com/processor.php?field1=value1&field2=value2");

Related

Submit a form on remote site via cURL and PHP, and download the file in response, remote page is html and https

What I am trying is submit a form on remote site, the output of that form is a pdf file which I want to store to my site locally. I want to automate this via cron job, using PHP and cURL.
Problems:
Remote site is https (even worse it is not properly setup)
The site is html and not PHP but it gives result as if PHP
What I have tried so far
I used cURL in PHP but did not quite work. It simply submits a response which does not include the response from the form submission.
I tried to create remote form on my local host and when I submit form, it does return pdf file but this solution does not uses Curl and hence I cannot automate it.
Code that I have tried so far
<?php
// set post fields
$post = [
'bench_sno' => '1',
'causelist_date' => '2010-10-1',
'btnSearch' => 'Search Causelist',
];
$ch = curl_init('http://peshawarhighcourt.gov.pk/app/site/4/p/Causelists_List.html');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
//var_dump($response);
echo $response;
?>
When I just copy the remote form to my site and submit the form, it does work fine Working Example but the problem is it does not work with cURL. I need to automate this task via cron job, any other solution if available, I can consider.
As came under discussion in the question itself, and pointed out by #vivek_23 to include it in the fields, the form now works
<input type="hidden" name="url" value="http://peshawarhighcourt.gov.pk:443/app/site/4/p/Causelists_List.html">

How do you submit a PHP form that doesn't return results immediately using Python?

There is a PHP form which queries a massive database. The URL for the form is https://db.slickbox.net/venues.php. It takes up to 10 minutes after the form is sent for results to be returned, and the results are returned inline on the same page. I've tried using Requests, URLLib2, LXML, and Selenium but I cannot come up with a solution using any of these libraries. Does anyone know of a way to retrieve the page source of the results after submitting this form?
If you know of a solution for this, for the sake of testing just fill out the name field ("vname") with the name of any store/gas station that comes to mind. Ultimately, I need to also set the checkboxes with the "checked" attribute but that's a subsequent goal after I get this working. Thank you!
I usually rely on Curl to do these kind of thing.
Instead of sending the form with the button to retrieve the source, call directly the response page (giving it your request).
As i work under PHP, it's quite easy to do this. With python, you will need pycURL to manage the same thing.
So the only thing to do is to call venues.php with the good arguments values thrown using POST method with Curl.
This way, you will need to prepare your request (country code, cat name), but you won't need to check the checkbox nor load the website page on your browser.
set_ini(max_execution_time,1200) // wait 20 minutes before quitting
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://db.slickbox.net/venues.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
// prepare arguments for the form
$data = array('adlock ' => 1, 'age' => 0,'country' => 145,'imgcnt'=>0, 'lock'=>0,'regex'=>1,'submit'=>'Search','vname'=>'test');
//add arguments to our request
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//launch request
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
echo $result;
How about ghost?
from ghost import Ghost
ghost = Ghost()
with ghost.start() as session:
page, extra_resources = session.open("https://db.slickbox.net/venues.php", wait_onload_event=True)
ghost.set_field_value("input[name=vname]", "....")
# Any other values
page.fire_on('form', 'submit')
page, resources = ghost.wait_for_page_loaded()
content = session.content # or page.content I forgot which
After you can use beautifulsoup to parse the HTML or Ghost may have some rudimentary utilities to do that.

Submit external form without leaving the page/site

I looked through the site for answers to this, but nothing's spot on to what I need (this is close, except it doesn't actually submit the form: Prevent form redirect OR refresh on submit?).
I'm trying to incorporate a mailing list sign-up (code borrowed from a sign-up page hosted on ReverbNation) to a website.
The form submits properly, but the signee is redirected to a hideously rendered page on ReverbNation's site. I cannot modify their script and don't think there's an API I can use to keep things tidy.
Is there a way I can submit the form in the background, without the user being redirected?
Here's an example in PHP for tunneling a POST.
//set POST variables
$url = 'http://domain.com/url-to-post-to';
$fields = array(
// Add the fields you want to pass through
// Remove stripslashes if get_magic_quotes_gpc() returns 0.
'last_name'=>urlencode(stripslashes($_POST['last_name'])),
'first_name'=>urlencode(stripslashes($_POST['first_name'])),
'email'=>urlencode(stripslashes($_POST['email']))
);
//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));
// returns the response as a string instead of printing it
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
If you're posting to the same domain, you can use an AJAX post. However, it seems you're trying to POST to different domain, so the browser's same origin policy will prevent you from doing so (http://en.wikipedia.org/wiki/Same_origin_policy). (JSONP can get around this but it doesn't work for POST)
Another way to get around this is to have your server do the POST and tunnel the response back to your page.
<form id='yourForm' action="" onsubmit="javascript: doPostToTunnelPage(); return false;">
<!-- inputs...-->
</form>
Make sure to return false, or your page will be redirected.
in my understand, you need send a form without redirect?
consider my example
$(function() {
$('#myForm').submit(function (e) {
e.preventDefault();
$.ajax({ /* params to send the form */ });
return false;
});
});
IIT it will work just because of the e.preventDefault.
If this method is called, the default action of the event will not be triggered.
See jQuery documentation here for more information.
Hope it help you

how to send redirect page pass variables as post variables from a php script

you can find a similar question here PHP Post Request inside a POST Request but this is not working in my context.
I have a form (reservation form for a tour website) and when the form is submitted, the values are processed in a script like validation and calculation of values and sending email.
after processing the variables, i want to send it to a page for payment and this page will post payment details to paypal.
My question is after the reservation form is submitted, after processing values retrieved from reservation from, how can i redirect the page in such a way that the variables will be passed as post variables. (I am not looking from response from the other form, i want to redirect to the other form).
To create a POST request, open a up a TCP connection to the host using fsockopen(), then use fwrite() on the handler returned from fsockopen() with the same values you used in the header functions in the OP. Alternatively, you can use cURL.
<?php
if(isset($_POST['Name'])) $Name = $_POST['Name'];
if(isset($_POST['Email'])) $Email = $_POST['Email'];
if(isset($_POST['Message'])) $Message= htmlentities($_POST['Message']);
$Curl_Session = curl_init('http://www.yoururl.com/script.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
?>
Where $Message would be your variables.
EDIT: i answered this long ago maybe i did forget to reference from php.net but here is the link per comment http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html as Reference
You can't. HTTP makes no provisions for redirecting with anything in the request body (which is where POST data goes). You can only redirect with a GET request. So the typical way to do this is to take the user to a second page that has a button to "Continue to PayPal", or something of that nature. That button POSTs all the data to PayPal as normal.
For what it's worth, if this is for the PayPal "Buy Now" button, they actually (even if not documented) allow sending all those form variables in the GET request, via the URL. We do this in one of our applications where we "track" the start of the payment process and then redirect to a PayPal URL containing all the form fields as a query string, then "complete" the transaction as the user returns.
Well, I suppose that if you are talking about keeping data in your own website between pages you need to use PHP's session functions
To start the session just do session_start();
and to add session vars just use the superglobal array $_SESSION['myvar'] = $value;
you can then read them through the same means print_r($_SESSION[]);
However if you are talking about sensing data with the paypal API I highly recommend looking at their developer API manual.
Hope that helps,
RayQuang

How redirect user with all variables coming with POST

I have a html form with action="script1.php"
In script1 I need write all data to the database and redirect to
script2.php, but I need all parameters posted to script1 to be sent to script2.
mod_rewrite is on
How I can redirect using PHP with all data come through POST ?
if i do like that this disgusting practice but
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function Search(){
wpc_form.submit();
}
// -->
</script>
</HEAD>
<BODY onload='Search()'>
<form name=wpc_form method="post" action="/script2/">
<?php
foreach($_REQUEST as $name => $value)
echo '<input type="hidden" name="'.$name.'" value="'.$value.'">'
?>
</form>
Impossible.
But you don't need it. Because you have all this data already. Just read it from the database in script2.php
A redirect doesn't allow you to do this unless you have custom client-side code running in the browser to extract state from the response message body in order to populate your form fields. This is advanced usage and probably not what you really want to do.
If you really do need to transmit state between your forms then you can use the session to do this. The form in the browser won't have access to the data, but your PHP script running on the server can store values between requests. Here's a link to a tutorial on sessions in PHP which might be of use to you. This approach is often used for maintaining application state between requests and redirects to third-party services such as OpenID providers etc.
You can use the cURL library (or similar) to send a separate POST request from your local script to the external service.
// assemble data from your post here:
$data = array('formfield' => 'data', 'otherfield' => 'otherdata');
// and then send it off somewhere else
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://somewhere.else');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

Categories