简体   繁体   English

将php验证表单实现到front-page.php wordpress中

[英]Implementing php validation form into front-page.php wordpress

I got a form here that I am trying to validate on my front page PHP file for my Wordpress site, whenever I click submit it takes me to a post "page not found", why is it even leaving the page? 我在这里得到一个表单,我试图在我的WordPress网站的首页PHP文件上验证,每当我点击提交时它会带我到一个帖子“找不到页面”,为什么它甚至离开页面? not sure what is wrong with my code, I am thinking the submit button must not be right, it shouldn't leave the page. 不确定我的代码有什么问题,我认为提交按钮一定不对,它不应该离开页面。

Maybe I'm not implementing PHP code the right way. 也许我没有以正确的方式实现PHP代码。 The HTML form is at the beginning and PHP code at the end. HTML表单在开头,PHP代码在最后。

<form id="contact-form"  method="post">
                    <ul>
                        <li class="desc">Email</li>

                            <li><input placeholder="i.e.email@email.com" type="" name="email" id="form_email">
                            <span><?php echo $email_error ?></span></li>

                        <li class="desc">First Name</li>

                            <li><input type="text" name="firstName" id="form_firstName" ><span><?php echo $firstName_error ?></span></li>

                        <li class="desc" >Last Name</li>
                            <li><input type="text" name="lastName" id="form_lastName"><span><?php echo $lastName_error ?></span></li>

                        <li class="desc">Company Name</li>
                            <li><input type="text" name="companyName" id="form_companyName"><span><?php echo $companyName_error ?></span></li>

                        <li class="desc">Phone</li>
                            <li><input type="integer" name="phone" id="form_phone"><span><?php echo $phone_error ?></span></li>

                        <li><button name="name" value="submit" type="submit"></button><!--<a href="" id="sub"><img src="images/downloadbutton.png"><input type="hidden" name="submit" value="submit"/></a>--></li>

                    </ul>
                </form>
                <?php
            if(!empty($_POST['email']) && !empty($_POST['firstName']) && !empty($_POST['lastName']) && !empty($_POST['companyName']) && !empty($_POST['phone'])) {
$email = $_POST['email'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$companyName = $_POST['companyName'];
$phone = $_POST['phone'];

if(strlen($email)<5) {
    $email_error = '*';
}
elseif (strlen($firstName)<1) {
    $firstName_error = '*';
}
elseif (strlen($lastName)<1) {
    $lastName_error = '*';
}
elseif (strlen($companyName)<1) {
    $companyName_error = '*';
}
elseif (strlen($phone)<10) {
    $phone_error = '*';
}
else{
    echo " <ul>
            <li>email           : $email </li>
            <li>First Name      : $firstName</li>
            <li>Last Name       : $lastName</li>
            <li>Company Name    : $companyName</li>
            <li>Phone           : $phone</li>
          </ul>
          ";
}
}

else{
        $email_error = '*';
        $firstName_error = '*';
        $lastName_error = '*';
        $companyName_error = '*';
        $phone_error = '*';
}
?>

You can try by adding form action like 您可以尝试添加表单操作

action='<?= get_permalink(page_id); ?>'

page_id = the page which process the form , may be its same page where form is displaying. page_id =处理表单的页面,可以是显示表单的同一页面。

Thnx 日Thnx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM