在提交表格上填写优惠券代码,然后发送表格

On submit form populate coupon code, then send the form

我制作了一个表格,顺便说一下,由于 authors.Here 我的问题,我通过了这个网站 :

一个带有电子邮件地址的表格,客户正在填写他的电子邮件地址,然后将提交一个隐藏字段并填充优惠券。

我知道 php 邮件程序和生成优惠券代码的 js。

但我不知道如何#1 填充代码,#2 发送带有优惠券代码的表格。

我敢肯定,以前有人问过这个问题。但是我找不到解决方案。

我是新手,但我很热情。

function gencode(lenght) {
  var result = "";
  var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var numbers = "0123456789"
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};
$('#btn-submit').click(function() {
  $('#secret-code').val(gencode(8));
});
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <form name="contactform" method="post" action="formsendattacs.php" type="submit">
    <table width="450px">
      <tr>
        <td valign="top">
          <label for="email">Email Address *</label>
        </td>
        <td valign="top">
          <input type="text" name="email" maxlength="80" size="30" id="email">
        </td>
      </tr>
      <tr>
        <td valign="top">
          <label for="ID">DEMANDE</label>
        </td>
        <td valign="top">
          <input type="hidden" name="secret-code" id="secret-code" placeholder="" maxlength="30" size="30" value="gencode" class="required">
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align:center">
          <input type="submit" value="Submit" class="btn submit" id="btn-submit" onclick="return result(gencode);">
          <div class="">
            <span class="gencode">
              <i class="fa fa-code fa-2x"></i>
            </span>
          </div>
        </td>
      </tr>
    </table>
    <div class="as-console-wrapper">
      <div class="as-console"></div>
    </div>
  </form>
</body>

</html>

我收到的邮件:

客户的优惠券需求详情

电子邮箱:XXXXX@xxxx.com ID需求:Array

我的编码可能一团糟...正如我所说的,我正在学习。 谢谢你的帮助 托尼

  1. 你加载jQuery两次
  2. 你做 onclick="return result(gencode);" 而不是 onclick="return gencode();" 并在函数中返回 false 以停止提交
  3. 表单没有提交类型
  4. 拼写长度
  5. 使用提交事件而不是点击
  6. 您不能将隐藏字段设置为必填项
  7. 测试用户填写了什么

这是使用提交事件的更好版本 - 我取消隐藏代码字段以显示发生的情况

function gencode(length) {
  var result = "";
  var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var numbers = "0123456789"
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};
$('#contactform').on("submit",function(e) {
  e.preventDefault(); // remove when you want to submit
  // test the value
  if ($("#email").val().trim() === "") {
    alert("Please fill in email")
    e.preventDefault(); // this one stays
    return; 
  }
  $('#secret-code').val(gencode(8));
});
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
  <form name="contactform" id="contactform" method="post" action="formsendattacs.php">
    <table width="450px">
      <tr>
        <td valign="top">
          <label for="email">Email Address *</label>
        </td>
        <td valign="top">
          <input type="text" name="email" maxlength="80" size="30" id="email">
        </td>
      </tr>
      <tr>
        <td valign="top">
          <label for="ID">DEMANDE</label>
        </td>
        <td valign="top">
          <input name="secret-code" id="secret-code" placeholder="" maxlength="30" size="30" value="gencode" class="required">
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align:center">
          <input type="submit" value="Submit" class="btn submit" id="btn-submit">
          <div class="">
            <span class="gencode">
     <i class="fa fa-code fa-2x"></i>
    </span>
          </div>
        </td>
      </tr>
    </table>
    <div class="as-console-wrapper">
      <div class="as-console"></div>
    </div>
  </form>
</body>

</html>

在 mplunjan 的帮助下重建代码以及我添加的修改

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
  <form name="contactform" id="contactform" method="post" action="formsendattacs.php">
    <table width="450px">
      <tr>
        <td valign="top">
          <label for="email">Email Address *</label>
        </td>
        <td valign="top">
          <input type="text" name="email" maxlength="80" size="30" id="email">
        </td>
      </tr>
      <tr>
        <td valign="top">
          <label for="coupcode">DEMANDE</label>
        </td>
        <td valign="top">
          <input name="coupcode" id="coupcode" placeholder="" maxlength="30" size="30" value="gencode" type="text" class="">
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align:center">
          <input type="submit" value="Submit" class="btn submit" id="btn-submit">
          <div class="">
            <span class="gencode">
     <i class="fa fa-code fa-2x"></i>
    </span>
          </div>
        </td>
      </tr>
    </table>

      <script type="text/javascript">
    function gencode(length) {
  var result = "";
  var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var numbers = "0123456789"
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};
$('#contactform').on("submit",function(e) {
  // test the value
  if ($("#email").val().trim() === "") {
    alert("Please fill in email")
    e.preventDefault(); // this one stays
    return; 
  }
  $('#coupcode').val(gencode(8));
});
</script>

    <div class="as-console-wrapper">
      <div class="as-console"></div>
    </div>
  </form>
</body>

</html>

php 函数是 30% 的问题原因,一些括号和大写字母根本没有得到遵守,我已经更正了这些

<?php

if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "xxxxx@xxxx.xxx";
    $email_subject = "demande coupon";}

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
 
 
    // validation expected data exists
    if (!isset($_POST['email']) ||
        !isset($_POST['coupcode'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
}
     
    $email_from = $_POST['email']; // required
    $coupcode = $_POST["coupcode"]; // code généré automatiquement


 
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(strlen($error_message) > 0) {
    died($error_message);
  }
 
    $email_message = "DETAIL DEMANDE DE COUPON CLIENT\n\n";
 
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
 
 
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "ID Demande: ".clean_string($coupcode)."\n";
 
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.

索取优惠券 De xxxxx@xxxxx.com À xxxxx@xxxx.xxx 日期 Aujourd'hui 00:34 详细需求 DE COUPON 客户 邮箱:xxxxx@xxxx.xxx ID需求:PTa6GpQu

我从填充了优惠券的表单收到的邮件,一个提交按钮有两个功能。 希望这对任何人都有帮助。