Jason
Jason

Reputation: 133

Using curl to submit/retrieve a forms results

I need help in trying to use curl to post data to a page and retrieve the results after the form has been submitted.

I created a simple form:

<form name="test" method="post" action="form.php">              
    <input type="text" name="name" size="40" />
    <input type="text" name="comment" size="40" />
    <input type="submit" value="submit" />
</form>

In addition, I have php code to handle this form in the same page. All it does is echo back the form values.

The curl that I have been using is this:

  $h = curl_init();

  curl_setopt($h, CURLOPT_URL, "path/to/form.php"); 
  curl_setopt($h, CURLOPT_POST, true);
  curl_setopt($h, CURLOPT_POSTFIELDS, array(
  'name' => 'yes',
  'comment' => 'no'
  ));
  curl_setopt($h, CURLOPT_HEADER, false);
  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($h);
  echo $result;

When I launch the page with the curl code in it, I get the form.php page contents but it doesn't not show the variables that PHP should have echo'd when the form is submitted.

would appreciate any help with this.

Thanks.

Upvotes: 11

Views: 23191

Answers (2)

Luke Stevenson
Luke Stevenson

Reputation: 10351

From reading the other two answers, and the comment by the OP, I have a couple of ideas.

Specifically, the OP Comment was:

Submitting the form manually does produce the right output. The PHP that handles the form is: if(isset($_POST['submitted'])){echo $_POST[name] ..... etc. and that's it

Under standard conditions, your basic form, as noted in the Original Question, would generate a $_POST array like the following:

array(
  'name' => 'The Name as Entered in the Form' ,
  'comment' => 'The Comment as Entered in the Form' ,
  'submit' => 'submit' # From the "Submit" button
);

Your comment suggests that some aspect of your form handler is looking for a $_POST element called "submitted".

1) The basic form, as set out in the question, will always return FALSE for a check for $_POST['submitted'], and, as such, will trigger the ELSE action (if present) for that conditional.

2) Your CURL Action does not set either 'submit' or 'submitted', and, again, will always return FALSE for the conditional.

So, I would suggest that you:

1) Check your Form Handler, and see what fields are essential, what their names are and what their content should be.

2) Check your Basic Form, and ensure that it has the appropriate fields.

3) Check your CURL Action, and ensure that it details every field which is required. (Easy check would be to insert print_r( $_POST );die(); at the top of your Form Handler and submit the basic form. This will show you exactly what the form is sending, so you can recreate it in CURL.

Upvotes: 1

dqhendricks
dqhendricks

Reputation: 19261

AHHHH after your comment, the problem is clear.

you need to include one more POST variable in your array.

'submitted' => 'submitted'

the submit button also returns a post value if clicked, which you are checking in your form processing PHP.

if ($_POST['submitted']) {

in your curl code however you have left out the 'submitted' post variable.

if this is what you are looking for, please select the check mark next to this answer. thanks!

Upvotes: 3

Related Questions