Php 向api提交表单并通过快捷码保存在db中
我想在wordpress中通过快捷码显示表单,当用户提交条目时,一些信息应发送到api,一些信息应存储在数据库中:Php 向api提交表单并通过快捷码保存在db中,php,wordpress,Php,Wordpress,我想在wordpress中通过快捷码显示表单,当用户提交条目时,一些信息应发送到api,一些信息应存储在数据库中: <?php function form($atts, $content = null) { extract( shortcode_atts( array( ), $atts ) ); echo ' <form id="trello" class="form-horizontal" method="post">
<?php
function form($atts, $content = null) {
extract( shortcode_atts( array(
), $atts ) );
echo '
<form id="trello" class="form-horizontal" method="post">
<div class="form-group">
<div class="col-md-6">
<input type="text" name="name" placeholder="Name" class="form-control"/>
</div>
<div class="col-md-6">
<input type="email" name="email" placeholder="eMail" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="text" name="title" placeholder="Title" class="form-control" required/>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<textarea name="content" cols="40" rows="10" placeholder="Content" class="form-control" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="file" name="attachment" class="form-control">
</div>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="notification"> Get an email, if something happens to the card
</label>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit">
</div>
</form>';
if (isset($_POST['title'])) { $title = $_POST['title']; };
if (isset($_POST['content'])) {$content = $_POST['content']; };
if (isset($_POST['name'])) {$name = $_POST['name']; };
if (isset($_POST['email'])) {$email = $_POST['email']; };
if (isset($_POST['attachment'])) {$file = $_POST['attachment']; };
if (isset($_POST['notification'])) {$notification = $_POST['notification']; };
if(isset($_POST['submit'])) {
trello_handler($title, $content, $name, $email, $file);
db_save($email, $notification);
}
}
add_shortcode('form', 'form');
问题是,当我按下submit按钮时,页面会重新加载,并显示404错误(url完全相同),并且不会保存任何数据库条目
(trello由这个框架管理:设置由)
有人能告诉我,为什么提交的表单不正确吗?找出了问题所在: Wordpress使用“name”intern,因此导致404错误
将“name”改为“clientname”解决了这个问题。我看到您有几个问题。1) 您没有名为
submit
的输入,因此if(isset($\u POST['submit']){
将永远不会发生。2)对于db\u save()
函数,因此我认为如果不设置该变量,该函数将无法工作。从代码中很难判断页面为什么会出现404
错误。谢谢:)我修复了提交按钮和函数调用,但提交时仍然出现404错误:/
function trello_handler($title, $content, $name, $email, $file) {
$titan = TitanFramework::getInstance( 'trello-tools' );
$key = $titan->getOption( 'trello_key' );
$token = $titan->getOption( 'trello_token' );
$trello = new Trello($key, null, $token);
$desc = $content."\r\n Reported by ".$name." (".$email.")";
$trello->actions->post("cards", json_encode(['name' => $title, 'desc' => $desc, 'idList' => "54f882603b3b0af795078283", 'pos' => "top", 'fileSource' => $file ]));
}
function db_save($email, $notification) {
global $wpdb;
$table_name = $wpdb->prefix . 'trello_submits';
$wpdb->insert(
$table_name,
array(
'email' => $email,
'notification' => $notification,
)
);
}