3

I'm making a theme wordpress use childtheme in localhost. I had a basic form html in footer.php:

<form method="post">     
    <input name="FNAME" type="text">
    <input name="LNAME" type="text">
    <input name="EMAIL" type="email">
    <button type="submit">Subscribe</button>
</form>

Now , I want to submit form to an api url via cURL php. I know code to set up cURL like this:

$data =  $_POST;

$curl = curl_init();

$options =array(
    CURLOPT_RETURNTRANSFER =>true,
    CURLOPT_URL => 'link_to_url',
    CURLOPT_POST => true,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POSTFIELDS => http_build_query($data)
);
curl_setopt_array($curl, $options);

$result = curl_exec($curl);
curl_close($curl);

I want to when form is submitted , POST data will send via this cURL, But I don't know where I should place this code and how to process it. I think it maybe placed in a function in function.php but I don't know how to write it .Please help me!

1 Answers1

1

If you wish to execute said code within the same file can do something like this:

<?php
// any other php code above
if(isset($_POST["infoForm"])) {
    $curl = curl_init();

    $data = array(
        'post_request_name_for_fName' => $_POST["FNAME"],
        'post_request_name_for_lName' => $_POST["FNAME"],
        'post_request_name_for_email' => $_POST["EMAIL"]       
     );

    $options =array(
        CURLOPT_RETURNTRANSFER =>true,
        CURLOPT_URL => 'link_to_url',
        CURLOPT_POST => true,
        CURLOPT_CONNECTTIMEOUT => 30,
        CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_POSTFIELDS => http_build_query($data)
    );
    curl_setopt_array($curl, $options);

    $result = curl_exec($curl);
    curl_close($curl);
}
// any other php code below
?>

and set the HTML to this:

<form method="post">     
    <input name="FNAME" type="text">
    <input name="LNAME" type="text">
    <input name="EMAIL" type="email">
    <button type="submit" name="infoForm">Subscribe</button>
</form>

Now here's the explanation behind it. You set the name of your submit button to infoForm (or whatever else you want it to be) so the PHP can know which form is posted and when. You then use if(isset($_POST["infoForm"])) to check whether the form was posted, and then you execute the lower code to post it.

The $_POST["FNAME"] and other variants check the data found in each one of the inputs and sets that to a value of the $data array. Then you can change all the values of post_request_name_for_fName to whatever the values for your post request need it to be (i.e.; if you're post request needs a 'name' and 'email' parameter, just change the values above as needed).

If you also want to transition to a different page you can make a new file (i.e.; post.php) and add the code in their without the if(isset), and set your html as this:

<form method="post" action="post.php">     
    <input name="FNAME" type="text">
    <input name="LNAME" type="text">
    <input name="EMAIL" type="email">
    <button type="submit" name="infoForm">Subscribe</button>
</form>
qBen_Plays
  • 262
  • 2
  • 9
  • Thank you for your help. When I put excute code in the same file, how can I prevent a browser resubmision form when reload page ? – Nguyen Nhut Tien May 22 '20 at 07:39
  • On your ``
    `` tag add the following attribute: ``onSubmit="return false;"``, so the form would look like this ``
    ``
    – qBen_Plays May 22 '20 at 12:37
  • `onsubmit="return false"` prevent completely form submit. I mean when I submited form success, and I reload the page, and the page appeared an alert `confirm form resubmission`. So I don't want it appears. How could I do ? – Nguyen Nhut Tien May 23 '20 at 14:30