简体   繁体   中英

extract data from elementor webhook with php

I'm trying to get the data elementor from a submission form to a MySql DataBase. I have already connected to the data base in a local way. But now that I want to get the data from the web page's webhook I can´t retrieve the data. Here's my code:

<!doctype html>
<html>
<body>


    <?php
        //Data Base connection info:
        $servername = "localhost";
        $username = "username"; 
        $password = "password";
        $dbName="dbName";

        ////////////////////////////Retrieved information from form ////////////////////////////////////////////
        $name=$email=$telephone=$message="";
                              ///////Data Obtained in the form:////////////
        if($_SERVER["REQUEST_METHOD"] == "POST") {
        $name=$_POST["No Label name"];
        $email=$_POST["No Label email"];
        $telephone=$_POST["No Label phone"];
        $message=$_POST["No Label message"];;


        //////////////////////////////////////////// DATA BASE CONNECTION ///////////////////////////////////////


        // Create connection with DB.

        $conn = mysqli_connect($servername, $username, $password, $dbName);
        //////////////////////////// Check connection ///////////////////////////////////
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        $sql="INSERT INTO TableName (Name, Email, Phone, Menssage ,Contacted) VALUES (?, ?, ?, ?,0)";
        $stmt=$conn->prepare($sql);
        $stmt->bind_param("ssss", $name, $email, $telephone, $message);
        $stmt->execute();
        mysqli_close($conn);
        }

    ?>
</body>
</html>

The "No Label name" came because i did not want to change how the web page looked. The request bin https://requestbin.com/ showed me the labels of the array being sent .

I have also tried this solution but it did't work:

<!doctype html>
<html>

<body>


    <?php
        //Data Base connection info:
        $servername = "localhost";// has to be changed to the actual host
        $username = "username"; 
        $password = "password";
        $dbName="dbName";//we need to add the data base name

        ////////////////////////////Retrieved information from form ////////////////////////////////////////////
        $name=$email=$telephone=$message="";
                              ///////Data Obtained in the form:////////////
        $Rawdata = file_get_contents("php://input");
        $data = json_decode($Rawdata, true); 
        $name=$data["No Label name"];
        $email=$data["No Label email"];
        $telephone=$data["No Label phone"];
        $message=$data["No Label message"];

        //////////////////////////////////////////// DATA BASE CONNECTION ///////////////////////////////////////


        // Create connection with DB.

        $conn = mysqli_connect($servername, $username, $password, $dbName);
        //////////////////////////// Check connection ///////////////////////////////////
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        $sql="INSERT INTO TableName (Name, Email, Phone, Menssage ,Contacted) VALUES (?, ?, ?, ?,0)";
        $stmt=$conn->prepare($sql);
        $stmt->bind_param("ssss", $name, $email, $telephone, $message);
        $stmt->execute();
        mysqli_close($conn);


    ?>
</body>
</html>

If their's any questions about the code or the variables please ask. Also I have the question if elementors webhooks sends the data in json or as a normal POST method. Regards

The fields in the webhook form are passed through a class called Ajax_Handler defined in wp-content/plugins/elementor-pro/modules/forms/classes/ajax-handler.php .

Although I haven't poked around in there much, looking at what is received by my PHP server it sends two POST parameters, at least it does if I toggle the Advanced Data option to yes in the Webhook section:

Parameter 'form' is an array containing keys 'id' and 'name' which are properties of the Form that the webhook call originates from - the 'id' is usually auto-generated but could be set in the Additional Options area, and the 'name' is the Form Name property in the Form Fields section.

Parameter 'fields' is an array of the Form Fields themselves. Each Form Field you created appears as a key to another array which holds the following:

'id' => the form field ID you set in Elementor
'type' => the type of field (text, email etc.)
'title' => the label for the field
'value' => the actual value in the input element
'raw_value' => not sure how this differs from 'value'...
'required' => whether the field was marked as required.

So for example I have a form named NewsSubscriber with two fields 'email' and 'name' and a hidden field called 'fn' to tell the server what to do, so I get the following in my PHP script:

$_POST=>[
  'form' => [
    'id' => '5cbb571',
    'name' => 'NewsSubscriber'
  ],
  'fields' => [
    'email' => [
      'id' => 'email',
      'type' => 'email',
      'title' => 'Email Address',
      'value' => 'person@example.com',
      'raw_value' => 'person@example.com',
      'required' => '1'
    ],
    'name' => [
      'id' => 'name',
      'type' => 'text',
      'title' => 'Name',
      'value' => 'Bob Person',
      'raw_value' => 'Bob Person',
      'required' => '1'
    ],
    'fn' => [
      'id' => 'fn',
      'type' => 'hidden',
      'title' => 'Function',
      'value' => 'add',
      'raw_value' => 'add',
      'required' => '0'
    ]
  ]
]

So (with a few checks) I can extract the function as $_POST['fields']['fn']['value'] , the email from $_POST['fields']['email']['value'] and the name from $_POST['fields']['name']['value']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM