我正在尝试从提交表单中获取数据元素到 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 方法发送数据。 问候
webhook 表单中的字段通过
wp-content/plugins/elementor-pro/modules/forms/classes/ajax-handler.php
中定义的名为 Ajax_Handler 的类传递。
虽然我没有太多研究,但看看我的 PHP 服务器收到的内容,它发送了两个 POST 参数,至少如果我在 Webhook 部分将 Advanced Data 选项切换为 yes 的话:
参数 'form' 是一个包含键 'id' 和 'name' 的数组,它们是 Webhook 调用源自的表单的属性 - 'id' 通常是自动生成的,但可以在 Additional Options 区域中设置,“名称”是 Form Fields 部分中的 Form Name 属性。
参数“fields”是表单字段本身的数组。您创建的每个表单字段都显示为另一个数组的键,该数组包含以下内容:
'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 的表单,其中包含两个字段“电子邮件”和“名称”以及一个名为“fn”的隐藏字段,用于告诉服务器要做什么,因此我在 PHP 脚本中得到以下内容:
$_POST=>[
'form' => [
'id' => '5cbb571',
'name' => 'NewsSubscriber'
],
'fields' => [
'email' => [
'id' => 'email',
'type' => 'email',
'title' => 'Email Address',
'value' => '[email protected]',
'raw_value' => '[email protected]',
'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']
的姓名