【问题标题】:extract data from elementor webhook with php使用 php 从 elementor webhook 中提取数据
【发布时间】:2020-10-02 18:59:20
【问题描述】:

我正在尝试将数据元素或从提交表单获取到 MySql 数据库。我已经以本地方式连接到数据库。但是现在我想从网页的 webhook 中获取数据,我无法检索数据。这是我的代码:

<!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>

出现“无标签名称”是因为我不想更改网页的外观。请求箱 https://requestbin.com/ 向我展示了正在发送的数组的标签

我也尝试过这个解决方案,但没有奏效:

<!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>

如果他们对代码或变量有任何疑问,请询问。 我还有一个问题,如果 elementors webhooks 以 json 格式或作为普通 POST 方法发送数据。 问候

【问题讨论】:

    标签: php html wordpress webhooks elementor


    【解决方案1】:

    webhook 表单中的字段通过 wp-content/plugins/elementor-pro/modules/forms/classes/ajax-handler.php 中定义的名为 Ajax_Handler 的类传递。

    虽然我并没有在那儿闲逛,但查看我的 PHP 服务器接收到的内容,它会发送两个 POST 参数,至少在我将 Advanced Data 选项切换为 yes 时它会发送Webhook 部分:

    参数 'form' 是一个包含键 'id' 和 'name' 的数组,它们是 Webhook 调用源自的 Form 的属性 - 'id' 通常是自动生成的,但可以在 中设置Additional Options 区域,'name' 是 Form Fields 部分中的 Form Name 属性。

    参数“字段”是表单字段本身的数组。您创建的每个表单字段都显示为另一个数组的键,该数组包含以下内容:

    '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.
    

    例如,我有一个名为 NewsSubscriber 的表单,其中包含两个字段“email”和“name”以及一个名为“fn”的隐藏字段来告诉服务器要做什么,所以我在 PHP 脚本中得到以下内容:

    $_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'
        ]
      ]
    ]
    

    所以(通过一些检查)我可以将函数提取为$_POST['fields']['fn']['value'],来自$_POST['fields']['email']['value'] 的电子邮件和来自$_POST['fields']['name']['value'] 的名称

    【讨论】:

    • 绝对的救星! :-)
    猜你喜欢
    • 2020-02-22
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多