我正在尝试制作自己的模板,该模板允许我将自定义数据插入表中,但是在测试表单时未提交POST
数据。这是我当前的代码:
<?php /* Template Name: signup-volunteers */
get_header(); ?>
<header class="archive-header">
<div class="cover-container row">
<div class="inner cover col-md-12">
<h1 class="cover-heading"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<p class="lead"><?php bloginfo( 'description' ); ?></p>
</div>
</div>
</header>
<div class="container blog">
<div class="row">
<?php
$theme_layout = get_theme_mod( 'latte_blog_sidebar', 'full' );
if ($theme_layout=="left") :
get_sidebar();
endif;
?>
<?php if ($theme_layout=="full") : ?>
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<?php else: ?>
<div class="col-lg-8 col-md-8">
<?php endif; ?>
<?php the_post() ?>
<?php get_template_part( 'content', 'page' );
<?php
if (isset($_POST['submit'])) :
// Process the form
global $wpdb;
$wpdb->show_errors(true);
$table = $wpdb->prefix.'volunteers';
var_dump($_POST);
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'city' => $_POST['city'],
'skills' => $_POST['skills']
);
$format = array(
'%s',
'%s',
'%s',
'%s'
);
$result = $wpdb->insert($table, $data, $format);
if ($result):
echo 'Successfully saved the data.';
else:
$wpdb->print_error();
endif;
else :
?>
<form method="post" action="">
<label for="name">Full Name: </label> <input type="text" name="name" id="name" required/>
<label for="email">Email: </label> <input type="email" name="email" id="email" required/>
<label for="city">City or Township: </label> <input type="text" name="email" id="city" required/>
<label for="skills">Skills: </label> <input type="text" name="skills" id="skills" />
<br />
<input type="submit" name="submit">
</form>
<br />
</div>
</div>
</div>
<?php endif;
get_footer(); ?>
我尝试添加一些调试语句以查看$_POST
数据,但是当我这样做时,它们全部以NULL
的形式出现。我不确定自己在做什么错,因为我的代码与我在其他帖子上看到的其他版本匹配,成为批准的答案。
答案 0 :(得分:0)
结果是我在WordPress中劫持了一个变量名。不要将POST变量命名为“ name”或“ email”。给它加上前缀:
<?php /* Template Name: signup-volunteers */
var_dump($_POST);
get_header(); ?>
<?php
if (isset($_POST['submit'])) :
// Process the form
global $wpdb;
$wpdb->show_errors(true);
$table = $wpdb->prefix.'volunteers';
$data = array(
'name' => sanitize_text_field($_POST['vol_name']),
'email' => sanitize_email($_POST['vol_email']),
'city' => sanitize_text_field($_POST['vol_city']),
'skills' => sanitize_text_field($_POST['vol_skills'])
);
$format = array(
'%s',
'%s',
'%s',
'%s'
);
$result = $wpdb->insert($table, $data, $format);
if ($result):
echo 'Successfully saved the data.';
else:
$wpdb->print_error();
endif;
else :
?>
<header class="archive-header">
<div class="cover-container row">
<div class="inner cover col-md-12">
<h1 class="cover-heading"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<p class="lead"><?php bloginfo( 'description' ); ?></p>
</div>
</div>
</header>
<div class="container blog">
<div class="row">
<?php
$theme_layout = get_theme_mod( 'latte_blog_sidebar', 'full' );
if ($theme_layout=="left") :
get_sidebar();
endif;
?>
<?php if ($theme_layout=="full") : ?>
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<?php else: ?>
<div class="col-lg-8 col-md-8">
<?php endif; ?>
<?php the_post() ?>
<?php get_template_part( 'content', 'page' ); ?>
<form method="post">
<label for="vol_name">Full Name: </label> <input type="text" name="vol_name" id="vol_name" required/>
<label for="vol_email">Email: </label> <input type="email" name="vol_email" id="vol_email" required/>
<label for="vol_city">City or Township: </label> <input type="text" name="vol_city" id="vol_city" required/>
<label for="vol_skills">Skills: </label> <input type="text" name="vol_skills" id="vol_skills" />
<br />
<input type="submit" name="submit">
</form>
<br />
</div>
</div>
</div>
<?php endif;
get_footer(); ?>
以上代码有效。在这里找到解决方案:https://wordpress.stackexchange.com/questions/11749/why-when-i-submit-a-form-in-wordpress-it-loads-a-404-page-though-url-is-correct