简体   繁体   English

PHP表单处理-使用JavaScript显示错误消息

[英]PHP form handling- display error message using javascript

I have a form.php file that redirects to a sendformdata.php file when I submit the form. 我有一个form.php文件,当我提交表单时,该文件重定向到sendformdata.php文件。 However, I can't get the sendformdata.php file to display an error message within the form when the proper fields aren't filled out. 但是,如果没有填写适当的字段,我无法获取sendformdata.php文件在表单内显示错误消息。

I am simply redirected to the include file, db_connect.php, which shows up as a blank page. 我只是被重定向到包含文件db_connect.php,该文件显示为空白页。 How can I get the page to stay on form.php and display an error message in html? 我如何才能使页面停留在form.php上并在html中显示错误消息? This is my current sendformdata.php file: 这是我当前的sendformdata.php文件:

<?php

include_once('db_connect.php');



$lokotitle = $description = $category = $showyourname = $yourname = $lat = $lng = "";


if($_SERVER['REQUEST_METHOD'] == 'POST'){


  $lokotitle=$_POST['lokotitle'];
  $description=$_POST['description'];
  $category=$_POST['category'];
  $showyourname=$_POST['showyourname'];
  $yourname=$_POST['yourname'];
  $lat=$_POST['lat'];
  $lng=$_POST['lng'];




  // Validation will be added here   


  if(empty($lokotitle)) {
    $error_message = "Please input a Loko title.";
    ?><script>$('#notitle'.text($error_message));</script><?php
    exit;
  }


  if(!isset($error_message)){

    //Inserting record in table using INSERT query

    $insqDbtb="INSERT INTO `new`.`web_form`
    (`lokotitle`, `description`, `category`, `showyourname`, `yourname`, `lat`, `lng`) VALUES ('$lokotitle', '$description', '$category', '$showyourname', '$yourname', '$lat', '$lng')";
    mysqli_query($link,$insqDbtb) or die(mysqli_error($link));

    ?><script>window.location.href = "../index.php"; </script> <?php
    exit;
  }
}
?>

This is a section of form.php that I am trying to display an error message in: 这是form.php的一部分,我试图在其中显示错误消息:

<form class="form-horizontal" role="form"  action="handleform/sendformdata.php" method="POST">
            <legend></legend>
            <div class="form-group">

                <label for="lokotitle" class="col-sm-2">Loko Title</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="lokotitle" id="lokotitle" placeholder="Title">
                    <span id="notitle"></span>
                </div>

Thanks in advance for the help! 先谢谢您的帮助!

I recommend you to use both html5 required attribute which will force the user to enter the data. 我建议您同时使用html5 required属性,这将强制用户输入数据。 But remember the user can bypass front end validation (by means of inspect element), hence back end validation is also necessary. 但是请记住,用户可以绕过前端验证(通过inspect元素),因此后端验证也是必要的。

Use required attribute like: 使用必需的属性,例如:

<input type="text" required />

You can try this 你可以试试这个

  if(empty($lokotitle)) {
    $error_message = "Please input a Loko title.";
     echo "<script>
             document.getElementById('#notitle').value='".$error_message."';".
           "</script>";
    exit;
  }

Let me know if this solves your problem 让我知道这是否解决了您的问题

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

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