Send form data to another script

This documentation page is for Quform version 1 and may not be applicable for Quform 2 click here to visit the documentation for Quform 2.

In this guide we will show you to how send the form data to another script, while maintaining the form validation and success actions of the Quform plugin.

You will need the cURL PHP extension enabled, on most servers this will already be enabled

Basic example

Step 1

Get the unique ID of the form, see Finding the form ID.

Step 2

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
4
5
6
78
9
10
11
12
13
14
15
16
17
function my_post_to_another_script($form)
{
    // Create a new cURL resource
    $ch = curl_init();
 
    // Set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/script.php');    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
 
    // Send the request
    curl_exec($ch);
 
    // Close cURL resource, and free up system resources
    curl_close($ch);
}
add_action('iphorm_post_process_3', 'my_post_to_another_script', 10, 1);
function my_post_to_another_script($form)
{
    // Create a new cURL resource
    $ch = curl_init();

    // Set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/script.php');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));

    // Send the request
    curl_exec($ch);

    // Close cURL resource, and free up system resources
    curl_close($ch);
}
add_action('iphorm_post_process_3', 'my_post_to_another_script', 10, 1);

Modify the highlighted lines to suit the Quform settings and script URL:

  • On line 7, change the URL to the other script URL
  • On line 17, change the number 3 on the last line to the unique ID of the form, from Step 1

Rewrite the post variables (optional)

If the other script is expecting the values in the post data to have different names, you can rewrite them before sending the data.

Step 1

Get the unique ID of the form, see Finding the form ID.

Step 2

Get the unique ID of each form field you want to rewrite, see Finding the unique element ID.

Step 3

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
4
56789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function my_post_to_another_script($form)
{
    // Rewrite the post variables
    $post = array(
        'name' => $_POST['iphorm_X_X'], // Change iphorm_X_X to name field unique ID        'email' => $_POST['iphorm_X_X'], // Change iphorm_X_X to email field unique ID        'phone' => $_POST['iphorm_X_X'], // Change iphorm_X_X to phone field unique ID        'message' => $_POST['iphorm_X_X'] // Change iphorm_X_X to message field unique ID    );
 
    // Create a new cURL resource
    $ch = curl_init();
 
    // Set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/script.php');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
 
    // Grab URL and pass it to the browser
    curl_exec($ch);
 
    // Close cURL resource, and free up system resources
    curl_close($ch);
}
add_action('iphorm_post_process_3', 'my_post_to_another_script', 10, 1);
function my_post_to_another_script($form)
{
    // Rewrite the post variables
    $post = array(
        'name' => $_POST['iphorm_X_X'], // Change iphorm_X_X to name field unique ID
        'email' => $_POST['iphorm_X_X'], // Change iphorm_X_X to email field unique ID
        'phone' => $_POST['iphorm_X_X'], // Change iphorm_X_X to phone field unique ID
        'message' => $_POST['iphorm_X_X'] // Change iphorm_X_X to message field unique ID
    );

    // Create a new cURL resource
    $ch = curl_init();

    // Set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/script.php');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

    // Grab URL and pass it to the browser
    curl_exec($ch);

    // Close cURL resource, and free up system resources
    curl_close($ch);
}
add_action('iphorm_post_process_3', 'my_post_to_another_script', 10, 1);

Modify the highlighted lines to suit the Quform settings and script URL:

  • On lines 5 to 8, change the array so that the key is the name of the post variable the other script is expecting and iphorm_X_X is the unique ID for the element, from Step 2
  • On line 25, change the number 3 on the last line to the unique ID of the form, from Step 1
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy