我有一个简单的表单,它使用ajax将数据发布到发送电子邮件的php脚本。它只使用名称字段工作得很好,但是当我是f_name和l_name时,它似乎没有通过它们?
html是
body { font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#444; }
input[type=text], input[type=email], textarea {
border:1px solid #ccc;
padding:8px;
margin:2px 0;
font-size:13px;
font-family:Arial, Helvetica, sans-serif;
color:#8f8f8f;
width:250px;
border-radius:5px;
box-shadow:inset 0 0 8px #e5e8e7;
}
input[type=submit] {
border:none;
padding:8px 25px;
margin:2px 0;
font-family:Arial, Helvetica, sans-serif;
color:#fff;
background:#0d7963;
border-radius:5px;
cursor:pointer;
}
input[type=submit]:hover {
opacity:0.9;
}
</style>
<script src="/js/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('.myform').on('submit',function(){
// Add text 'loading...' right after clicking on the submit button.
$('.output_message').text('Processing...');
var form = $(this);
$.ajax({
url: form.attr('action'),
method: form.attr('method'),
data: form.serialize(),
success: function(result){
if (result == 'success'){
$('.output_message').text('Message Sent!');
} else {
$('.output_message').text('Error Sending email!');
}
}
});
// Prevents default submission of the form after clicking on the submit button.
return false;
});
});
</script>
</head>
<body>
<form method="post" class="myform" action="process-form3.php">
<input type="text" name="f_name" placeholder="Your first Name" required><br>
<input type="text" name="l_name" placeholder="Your last Name" required><br>
<input type="email" name="email" placeholder="Your Email" required><br>
<textarea rows="4" cols="20" name="message" placeholder="Your Message"></textarea><br>
<input type="submit" name="send" value="Send"> <span class="output_message"></span>
</form>
php文件是:
<?php
if (isset($_REQUEST['f_name'],$_REQUEST['email'])) {
$f_name = $_REQUEST['f_name'];
$l_name = $_REQUEST['l_name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$new_message = $message+f_name+l_name;
// Set your email address where you want to receive emails.
$to = 'billycov@icloud.com';
$subject = 'Contact Request From Website';
$headers = "From: ".$name." <".$email."> \r\n";
$send_email = mail($to,$subject,$new_message,$headers);
echo ($send_email) ? 'success' : 'error';
}
?>
答案 0 :(得分:2)
我发现了一些问题:
您可以在这里连接像PHP这样的PHP字符串:
$new_message = $message+f_name+l_name;
只需替换为:
$new_message = $message . $f_name . $l_name;
显然,此处未定义$name
:
$headers = "From: ".$name." <".$email."> \r\n";
我认为就是这样!
答案 1 :(得分:0)
问题在这里:
$headers = "From: ".$name." <".$email."> \r\n";
您应该定义$name
或使用$f_name
或$l_name
答案 2 :(得分:0)
错误地连接在这一行:
$new_message = $message+f_name+l_name;
它应该是这样的:
$new_message = $message.$f_name.$l_name;
并且您还需要在此行中用$ f_name替换$ name:
$headers = "From: ".$name." <".$email."> \r\n";
你去;)