将phpsendmail函数插入到弹出框中的确认按钮中


inserting the php sendmail fuction into a confirm button within a pop up box

我试图在php中使用sendmail函数,抛出了一个弹出框。我已经使sendmail功能和弹出窗口分别工作。但我一直没能想出如何以正确的方式连接sendmail。下面是我的最后一个git版本。任何帮助都将是美妙的。

{
<tr>
<th align="center">Instrument Serial Number:</th>
<td align="left"><input type="text" class="cInstTravText" name="SerialNumber" id="idSerialNumber"/></td>
<button onclick="drop_check()">comfirm</button>
<p id="drop_check"></p>
<script>
function sendEmail() 
{
$to = 'jonsrod1992@gmail.com'; 
$subject = 'Test email using PHP';
$message = "test";
//$message = 'hello world.'n'n this is a test for functionality'n'n this is the first try'n place instrument serial Number here--> '<$modelNumber>'<-- there.'; 
$headers = 'From: jonr@twobtech.com' . "'r'n" . 'Reply-To: jonr@twobtech.com' . phpversion();
                                mail($to, $subject, $message, $headers, '-fwebmaster@example.com'); 
}
function drop_check() {
var x;
if (confirm("transfer and send email!") == true) {
x = "transfered and email sent!";
} else {
x = "You pressed Cancel!";
}
document.getElementById("drop_check").innerHTML = x;
}
</script>
</tr>

您尝试的方法是行不通的。页面上的php首先运行(服务器端),然后使用JavaScript触发弹出窗口。这意味着您需要将弹出窗口中的响应发送到另一个php页面,然后由该页面处理sendmail。你可以在表单提交中通过POST来完成这项工作,或者更好的是,使用AJAX调用,就像这样(这在脚本标记之间进行,最好在页面的顶部或底部,而不是在HTML的中间):

 if (confirm("transfer and send email!")) {
     //if needed, get values from one or more HTML inputs like so:
     var stuff = $('#someInputId').val();
     $.ajax({
        type: "POST",
        url: "sendmail.php",
        data: { data: stuff }  //if you don't need to send any data from your HTML elements this can be ommitted
     }).success(function() {
     //optional: let the user know email was sent
     });
 }

然后你可以把你的sendmail代码放在sendmail.php中。如果你在AJAX调用中发送任何数据,你可以从$_POST中检索它。sendmail.php看起来像:

$body = isset($_POST['stuff']) ? $_POST['stuff'] : 'no stuff';
$to = 'jonsrod1992@gmail.com';
$subject = 'Test email using PHP';
$headers = 'From: jonr@twobtech.com' . "'r'n" .
        'Reply-To: jonr@twobtech.com' . "'r'n" .
        "MIME-Version: 1.0'r'n" .
        "Content-Type: text/html; charset=ISO-8859-1'r'n";
mail($to,$subject,$body,$headers);

也许我错过了什么,但是。。。看起来您可能正试图将PHP添加到javascript中。这是行不通的。此外,如果这只是对代码进行奇怪的重新格式化,则不会调用sendEmail函数。