我在Wordpress页面中编写了一个自定义注册表,它收集表单详细信息,然后尝试提交到数据库中的用户表。
当我提交时,页面(非故意)重定向到主页,不提交用户。
你能看到我的代码有什么问题吗?如果您想知道“[插入PHP]”标记,我正在使用“插入PHP”插件。此外,开发表单时调试表单的最简单方法是什么
[insert_php]
// Initialise variables
$name = "";
$password = "";
$email = "";
$phone = "";
$how = "";
$type = "";
// Sanitise fields
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["inputUsername"]);
$password = test_input($_POST["inputPassword"]);
$email = test_input($_POST["inputEmail"]);
$phone = test_input($_POST["inputPhone"]);
$how = test_input($_POST["inputHow"]);
$type = test_input($_POST["inputType"]);
}
// Validate fields
registration_validation(
$name,
$password,
$email,
$phone,
$how,
$type
);
// Attempt to submit to database.
complete_registration(
$name,
$password,
$email,
$phone,
$how,
$type
);
// Strip any special characters out of the input.
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Do specific checks e.g. length of username etc.
function registration_validation($name, $password, $email, $phone, $how, $type)
{
global $reg_errors;
$reg_errors = new WP_Error;
if (empty($name) || empty($password) || empty($email) || empty($phone) || empty($how) || empty($type))
{
$reg_errors->add(\'field\', \'Required form field is missing\');
}
if (is_wp_error($reg_errors))
{
foreach ($reg_errors->get_error_messages() as $error)
{
echo \'<div>\';
echo \'<strong>ERROR</strong>:\';
echo $error . \'<br/>\';
echo \'</div>\';
}
}
}
// Submit to the database.
function complete_registration($name, $password, $email, $phone, $how, $type)
{
global $reg_errors;
if ( 1 > count( $reg_errors->get_error_messages() ) )
{
$userdata = array(
\'user_login\' => $name,
\'user_email\' => $email,
\'user_pass\' => $password,
\'user_phone\' => $phone,
\'user_type\' => $type,
\'user_how\' => $how
);
$user = wp_insert_user( $userdata );
echo \'Registration complete. Goto <a href="\' . get_site_url() . \'/wp-login.php">login page</a>.\';
}
}
[/insert_php]
<form method="post" action="[insert_php] echo htmlspecialchars($_SERVER["PHP_SELF"]);[/insert_php]">
<div class="form-group">
<label for="inputUsername">Username</label>
<input type="text" class="form-control" id="inputUsername">
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" id="inputPassword">
</div>
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="name@example.com">
</div>
<div class="form-group">
<label for="inputPhone">Contact Phone Number</label>
<input type="password" class="form-control" id="inputPhone">
</div>
<div class="form-group">
<label for="how">How did you hear about us?</label>
<select class="form-control" id="how">
<option>Word of mouth</option>
<option>Local paper</option>
<option>Search engine</option>
<option>Other</option>
</select>
</div>
<div class="form-group">
<label for="signupType">I am a:</label>
<select class="form-control" id="signupType">
<option>Parent / Guardian</option>
<option>Student</option>
</select>
</div>
<button type="submit" value="Submit" class="btn btn-primary">Submit</button>
</form>