【问题标题】:PHP Form conditional statementPHP 表单条件语句
【发布时间】:2012-05-31 07:06:34
【问题描述】:

我有一个包含现有验证码的联系表。我创建了表单并在其中结合了上传功能。问题是当我尝试在没有验证码的情况下提交表单时,它不会显示 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>

【问题讨论】:

  • 当你说“它没有验证”时,你到底是什么意思?

标签: php forms contact


【解决方案1】:

您的最后一行 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 子句:

【讨论】:

  • 我想寻求帮助。正如您在尝试提交表单时所看到的那样,它并没有像我希望的那样工作。如果用户提交表单并输入了错误的验证码,如果成功,它应该说 Invalid captcha,然后它应该说 echo ""; echo "";但现在上传功能一团糟。
  • 就像我说的,你在第 104 行缺少一个分号。
  • 我在表单上添加了分号。你能再检查一下链接吗?当您单击提交时,它没有显示 javascript 警报。
  • 更新了答案。你没有涵盖所有的可能性。
猜你喜欢
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 2016-04-14
  • 2015-12-08
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2016-06-03
相关资源
最近更新 更多