tags:

views:

134

answers:

3
+2  Q: 

Virtual PHP Form

I have a PHP file that spits out a form. I want to call this PHP file server-side, (currently using "include"), fill it in, and submit it.

This is better so I don't have to meddle around with the actual form PHP, just deal with the presentation layer so the data gets understood by its own PHP file.

Is this possible? The form "method" is POST.

+1  A: 

So you want to submit a form to yourself? Sounds like CURL will be the way to do it, but you'd be creating another HTTP session. Otherwise you can emulate the POST variables and call the form's action from your outside script.

Rob Elsner
I understood the part about filling in the (possibly read-only?) array of POST data, but how do I call the form's action? What PHP function could I use?
Jenko
+4  A: 

You won't be able to fill in the form and submit it using include(). Submitting a form means that it has to go over HTTP to a web server, so what you're looking for is to emulate a POST request. PHP has a popular library called CURL to do this.

Try something like this:

$ch = curl_init('http://www.example.com/yourform.php');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'var1=value1&var2=value2&whatever=stuff');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

$output will contain the page output as if you had just submitted the form.

zombat
Will this CURL request execute within the same session? (current PHP session is used)And will it execute within the same "environment"? (global vars can be accessed)?
Jenko
No. You can add cookies to a CURL request though if you wanted to propagate the session.I think you might be confusing the issue a little bit. You can't submit a form inside a PHP script to the PHP script. The form doesn't exist inside the PHP script... it only exists on a web page. Once the form is submitted, PHP reads it in as a series of variables (the $_POST array).If you're talking about having the form submitted to a script from inside the same PHP script, simply write a function "handleForm()" that does what you want, manually set up the $_POST array, and run the function.
zombat
+1  A: 

Based on your comments, it sounds like you're trying to simulate the submission of this form within the current request. I'm not sure if you can do that with cURL - I think each invocation of your PHP script executes independently, so the global variables from the current run of the script wouldn't be shared by the run which handles the cURL request.

The way I'd handle this would be to create a function to handle the form submission:

function handle_form($postdata) {
    // whatever you would normally do, just replace $_POST with $postdata
}

// to handle a normal form submission:
handle_form($_POST);

This way, you can simulate a submission of the form by just creating an array with the form field values:

$formvals['fieldname1'] = 'fieldvalue1';
$formvals['fieldname2'] = 'fieldvalue2';

and calling

handle_form($formvals);
David Zaslavsky