duanfoumi5620 2018-09-25 16:35
浏览 47

PHP单页表单验证表单未验证

Nothing happens when you click the submit button at the bottom of the page. I simply want it to validate user input and I am only focused on the name field at the moment and I cannot get it to validate any input in the name field. No error messages pop up or anything. Please review this and offer any suggestions, I cannot find my error.

PHP portion, where variables are initialized and set to empty. As well as the post methods and isset functions

<?php


//define variables and set them to empty values    

$fname_error= $phone_error= $address1_error= $address2_error= $city_error= $state_error= $zipcode_error= "";

$fname= $phone= $address1= $address2= $city= $state= $zipcode= "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["fname"])) {
        $fname_error = "Missing";
    }
    else {
        $fname = test_input($_POST["fname"]);
        //now we check to see that the name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
            $fname_error = "Please use letters and white space only";
        }
    }

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>

The Html portion:

                               <div class="userinput">
                                    <label for="fname"><b>First Name</b></label>
                                    <input type="text" name="fname" value="<?php 
                                         echo $fname ?>">
                                    <span class="error">
                                        <?php echo $fname_error;?></span>
                                </div>
  • 写回答

1条回答 默认 最新

  • dpn517111 2018-09-25 16:44
    关注

    Good day. This is just a hypothesis, I may be wrong as I couldn't check the entire code, but you cannot have more than 1 form on the same page. Because, you need a single opening and closing form tag that wraps ALL form elements on your page. Form fields are only counted as part of a form if they are contained within the form elements. And you do have more than 1 form on the same page.

    Also, you should consider minimizing your code to only what's needed.

    Hope this helps!!!

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算