添加jQuery验证后,Php表单将无法正确提交

时间:2013-06-21 15:48:13

标签: php jquery forms validation

更新

尝试来自OhGodWhy评论的新代码。当我使用此代码单击提交时,没有任何反应:

        <script type="text/javascript">
         $(document).ready(function(){
          $("#jobapp").validate({
                 debug: false,
           rules: {
            email: {
             required: true,
             email: true
            }
           },
           messages: {
            email: "Please enter a valid email address.",
           },
           submitHandler: function(form) {
            $.ajax('../scripts/process-application.php', new FormData($("#jobapp")[0]), function(data) {
             processData: false;
             $('#confirm').html(data);
             $('#job-application').hide();
            });
           }
          });
         });
        </script>

原始问题

我正在尝试提交包含3个文本字段,富文本字段和文件上传的表单。我已经成功地在另一个表单上使用了jQuery验证,它运行得很好。但是,当我更新此表单的代码时,它不会提交上载的富文本数据或文件。放入名称,电子邮件和电话字段的信息会进入数据库,但最后两个(富文本和文件选择)不会,并且不会发送相应的电子邮件。提交表单后,如果一切都不正常,我会看到我写的信息。

我是PHP的新手,不知道如何解决这个问题。我对jQuery的理解也很有限。我尝试跟随the steps here,但除了输入的消息外,屏幕上看不到任何内容。当我查看FF或Chrome中的开发人员工具时,我在控制台中看不到任何错误消息。如果我删除jQuery验证脚本,一切都很完美。

有人能帮帮我吗?以下是我的表单页面上的js

<script type="text/javascript" src="../assets/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
  $("#jobapp").validate({
         debug: false,
   rules: {
    email: {
     required: true,
     email: true
    }
   },
   messages: {
    email: "Please enter a valid email address.",
   },
   submitHandler: function(form) {
    $.post('../scripts/process-application.php', $("#jobapp").serialize(), function(data) {
     $('#confirm').html(data);
     $('#job-application').hide();
    });
   }
  });
 });
</script>

这是表单的HTML:

<div id="job-application">
<p><span>Position:</span> <?php echo $jobtitle;?></p>
<p><span>Job ID:</span> <?php echo $jobid;?></p>
<p class="req">All Fields are Required</p>
<form action="" method="post" enctype="multipart/form-data" name="jobapp" id="jobapp">
<input name="jobtitle" type="hidden" value="<?php echo $jobtitle;?>" />
<input name="jobid" type="hidden"  value="<?php echo $jobid;?>" />   
 <label>Full Name</label><input name="name" type="text" id="name"/>
 <label>Email</label><input name="email" type="text" id="email" size="35" />
 <label>Phone</label><input name="phone" type="text" id="phone" size="35" />
 <label>Cover Letter</label><textarea name="coverletter" id="coverletter"></textarea>
        <script>
            CKEDITOR.replace( 'coverletter' );
        </script><br />
 <label>Upload Resume:</label><input type="file" name="resume"  />
<input name="Submit" type="submit" id="submit" value="Submit" class="button" />
<input name="clear" type="reset" value="Clear Form"  class="button"/>
</form>
</div>
<div id="confirm"></div>

这是处理应用程序的php文件:

<link href="../assets/lcoastyles.css" media="all" type="text/css" rel="stylesheet" />
<?php require_once('lcoa.php'); ?>
<?php 
 //This is the directory where resumes will be saved 
 $target = "../careers/resumes/"; 
 $target = $target . basename( $_FILES['resume']['name']); 
 $resume=($_FILES['resume']['name']);
 //Writes the information to the database 
$insertSQL = "INSERT INTO applicants (id, name, email, phone, jobid, jobtitle, coverletter, resume) VALUES ('','".$_POST['name']."','".$_POST['email']."','".$_POST['phone']."','".$_POST['jobid']."','".$_POST['jobtitle']."','".$_POST['coverletter']."','".$resume."')";
        mysql_select_db($database_lcoa, $lcoa);
        $Result1 = mysql_query($insertSQL, $lcoa) or die(mysql_error());
//Writes the resume to the server 
 if(move_uploaded_file($_FILES['resume']['tmp_name'], $target)) 
 { 

 //Sends Email
$sendto   = "email@email.com";
$name  = nl2br($_POST['name']);
$email  = nl2br($_POST['email']);
$phone  = nl2br($_POST['phone']);
$jobid = nl2br($_POST['jobid']);
$jobtitle = nl2br($_POST['jobtitle']);
$cover = nl2br($_POST['coverletter']);
$subject  = "Submitted Job Application";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$headers  = "From: " . strip_tags($email) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg  = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>Job Application Submitted</h2>\r\n";
$msg .= "<p><strong>Applied for:</strong> ".$jobtitle."</p>\r\n";
$msg .= "<a href='http://lcoawebservices.com/careers/resumes/".$resume."'>Download Resume</a>\r\n";
$msg .= "</body></html>";

if(@mail($sendto, $subject, $msg, $headers)) {
    echo "";
} else {
    echo "false";
}


 //Tells you if its all ok 
 echo "<p>Thank you for submitting your application.  Resumes submitted will be reviewed to determine qualifications that match our hiring needs.<br /><br />  If you are selected you will be contacted by a member of our recruiting team.</p><br /><br /><a href='../careers/job-postings.php'>Return to current opportunities</a>"; 
 } 
 else { 

 //Gives and error if its not 
 ini_set('display_errors',1); 
 error_reporting(E_ALL);
 echo "Sorry, there was a problem uploading your file. Please try again.<br /><br />If you continue to experience errors, please email lcoaweb@lecreuset.com."; 
 } 
 ?>

我知道这是很多代码,不知道有什么更好的方式来展示你。感谢您对此的关注。

0 个答案:

没有答案