duan0708676887 2014-02-21 09:11
浏览 41
已采纳

PHP验证错误消息顺序

When a user submits the form, all the POST data gets sent across to salesforce. I am currently working on improving the validation process on the server.

When the user miss out the all the fields, the validation process picks up company name as the error message.

Is there anyway to place the validation in order e.g. if none of the fields are filled out, first name is the first error message shown.

Please review the code below.

index.php

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<input type='hidden' name="lead_source" value="web-to-lead">
<input type='hidden' name="Campaign_ID" value="campaignid">
<input type='hidden' name="oid" value="uniqueid">
<input type='hidden' name="ididid" value="Grade 3 (Hot)">
<input type='hidden' name="retURL" value="www.niceic.com">
<input type="hidden" id="recordType" name="recordType" value="ididididid">
<input type='hidden' name="designgenieid" value="Design Genie Webinar">

<div class="errorMessage"><?php if(isset($error)){ echo $error; } ?></div>

<label for="first_name">First Name</label>
<input  id="first_name" name="first_name" size="20" type="text" value="<?php echo $_POST['first_name']; ?>" /><br>

<label for="last_name">Last Name</label>
<input  id="last_name" name="last_name" size="20" type="text" value="<?php echo $_POST['last_name']; ?>" /><br>

<label for="phone">Phone</label>
<input  id="phone" name="phone" size="20" type="text" value="<?php echo $_POST['phone']; ?>" /><br>

<label for="email">Email</label>
<input  id="email" name="email" size="20" type="text" value="<?php echo $_POST['email']; ?>"  /><br>

<label for="company">Company</label>
<input  id="company" name="company" size="20" type="text" value="<?php echo $_POST['company']; ?>"  /><br>

<label for="00ND0000005gYZo">Question<input id="00ND0000005gYZo" maxlength="80" name="00ND0000005gYZo" value="<?php echo $_POST['00ND0000005gYZo']; ?>" type="text">

<br><input type="submit" name="submit">

</form>

config.php

<?php

//Initialize the $query_string variable for later use
$query_string = "";



if(isset($_POST['submit'])){

    if ($_POST['first_name'] == "") {
        $error="Please enter in your first name<br>";
    } elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST['first_name'])) {
                $error="Only letters and white space allowed<br>";
        }



    if($_POST['last_name'] == ""){
        $error="Please enter in your last name<br>";
        }elseif(!preg_match("/^[a-zA-Z ]*$/",$_POST['last_name'])){
                    $error="Only letters and white space allowed<br>";
            }

    if($_POST['phone'] == ""){
        $error="Please enter in your phone number<br>"; 
    }elseif(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $_POST['phone'])){
      $error="Please enter in a valid phone number<br>";    
        }

    if($_POST['email'] == ""){
        $error="Please enter in your email<br>";
    }else if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])){
            $error="Please enter in a valid email address<br>";
        }

    if($_POST['company'] == ""){
        $error="Please enter in your Company details<br>";
        } elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST['first_name'])) {
                    $error="Only letters and white space allowed<br>";
            }


    if(isset($error)){
        //echo $error;
    }else{
            if ($_POST) {

            //Initialize the $kv array for later use
            $kv = array();

            //For each POST variable as $name_of_input_field => $value_of_input_field
            foreach ($_POST as $key => $value) {

            //Set array element for each POST variable (ie. first_name=Arsham)
            $kv[] = stripslashes($key)."=".stripslashes($value);


            }


            //Create a query string with join function separted by &
            $query_string = join("&", $kv);
            }
            //Check to see if cURL is installed ...
            if (!function_exists('curl_init')){
            die('Sorry cURL is not installed!');
            }

            //The original form action URL from Step 2 :)
            $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

            //Open cURL connection
            $ch = curl_init();


            //Set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, count($kv));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);

            //Set some settings that make it all work :)
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

            //Execute SalesForce web to lead PHP cURL
            $result = curl_exec($ch);

            //close cURL connection
            curl_close($ch);
    }

};


?>
  • 写回答

3条回答 默认 最新

  • dqm83011 2014-02-21 09:19
    关注

    All you have to do is to initialize the $error before you check and instead of setting it like

    $error = "Please enter in your first name<br>";
    

    You should only append all the errors to the string like this:

    $error .= "Please enter in your first name<br>";
    

    Your code should look like this:

    if(isset($_POST['submit']))
    {
    $error = "";
    
    if ($_POST['first_name'] == "") {
        $error .= "Please enter in your first name<br>";
    } elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST['first_name'])) {
                $error .= "Only letters and white space allowed<br>";
        }
    
    
    
    if($_POST['last_name'] == ""){
        $error .= "Please enter in your last name<br>";
        }elseif(!preg_match("/^[a-zA-Z ]*$/",$_POST['last_name'])){
                    $error .= "Only letters and white space allowed<br>";
            }
    
    ...
    
    if(isset($error) && trim($error) != ""){
        //echo $error;
    }else{
    ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据