以下是 HTML 代码: - https://pastebin.com/Rc3AGC8x
<html>
<body bgcolor="#ccccb3">
<script type="text/javascript">
function hello(min,max) {
var x;
x= Math.floor(Math.random()*(max-min+1)+min);
document.getElementById("complaint").value =x;
//document.write(x);
};
</script>
<center>
<form action="http://localhost/PHPMailer-master/" method="post">
<p>Name: <input id="n" placeholder="Name" name="n" required></p>
<p>E-Mail: <input id="e" placeholder="Email Address" type="email" name="e" required></p>
<p> <textarea id="m" placeholder="write your message here" name="m" rows="10" required></textarea></p>
<p><input id="mybtn" type="submit" value="Submit Form" onClick="hello(112,78945)"></p>
<input type="hidden" value="The complaint id is : #" id="complaint" name="complaint">
<!--<p>Clicks: <a id="clicks">0</a></p>-->
</form>
</center>
</body>
</html>
以下是 PHP 代码: - https://pastebin.com/g0Cnh8iR
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example@gmail.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['m']) ){
$n = $_POST['n'];
$e = $_POST['e'];
$m = nl2br($_POST['m']);
$c = $_POST['complaint'];}
else{
$n='';
$e='';
$m='';
$c='245';
}
$mail->setFrom('noreply@gmail.com', 'Panasonic');
$mail->addAddress('testpanasoniccontact@gmail.com', 'Pragzz'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Complaint Number: '.$c;
$mail->Body = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p><b>Message: </b>'.$m.'</p>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent. Your complaint will be attended within 48 hours. Your complaint No. is '.$c;
}
?>
密码和用户名字段已被故意删除。
HTML脚本包含隐藏类型的表单字段,投诉为ID 。
无法从 javascript函数获取值并发送到php代码。
答案 0 :(得分:0)
尝试以下代码。我修改了JS部分,并在提交按钮之前放置了隐藏的feild。它现在运作良好。
<html>
<body bgcolor="#ccccb3">
<center>
<form action="http://localhost/PHPMailer-master/" method="post">
<p>Name:<input id="n" placeholder="Name" name="n" required></p>
<p>E-Mail: <input id="e" placeholder="Email Address" type="email" name="e" required></p>
<p><textarea id="m" placeholder="write your message here" name="m" rows="10" required></textarea></p>
<input type="hidden" value="100" id="complaint" name="complaint">
<p><input id="mybtn" type="submit" value="Submit Form" onClick="hello(112,78945)"></p>
</form>
</center>
<script type="text/javascript">
function hello(min,max) {
var complaint = document.getElementById("complaint").value
console.log(complaint);
};
</script>
</body>
</html>