在提交表格时输入优惠券代码,然后发送表格。

问题描述 投票:-1回答:1

我做了一个表格,顺便说一下,我通过这个网站得到的,感谢作者.这是我的问题。

一个表格,与电子邮件地址,自定义填写他的电子邮件地址,然后一个隐藏的字段将提交一个优惠券弹出。

我已经想好了生成优惠券代码的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>

我收到的邮件。

DETAIL DEMANDE DE COUPON CLIENT(优惠券客户详情)

电子邮件。[email protected] Demande: 阵列

这可能是一个混乱的我的编码......正如我说我正在学习。谢谢你的帮助Tony

javascript arrays form-submit
1个回答
0
投票
  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>

0
投票

与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>

0
投票

导致问题的原因是30%的php函数,一些括号和大写字母根本不受尊重,我已经纠正了这些问题。

<?php

if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "[email protected]";
    $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.

0
投票

Objet demande coupon De [email protected] À [email protected] Date Aujourd'hui 00:34 DETAIL DEMANDE DE COUPON CLIENT Email: [email protected] ID Demande: PTa6GpQu

我已经收到的邮件,从表单中弹出的优惠券,一个提交按钮两个功能.希望这将是帮助任何人。

© www.soinside.com 2019 - 2024. All rights reserved.