Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php noob:在页面上显示错误消息(第2部分)_Php_Javascript_Jquery_Error Handling_Validation - Fatal编程技术网

Php noob:在页面上显示错误消息(第2部分)

Php noob:在页面上显示错误消息(第2部分),php,javascript,jquery,error-handling,validation,Php,Javascript,Jquery,Error Handling,Validation,我昨天发布了这篇文章,如果你回复了,并且再次看到这篇文章感到沮丧,我向你道歉!但是这个表单现在根本不起作用,我非常感谢您能帮我解决这个问题 我有一个表单,它将数据发送到数据库,并且需要验证。昨天它至少验证正确,但今天我甚至不能让它正确地挂接到我的操作页面。以下是两页: 第w页/表格: <?php session_start(); $form_field = array( 'First_Name'

我昨天发布了这篇文章,如果你回复了,并且再次看到这篇文章感到沮丧,我向你道歉!但是这个表单现在根本不起作用,我非常感谢您能帮我解决这个问题

我有一个表单,它将数据发送到数据库,并且需要验证。昨天它至少验证正确,但今天我甚至不能让它正确地挂接到我的操作页面。以下是两页:

第w页/表格:

    <?php
    session_start();
    $form_field =   array(  
                                'First_Name'                =>  '',
                                'Last_Name'             =>  '',
                                'Business_Name'             =>  '',
                                'Phone_Number'              =>  '',
                                'Web_Address'               =>  '',
                                'Email_Address'             =>  '',
                                'How_selling_product'           =>  '',
                                'Where_did_you_hear_about_us'   =>  '',
                            );
    foreach($form_field as $key => $value){$profiledata[$key]='';}              

    if(!empty($_SESSION['profiledata'])){   
        $profiledata    =   $_SESSION['profiledata'];
    }

    ?>

    [js]
    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_validateForm() { //v4.0
      var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

      errors    += addValidation("req","first_name","- First Name is required.");
      errors    += addValidation("req","last_name","- Last Name is required.");

      for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
        if (val) { nm=val.name; if ((val=val.value)!="") {
          if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
          } else if (test!='R') { num = parseFloat(val);
            if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
              min=test.substring(8,p); max=test.substring(p+1);
              if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
        } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
      } 

      errors    += addValidation("req","security_code","- Security Code is required."); 

      if(!errors){
        errors += addValidation("valid_lastname","first_name","- Please enter valid First Name");
        errors += addValidation("valid_lastname","last_name","- Please enter valid Last Name");
        errors += addValidation("numhyphenbrace","phone number","- Please enter valid Phone Number");
        errors += addValidation("valid_url","web address","- Please Enter Valid Web Address");
        errors += addValidation("email","email address","- Please Enter Valid Email");

      }
      if (errors) alert('The following error(s) occurred:\n'+errors);
      document.MM_returnValue = (errors == '');
    }
    [/js]

    <h1>Some content</h1>
    <p>Some text</p>

    <form id="form1" name="form1" method="post" action="mailform" onsubmit="MM_validateForm('Name','','R','Business Name','','R','Email Address','','R','How selling product','','R','Where did you hear about us','','R');return document.MM_returnValue">
        <div style="color:#FF0000; text-align:center;"><?php if(!empty($_GET['err_msg'])){echo $_GET['err_msg'];} ?></div>
            <fieldset>
                        <legend>Contact form</legend>
                    <p class="first">
                        <label for="name">First Name</label>
                        <input type="text" name="First Name" id="first_name" size="30" value="<?=htmlentities($profiledata['First_Name'])?>" />
                    </p>
                            <p class="first">
                        <label for="name">Last Name</label>
                        <input type="text" name="Last Name" id="last_name" size="30" value="<?=htmlentities($profiledata['Last_Name'])?>" />
                    </p>                
                    <p>
                        <label for="name">Phone Number</label>
                        <input type="text" name="Phone Number" id="phone number" size="30" value="<?=$profiledata['Phone_Number']?>" />
                    </p>
                    <p>
                        <label for="email">Business Name</label>
                        <input type="text" name="Business Name" id="business name" size="30" value="<?=htmlentities($profiledata['Business_Name'])?>" />
                    </p>
                    <p>
                        <label for="email">Web Address</label>
                        <input type="text" name="Web Address" id="web address" size="30" value="<?=$profiledata['Web_Address']?>" />
                    </p>
                    <p>
                        <label for="email">Email</label>
                        <input type="text" name="Email Address" id="email address" size="30" value="<?=$profiledata['Email_Address']?>" />
                    </p>            
            </fieldset>
            <fieldset>                                                                          
                    <p>
                        <label for="message">Describe how you plan on selling this product</label>
                        <textarea name="How selling product" id="How selling product" cols="30" rows="4"><?=htmlentities($profiledata['How_selling_product'])?></textarea>
                    </p>    
                    <p>
                        <label for="message">Where did you hear about us?</label>
                        <textarea name="Where did you hear about us" id="Where did you hear about us" cols="30" rows="4"><?=htmlentities($profiledata['Where_did_you_hear_about_us'])?></textarea>
                    </p>
                            <p>
                                <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
                        <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" />
                            </p>                
            </fieldset>
                <p class="submit"><button type="submit">Send</button></p>
                <input name="mailform_address" type="hidden" value="email@address.com" />
    </form>

[js]
函数MM_findObj(n,d){//v4.01
变量p,i,x;如果(!d)d=document;如果((p=n.indexOf(“?”)>0&&parent.frames.length){
d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}

如果(!(x=d[n])&&d.all)x=d.all[n];对于(i=0;!x&&i我承认我没有彻底阅读您的代码,但其中任何一个都可能是您的问题:

  • 在脚本中调用
    MM_validateForm()
    时,我认为您需要传递HTML表单字段的名称,但尽管每个参数似乎都正确,但第一个参数是“Name”,它似乎不是有效的表单字段

  • MM\u validateForm()
    中的函数
    addValidation()
    是什么?它的作用是什么?因为我看不到它的定义,我假设它可能会在JS中生成错误

  • MM_validateForm()
    中,在第3行和第4行之后,变量
    errors
    似乎从来都不是空的,从而阻止了表单的提交


  • 我不太清楚你在哪一点上被卡住了-你的表单根本没有提交吗?你有没有尝试在
    MM\u validateForm()
    的末尾发出
    错误的警告?也许会有帮助。

    注意:这是一个WordPress站点。[js][/js]标签正与一个允许在页面上使用js的插件一起使用。你知道从它开始工作到现在,代码发生了什么变化吗?我知道这似乎很明显,但我想我会问一下。我知道有时很难确定可能发生了什么变化。如果你不使用某种形式的源代码控制,我会推荐它。比如Gitor SVN等。我知道这并不能回答您的问题,但我希望这对将来有所帮助:)不,@M3NTA7,我找不到对代码的任何更改。我还没有实现任何WP或插件更新。我还扫描了FTP文件夹的日期,看看是否有任何文件发生了更改,但什么也找不到……好主意,谢谢。表单以前没有提交,但现在至少验证了
    mailform.php的url中的错误
    。我确实发现我犯了一个错误-
    action=“mailform.php”
    应该是
    action=“mailform”
    ,我在上面的代码中做了更改。所以-现在我在url中进行了错误验证,但我希望它在他们点击提交时出现在表单页面上。很抱歉,我不太擅长php或js-我只是在尝试维护此代码。
    <?php 
    session_start(); 
    include("connect.php");
    
    function valid_email($str)
    {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }
    
    function is_url($str)
    {
        return ( ! preg_match("/^((www)\.)?((\w+|[\d]?+)+(\.|\-)(\w+[\d]?+))+(\w)$/", $str)) ? FALSE : TRUE;
    }
    
    function valid_phone($str)
    {
        $Num = $str;
        $Num = ereg_replace("([     ]+)","",$Num);;
        $Num = eregi_replace("(\(|\)|\-|\+)","",$Num);
        if(!is_numeric($Num))
        {       
            return FALSE;
        }   
        else
            return TRUE;
    }
    
    $form_field =   array(  
                                'First_Name'                    =>  '',
                                'Last_Name'                     =>  '',
                                'Business_Name'                 =>  '',
                                'Phone_Number'                  =>  '',
                                'Web_Address'                   =>  '',
                                'Email_Address'                 =>  '',
                                'How_selling_product'           =>  '',
                                'Where_did_you_hear_about_us'   =>  '',
                            );
    foreach($form_field as $key => $value){$profiledata[$key]=trim($_POST[$key]);}
    
    $_SESSION['profiledata']    =   $profiledata;
    
    $emailto = NULL;
    $emailmessage = "Dealer Contact Form\n";
    $emailsubject = "Dealer Contact Form";
    
    if(!empty($_POST)){
    
        //echo "<pre>";print_r($_POST);die;
    
        if( $_SESSION['security_code'] != $_POST['security_code'] || empty($_SESSION['security_code'] ) ) {
    
            // Insert your code for showing an error message here
            $err_msg    = 'Sorry, you have provided an invalid security code';
            header("Location: reseller.php?err_msg=".urlencode($err_msg));
            return;
    
        } else {
            unset($_SESSION['security_code']);  
        }
    
        $fname      =   html_entity_decode(trim($_POST['First_Name']));
        $lname      =   html_entity_decode(trim($_POST['Last_Name']));
        $company    =   html_entity_decode(trim($_POST['Business_Name']));
        $phone      =   html_entity_decode(trim($_POST['Phone_Number']));
        $website    =   html_entity_decode(trim($_POST['Web_Address']));
        $email      =   html_entity_decode(trim($_POST['Email_Address']));
        $notes      =   "Lead Source: ".html_entity_decode(trim($_POST['Where_did_you_hear_about_us']))."\n";
        $notes      .=  "Selling Method: ".html_entity_decode(trim($_POST['How_selling_product']));
    
        if(!valid_phone($phone)){
            $err_msg    = 'Please enter valid Phone Number.';
            header("Location: reseller.php?err_msg=".urlencode($err_msg));
            return;
        }
    
        if(!is_url($website)){
            $err_msg    = 'Please enter valid Web Address.';
            header("Location: reseller.php?err_msg=".urlencode($err_msg));
            return;
        }
    
        if(!valid_email($email)){
            $err_msg    = 'Please enter valid Email.';
            header("Location: reseller.php?err_msg=".urlencode($err_msg));
            return;
        }
    
        if(!stristr($website,"http://") && !stristr($website,"https://") && $website){
            $website    =   "http://".$website;
        }
    
        $res    =   mysql_query("SELECT in_customer_id FROM tbl_customer WHERE st_company_name = '".addslashes($company)."'");
        if(mysql_num_rows($res)){   
            $err_msg    =   'Business Name already exists';
            header("Location: reseller.php?err_msg=".urlencode($err_msg));
            return;
        }
    
        $res    =   mysql_query("SELECT st_user_name,st_user_email_id FROM tbl_admin_user WHERE st_user_email_id='".addslashes($email)."' AND flg_is_delete=0");
        if(mysql_num_rows($res)){   
            $err_msg    =   'Email already exists';
            header("Location: reseller.php?err_msg=".urlencode($err_msg));
            return;
        }
    
        $sql_customer   =   "INSERT INTO tbl_customer (`st_company_name`, `in_customer_type`, `st_customer_account`, `in_customer_phone_number`, `flg_customer_account_type`, `flg_disable_ordering`,  `st_message`, `in_status`, `dt_added_date`, `st_web`, `st_notes`) VALUES ('".addslashes($company)."', '2', '".time()."', '".addslashes($phone)."', '0', '1', 'You are an Inquiry customer', '0', '".date("Y-m-d H:i:s")."', '".addslashes($website)."', '".addslashes($notes)."')";
    
        mysql_query($sql_customer);
        $cust_id        =   mysql_insert_id();
    
        if($cust_id){
            $sql_user       =   "INSERT INTO tbl_admin_user (`st_user_first_name`, `st_user_last_name`, `st_user_company_name`, `st_user_email_id`, `in_customer_id`) VALUES ('".addslashes($fname)."', '".addslashes($lname)."', '".addslashes($company)."', '".addslashes($email)."', '".$cust_id."')";
            mysql_query($sql_user);
    
            $msg    =   "";
            $sub    =   "Thank you for contacting us.";
    
            ob_start();
            include("mailformat.php");
            $msg =  ob_get_contents();
            ob_end_clean();
    
            $headers    =   ''; 
            //$headers = "MIME-Version: 1.0\r\n";               
            $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
            $headers .= "From: " . $_POST['mailform_address'] . "\r\n";
            $headers .= "Reply-To: ". $_POST['mailform_address'] . "\r\n";
            $headers .= "Bcc: email@address.com";
    
            if (@mail($email, $sub, $msg, $headers))
            {
                session_unset('profiledata');
            }
        }
    
        foreach($_POST as $name => $value)
        {
            if ($name == 'mailform_address')
            {
                $emailto = $value;
            }
            elseif($name    !=  "security_code")
            {
                $emailmessage .= $name . ": " . $value . "\n";
            }
        }
        session_unset('profiledata');
    }
    
    ?>
    
        <?            
            if (@mail($emailto, $emailsubject, $emailmessage))
            {
            ?>
    <p>We'll be in touch within two business days.  If you haven't heard back from us within two business days, please <a href="contact">contact us</a></p>
        <?
            }
            else
            {
            ?>    
    <p>E-mail could not be sent. Please <a href="contact">contact us</a>.</p>   
            <?
            }
            ?>