PHP表单条件语句

时间:2012-05-31 07:06:34

标签: php forms contact

我有一张现有验证码的联系表格。我创建了表单并将上传功能合并到其中。问题是,当我尝试提交没有验证码的表格时,它没有显示js警报或无效验证码警报但是当我试图附加图像然后提交没有验证码的表格时,它被提交..代码现在一团糟。任何人都可以查看有条件的陈述,看看你是否可以帮我解决这个问题?我不擅长PHP。

这是我的mail.php

        <?php
if($_SESSION["captcha"] == $_POST["captcha"]) {

  //if($_SESSION["captcha"] == $_POST["captcha"]) {

   // we'll begin by assigning the To address and message subject
   $to="myemail@gmail.com";

   $subject="E-mail with attachment";

   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // store the file information to variables for easier access
   $tmp_name = $_FILES['filename']['tmp_name'];
   $type = $_FILES['filename']['type'];

   $size = $_FILES['filename']['size'];

   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = 'Message from '.$_REQUEST['name'].'


    Full Name: '.$_REQUEST['name'].'
    Address: '.$_REQUEST['address'].'
    Contact Number: '.$_REQUEST['contact'].'
    Email: '.$_REQUEST['email'].'
    How do you come to know about us: '.$_REQUEST['type'].'
    Car Make: '.$_REQUEST['make'].'
    Car Model: '.$_REQUEST['model'].'
    Year: '.$_REQUEST['year'].'
    Registration: '.$_REQUEST['registration'].'
    Comment: '.$_REQUEST['comments'].'
   ';

   // if the upload succeded, the file will exist
   if (file_exists($tmp_name)){

      // check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){

         // open the file for a binary read
         $file = fopen($tmp_name,'rb');

         // read the file content into a variable
         $data = fread($file,filesize($tmp_name));

         // close the file
         fclose($file);

         // now we encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));
     }

      // now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // next, we'll build the message body
      // note that we insert two dashes in front of the
      // MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

      // now we'll insert a boundary to indicate we're starting the attachment
      // we have to specify the content type, file name, and disposition as
      // an attachment, then add the file content and set another boundary to
      // indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // now we just send the message
      if (@mail($to, $subject, $message, $headers)){
        echo "<script>alert('Your details has been Sent Successfully, we shall revert back to you shortly!!!');</script>";
        echo "<script>window.location.href='index.php';</script>";
      } else {
         echo "<script>alert('Sending Failed');</script>";
         echo "<script>window.location.href='index.php';</script>";
   } }
} else {
    echo "<script>window.location.href='index.php?cap=invalid';</script>";
}
?>

以下是表格:

    <div class="quote-wrap left">
<form name="quoteForm" enctype="multipart/form-data" method="post" action="mail.php" onsubmit="return validateQuoteForm();">
<div class="quote-table left liquid-size">
<table width="100%">
    <tr>
        <td colspan="2" align="center" valign="middle" style="padding-bottom:0px;"><?php $qerror ?></td>
    </tr>                                               
    <tr>
        <td colspan="2" align="center" valign="middle" style="padding-bottom:0px; color:#FF0000;"><h3><?php $captcha_stat?></h3></td>
    </tr>
    <tr>
        <td valign="middle" align="left">
        <input type="text" name="make" placeholder="Make:" class="input-field" value="$make" />
        <span class="required">*</span> 
        </td>
    </tr>
        <tr>
        <td valign="middle" align="left">
        <input type="text" name="model" placeholder="Model:" class="input-field" value="$model" /><span class="required">*</span></td>
    </tr>
    <tr>
        <td valign="middle" align="left">
        <input type="text" name="year" placeholder="Year:" class="input-field" value="$year"/>
        <span class="required">*</span>                                                  
    </td>
    </tr>
        <tr>
        <td valign="middle" align="left">
        <input type="text" name="registration" placeholder="Registration:" class="input-field" value="$registration" /><span class="required">*</span></td>
    </tr>
</table>
</div>

<div class="quote-table left liquid-size">
<table width="100%">
    <tr>
        <td valign="middle" align="left">
        <input type="text" name="address" placeholder="Full Address:" class="input-field" value="$address" /><span class="required">*</span></td>
    </tr>
    <tr>
        <td valign="middle" align="left">
        <input type="text" name="name" placeholder="Name:" class="input-field"  value="$name"/><span class="required">*</span></td>
    </tr>
    <tr>
        <td valign="middle" align="left">
        <input type="text" name="contact" placeholder="Contact No:" class="input-field"  value="$contact"/><span class="required">*</span></td>
    </tr>
    <tr>
        <td valign="middle" align="left">
        <input type="text" name="email" placeholder="Email:" class="input-field" value="$email" /><span class="required">*</span></td>
    </tr>                                           
    <tr>
        <td valign="middle" align="left">How do you come to know about Us?</td>
    </tr>
    <tr>
        <td valign="middle" align="left">
        <select class="quotable-select" name="type">
        <option value="Google search">Google search</option>
        <option value="Yahoo search">Yahoo search</option>
        <option value="Bing search">Bing search</option>
        <option value="Hotfrog">Hotfrog</option>
        <option value="Local Newspaper">Local Newspaper</option>
        <option value="Magazine">Magazine</option>
        <option value="Returning Customer">Returning Customer</option>
        <option value="Heard about us">Heard about us</option>
        <option value="Saw us on the road">Saw us on the road</option>                                      
        <option value="Referral">Referral</option>
        <option value="Facebook">Facebook</option>
        <option value="Trademe">Trademe</option>
        <option value="Yellow Online">Yellow Online</option>
        </select>
        </td>
    </tr>
</table>
</div>

<table width="100%">
    <tr>
        <td colspan="2" align="left" class="required" valign="middle">
        <span class="required">Note: Image must not be greater than 1MB.</span>
        </td>
    </tr>
    <tr>
        <td valign="middle" align="left" wid>
            Attach Photos:&nbsp;&nbsp; 
            <input type="file" id="fileupload" name="filename" border="0" />  
        </td>
    </tr>
        <tr>
            <td valign="middle" align="left">
            <textarea class="quote-txtarea" placeholder="Comments:" name="comments" rows="3" cols="22">$comments</textarea>   
        </td>
    </tr>                                               
    <tr>                                                    
        <td valign="top" >
            <img src="captcha.php" name="captcha" id="captcha" style="padding: 0px;  margin: 0 0 -4px;" />
            <input class="input" type="text" name="captcha" style="width:100px; height: 20px; padding: 0px;" />
            <input type="submit" name="submit_quote" class="submit" value="Submit" />
        </td>
    </tr>
</table>
</form>
</div>

1 个答案:

答案 0 :(得分:0)

您的最终echo行有语法错误,请记住以分号结尾。你没有提供很多关于你没有验证的意思的信息 - 所以我只是在这里猜测 - 但是你正在使用@mail@符号可以抑制错误。

因此,请尝试删除@符号以查看是否有更多描述性错误,并通过在开头附近添加此行来启用错误报告:

error_reporting(E_ALL);

此外,您不会一直看到错误,因为您没有涵盖所有可能的情况。这是对代码的简化:

 <?php
    if($_SESSION["captcha"] == $_POST["captcha"]) {
        if (file_exists($tmp_name)){
            if (@mail($to, $subject, $message, $headers)){
                // Success
            } else {
                // Send failed
            } 
        } // <-- Add an else clause here!
    } else {
        // Invalid
    }
?>

如果file_exists返回false,则不执行任何操作。添加一个返回消息的else子句: