dpgui8229808 2018-06-02 00:00
浏览 92
已采纳

php验证在同一页面上,然后通过不同的PHP处理

I'm very new to PHP and have managed to learn a bit on my own, but I am getting stuck going in circles on this one. I have a form that will validate to self and show errors on the same page. Once the information is filled and validated, I need it to be processed through a separate PHP script that will send me an email, post the information to a .csv file, and display a confirmation page. I currently have both of theses things working, but not together. In other words, my form will validate to self, but the information goes nowhere. This is obviously useless. Separately, I can have the form do all of the other functions by setting the action to my PHP script, but it doesn't matter if the fields are filled in or not, it sends anything. I have tried everything from sessions, functions, include, and I cannot even remember what else to get this to work. I have even tried to set my errors to an array and then call a TRUE/FALSE function on the results in order to get it to validate and then redirect. I have had results from validating on the same page, but sending all of the info anyway to doing nothing but showing up on a blank page.

Please do not reply that this is a repeat. This is a problem I have been working on and researching for at least a week. I have read many threads on this site and others and not found a working answer.
I am not looking to validate on the client side and then send through PHP. I also need to same page validate and ensure that any previously inputted responses a are kept (which my form currently does) as there are many fields that are required.

I am posting part of my code to keep it to the necessary information. As I said, my validation is working on its own as well as the PHP script if I set the action to run it.

This is a portion of the validation:

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["childsfirstname"])) {
    $childsfirstnameErr = "Child's first name is required.";
  } else {
    $childsfirstname = test_input($_POST["childsfirstname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$childsfirstname)) {
      $childsfirstnameErr = "Only letters and white space allowed";
      die;  
    }
  }

  if (empty($_POST["childslastname"])) {
    $childslastnameErr = "Child's last name is required.";
  } else {
    $childslastname = test_input($_POST["childslastname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$childslastname)) {
      $childslastnameErr = "Only letters and white space allowed";
      die;  
    }
  }

   if (empty($_POST["month"])) {
    $monthErr = "Child's birthday is required.";
  } else {
    $month = test_input($_POST["month"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[0-9]*$/",$month)) {
      $monthErr = "Only numbers allowed";
      die; 
    }
  }
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

This is a portion of the form:

<div id="Field" style="left: 0px; top: 0px"><strong>Youth's:</strong> 
        First Name: 
            <input type="text" name="childsfirstname" style="width: 220px" value="<?php echo $childsfirstname;?>"/>
            &nbsp;&nbsp;&nbsp; 
        Last Name: 
            <input type="text" name="childslastname" style="width: 300px" value="<?php echo $childslastname;?>"/>
    </div>
    <div id="parent" class="error"> 
        <div id="error1a" style="right: 561px"> <?php echo $childsfirstnameErr;?></div>
        <div id="error1b"> <?php echo $childslastnameErr;?></div> 
    </div>

While I realize that there is no code on here to show that I am attempting to redirect for the sending of the information, please trust that I have made more than several attempts and that all I am looking for is the best manner in which to do this and some ACTUAL guidance in how to go about doing it. So long as it works, it doesn't matter to me whether it's include or require, sessions or otherwise as long as it's PHP and Validates to self and then submits to another PHP script once validation is complete and form is error free.

Any and all help is greatly appreciated especially as I am doing this on a volunteer basis. Thank you in advance to any and everyone who can offer a helping hand.

This is the beginning and the end of my PHP script - I'll leave the middle out since there are 16 essentially identically working written validations in all.
if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["childsfirstname"])) {
  $Err[1] = "Child's first name is required.";
 }else {
  $childsfirstname = test_input($_POST["childsfirstname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$childsfirstname)) {
    $Err[1] = "Only letters and white space allowed";
  }
}

  if (empty($_POST["childslastname"])) {
  $Err[2] = "Child's last name is required.";
  } else {
  $childslastname = test_input($_POST["childslastname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$childslastname)) {
    $Err[2] = "Only letters and white space allowed";
  }
}

. . .

if (empty($_POST["date"])) {
$Err[15] = "Date required.";
}else {
$date = test_input($_POST["date"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9]*$/",$date)) {
  $Err[15] = "Letters are not allowed"; 
 }
}

if (empty($_POST["checkbox"])) {
$Err[16] = "VALIDATION REQUIRED";
}else {
$checkbox = test_input($_POST["checkbox"]);
 }
}  

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


?>

This is without any current attempt at passing the form information on - only with validation working. I am continually reverting to what is working so that I don't break something that is working.

  • 写回答

5条回答 默认 最新

  • duanliaouu965826 2018-06-11 17:38
    关注

    Thanks to all that tried to help! Some of the information was helpful in allowing me to find a solution. I was able to use information from @dcromley and @Joseph_J. I set up all of my errors as an array and then used the true/false set up suggested by @dcromley.

         `$Error = "false";
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
          $Error = "true";
          if (empty($_POST["childsfirstname"])) {
        $Error = "false";
          $Err[1] = "Child's first name is required.";    
        }else {
        $childsfirstname = test_input($_POST["childsfirstname"]);
         if (!preg_match("/^[a-zA-Z ]*$/",$childsfirstname)) {
        $Error = "false";
        $Err[1] = "Only letters and white space allowed";
        }
      }
    

    Then I placed a hidden text field in my html to collect the $Error = "true" or $Error = "false" and set a function to include my next php if $Error = "true".

    <input name="Err" type="hidden" value="<?php echo $Error ?>" style="width: 945px"/>
        <div id="submit">
    <label>&nbsp;<strong><input id="submit" name="Submit" type="submit" value="Submit" style="left: 0px; top: 0px" /></strong>
    </label>
        </div>
    &nbsp;</div>
    </form>
    
     <?php 
     if(isset($_POST['Submit']) && ($Error === "true")){ 
     enter();
     }
     ?>
    

    Since I could not (for some unknown reason) get header to work, this is why I used include. In order to hide the form once the confirmation appeared, I did this before the form and at the beginning of the form:

        $style = "";
        if($Error === "true"){
         $style = "style='display:none;'";
        }
    
    ?>
    <div id="wrap">
    <form id="RegForm" method="post" action= "<?php echo htmlspecialchars  ($_SERVER["PHP_SELF"]);?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名