PHP表单验证和发布工作,但不在一起? [英] Php form validate and post are working but not together?

查看:100
本文介绍了PHP表单验证和发布工作,但不在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了正常工作,我需要设置表单以首先检查验证,然后在验证通过时发布数据。这很好,但我不确定如何将验证代码与表单操作中的邮政代码结合起来。例如,如果动作是: action =<?php echo htmlspecialchars($ _ SERVER [PHP_SELF]);?>> 表单验证正确但是不发送任何地方!如果我将表单操作更改为 action =contact-engine.php> ,那么表单会被发布但没有验证!考虑到这一点,我需要结合到验证行动中,然后(一旦通过验证)contact-engine.php问题是我根本不知道如何做到这一点?我真的是一名PHP学习者,这对我来说很复杂!任何帮助真的很感谢,我从现在开始为此工作了几天! (N.B. both pages are .php)完整代码如下:

In order to work properly my form needs to be set up to first check the validation and then post the data if the validation passes. This is fine but I am not sure how to combine the validation code with the post code in the form action. Example if the action is: action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> the form validates correctly but does not send anywhere! If I change the form action to action="contact-engine.php"> then the form is posted but without validation! With this in mind I need to combine into the action both the validation and then (once passed validation) the contact-engine.php problem is I simply do not know how to do this? I really am a learner in php and this is complicated for me! Any help is really appreciated I have been working on this from now for a few days! (N.B. both pages are .php) Full code is as follows:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Help!</title>

    <style type="text/css">
        .error {color: #FF0000;}
    </style>
</head>

<body>

<?php
    // define variables and set to empty values
    $nameErr = $emailErr = "";
    $name = $email = "";

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

//Name
    if (empty($_POST["name"]))
    {$nameErr = "Name is required";}
    else
    {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
    {
    $nameErr = "Only letters and white space allowed";
    }}

//Email
   if (empty($_POST["email"]))
   {$emailErr = "Email is required";}
   else
   {
   $email = test_input($_POST["email"]);
   if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
   {
   $emailErr = "Invalid email format";
   }}
}
function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>

<form method="post" id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <p><span class="error">* required field.</span></p><br />

    <div class="contact-font" style=" margin-top: 20px;">
        <span class="asterix">* </span>Name:<br />
        <input type="text" name="name" class="border" size="25" value="<?php if(isset($_POST['name'])) {echo $_POST['name']; } ?>">
        <span class="error"><?php echo $nameErr;?></span>
    </div>

    <div class="contact-font" style=" margin-top: 20px;">
        <span class="asterix">* </span>Email: (please double check enty)<br />
        <input type="text" name="email" class="border" size="25" value="<?php if(isset($_POST['email'])) {echo $_POST['email']; } ?>"><span class="error">
        <?php echo $emailErr;?></span>
    </div>

    <div>
        <input type="submit" value="Send" id="submit">
    </div>
</form>

</body>
</html>


And below is the contact-engine code:
<html>
<head>
    <title>Contact Engine</title>
</head>

<body>

    <br />Name:<?php echo htmlspecialchars($_POST['name']); ?><br />
    <br />Email:<?php echo htmlspecialchars($_POST['email']); ?><br />

</body>
</html>


推荐答案

您可以试试这个。这是一个简单的代码,您可以通过它来实现此目的:

You can try this. This is a simple code below through which you can achieve this:

//sample index.php file
<?php
  include 'submitted.php';
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
      <input type="text" name="foo" />
      <input type="submit" value="submit">
</form>
</body>
</html>

这里,我使用post方法将表单提交到同一页面。但是,我已经包含另一个文件,它是 include'submitted.php'

Here, I am submitting the form to the same page using post method. But, I have included another file which is include 'submitted.php'.

以下是 submitted.php中的测试脚本

//sample submitted.php
<?php
  if(isset($_POST['foo']))
 {
    if(strlen($_POST['foo']) < 5){
  echo "String length too small";
  }
  else
  {
   echo $_POST['foo'];
  }
 }
?>

测试时,只需检查长度是否超过5。如果不是,则错误消息会显示在您的网页所在的同一页面上。

For test, it simply checks if the length is more than five or not. If it is not, the error message is displayed on the same page where your page is present.

您自己也要测试它。

这篇关于PHP表单验证和发布工作,但不在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆