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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)