如何在没有php

时间:2015-08-17 08:27:54

标签: php html forms

我有一个用户提交的表单,并直接发送到电子邮件。问题是我没有将这些值存储在数据库中并直接邮寄它们。这就像一个投诉登记系统。我想要做的是,当用户提交投诉并被重定向到成功页面时,会生成一个投诉编号,对于下次提交,该投诉编号显然应增加1。此外,没有单独的用户帐户,因为访问该网站的任何人都可以提交投诉。我尝试使用字段选项作为唯一ID,但它没有真正起作用。 html表单是,

    <form method="post" action="handler.php">

    <div>
        <label for="first_name"><span class="labelname"><strong>First Name:</strong></span></label> 
        <input type="text" maxlength="50" size="50" name="first_name" id="first_name" value="" class="required" />
    </div>

    <div>
        <label for="last_name"><span class="labelname"><strong>Last Name:</strong></span></label> 
        <input type="text" maxlength="50" size="50" name="last_name" id="last_name" value="" class="required" />
    </div>

    <div>
        <label for="telephone"><span class="labelname"><strong>Telephone Number:</strong></span></label> 
        <input type="text" maxlength="20" size="50" name="telephone" id="telephone" value="" class="required" />
    </div>


    <div>
        <label for="email"><span class="labelname"><strong>E-mail: (Optional)</strong></span></label> 
        <input type="email" maxlength="30" size="50" name="email" id="email" value="" class="" />
    </div>



    <div>
        <label for="com_type"><span class="labelname"><strong>Complaint Type:</strong></span></label>   
        <select name="com_type" id="com_type" class="required">
         <option value=""></option>
         <option value="Electrician">Electrician</option>
         <option value="Plumber">Plumber</option>
         <option value="Mason">Mason</option>
         <option value="Miscellaneous">Miscellaneous</option>
        </select>
    </div>

    <div>
        <label for="flat_no"><span class="labelname"><strong>Flat No.:</strong></span></label> 
        <input type="text" maxlength="10" size="50" name="flat_no" id="flat_no" value="" class="required" />
    </div>

    <div>
        <label for="block_no"><span class="labelname"><strong>Block Number:</strong></span></label>   
        <select name="block_no" id="block_no" class="required">
         <option value=""> </option>
         <option value="A-1">A-1</option>
         <option value="A-2">A-2</option>
         <option value="A-3">A-3</option>
         <option value="A-4">A-4</option>
         <option value="A-5">A-5</option>
         <option value="A-6">A-6</option>
         <option value="A-7">A-7</option>
         <option value="B-1">B-1</option>
         <option value="B-2">B-2</option>
         <option value="B-3">B-3</option>
         <option value="B-4">B-4</option>
         <option value="C-1">C-1</option>
         <option value="C-2">C-2</option>
        </select>
    </div>

    <div>
        <label for="message"><span class="labelname"><strong>Describe your problem:</strong></span></label>
        <textarea rows="10" cols="50" maxlength="2000" name="message" id="message" class="required"></textarea>
    </div>

       <button class="submit" type="submit" name="submit" value="Send Email">Submit Complaint</button> <button class="reset" type="reset">Reset</button>

php code,

<?php
if(!isset($_POST['submit']))
{
die("Error. You need to submit the form.");
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$telephone = $_POST['telephone'];
$visitor_email = $_POST['email'];
$com_type = $_POST['com_type'];
$flat_no = $_POST['flat_no'];
$block_no = $_POST['block_no'];
$message = $_POST['message'];

$email_from = $visitor_email;
$email_subject = "New Complaint";
$email_body = "message\n\n". "First Name: $first_name\n\n". 
"Last Name:  $last_name\n\n".
"Telephone: $telephone\n\n". "Complaint Type: $com_type\n\n". 
"Flat No.: $flat_no\n\n". "Block No.: $block_no\n\n". 
"Complaint: $message";

$to = "my email.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
mail($to,$email_subject,$email_body,$headers);
//success, redirect to thank

header('Location: http://mywebsite.com/thank.php'); 
} catch(Exception $e){
//problem, redirect to fail
header('Location: http://mywebsite.com/fail.php'); 
}       
?>

我只想在成功提交页面上提供投诉编号,投诉编号也应该包含其他详细信息。我可以不使用数据库吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

如果您将数字存储为主键,那么您可以执行以下操作:

SELECT COUNT(*) FROM `table`;

或者,如果您设置了auto_incrementPRIMARY KEY,则可以使用:

SELECT MAX(`id`) FROM `table`;

然后将+ 1添加到结果中并将其作为新插入。如果您没有使用数据库,那么请使用名为count.txt的平面文件并输入当前数字和增量:

<?php
  $count = file_get_contents("count.txt");
  $count++;
  file_put_contents("count.txt", $count);
?>

但是这个选项并不是那么好。因此,请在更新计数时使用机制锁定文件。