PHP表单提交:按递增顺序发送报价编号到邮件

时间:2017-11-06 10:29:47

标签: javascript php json html5 forms

我正在构建一个简单的表单,在提交时执行以下任务:

1)将表格数据发送到邮件 2)我在json文件中也有相同的副本(即提交的表单也保存在json文件中)。

现在我想要实现的目标是: 提交表单时,应将报价编号发送到邮件和json文件,其中引号应按递增顺序排列(如:q1,q2,q3,....)。

这是我的PHP代码:

    <?php 
if(isset($_POST['submit'])){    
    $to = "s****@gmail.com"; // this is your Email address
    $from = (!empty($_POST["email"])) ? "" . $_POST["email"] . "" : "";
    $name = (!empty($_POST["name"])) ? "" . $_POST["name"] . "" : "";
    $mob =  (!empty($_POST["mob"])) ? "" . $_POST["mob"] . "" : ""; 
    $book_shelf = (!empty($_POST["book_shelf"])) ? "\n\nBook Shelf:" . $_POST["book_shelf"] . "" : "";
    $glass_ware = (!empty($_POST["glass_ware"])) ? "\n\nGlass Ware:" . $_POST["glass_ware"] . "" : "";
    $speakers = (!empty($_POST["speakers"])) ? "\n\nSpeakers: " . $_POST["speakers"] . "" : "";
    $carpets = (!empty($_POST["carpets"])) ? "\n\nCarpets: " . $_POST["carpets"] . "" : "";

    $subject = "Quotation submission From: ".$name;

    $message = $name.$from.$mob. " wrote the following:".$book_shelf.$glass_ware.$speakers.$carpets;




    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

    echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";    
/// To Json file    
   $myFile = "data.json"; 
   $arr_data = array(); // create empty array
  try
  {
      $message_db = array('name'=>$_POST["name"],
                        'from'=>$_POST["email"],
                        'mob'=>$_POST["mob"],                       
                        'book_shelf'=>$_POST["book_shelf"],
                        'glass_ware'=>$_POST["glass_ware"],
                        'speakers'=>$_POST["speakers"],
                        'carpets'=>$_POST["carpets"]);
       //Get data from existing json file
       $jsondata = file_get_contents($myFile);

       // converts json data into array
       $arr_data = json_decode($jsondata, true);

       // Push user data to array
       array_push($arr_data,$message_db);

       //Convert updated array to JSON
       $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

        if(file_put_contents($myFile, $jsondata)) {
            echo 'Success';
        }
       else 
            echo "error";     
   }
   catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
   }
   ///End of Json File   
    }
?>

Html代码:

<form action="" method="post">

    <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" required>

    <input type="email" class="form-control" id="email" placeholder="Enter E-Mail" name="email" required>

    <input type="tel" class="form-control" id="mob" placeholder="Enter Mobile No" name="mob" required>

    <input type="number" id="book-shelf" name="book_shelf">

    <input type="number" id="glass-ware" name="glass_ware">

    <input type="number" id="speakers" name="speakers">

    <input type="number" id="Carpets" name="carpets">

    <input type="submit" name="submit" value="Submit">

</form>

0 个答案:

没有答案