0

If there are 2 form fields in a page with 2 different submit buttons how do I tell to cURL which form to submit?

Example

<form method="post" action="index.php">
   <input type="text" name="age">
   <input type="submit" name="agree">
   <input type="submit" name="disagree">
</form>

My code so far

//create array of data to be posted
$post_data['age'] = '5';
 
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

$post_string = implode ('&', $post_items);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

2 Answers 2

1

There is no needing to know which form in HTML must be posted.

Form can be determined by Method, Action and/or Set of fields.

P.S. Clicked named submits are sending too. So add post_data['agree'] = '';.

P.P.S. Don not use it for spam.

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass an array to CURLOPT_POSTFIELDS and let php's curl extension do the dirty work of encoding etc.

This saves you from encoding the string!

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);

3 Comments

Yes but what about specifying which submission button to click?
$post_data['agree'] = 'Submit' where agree is either agree or disagree :)
Name of submit button is a key in $_POST array. Simply set it and specify some dummy value, e.g. "Submit"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.