使用 jQuery 或 Javascript 包含 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15699335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include PHP file using jQuery or Javascript
提问by Stephan Botes
I have a Javascript file that submits a form and loads an alert etc. Now I want to add functionality to call or get a .php
file, I tried using $.get("mail.php");
but it doesn't seem to work.
我有一个 Javascript 文件,用于提交表单并加载警报等。现在我想添加调用或获取.php
文件的功能,我尝试使用$.get("mail.php");
但它似乎不起作用。
Javascript Code:
Javascript代码:
$('#myform').on('submit', function(e){
e.preventDefault();
$.get($(this).attr('action') + $(this).serialize());
alert("Thank You! \nTop Up for: <?php echo "$username" ?> Purchased Successfully");
$.get("mail.php"); //this is the added part to get mail.php//
location.reload();
PHP - mail.php
:
PHP - mail.php
:
<?php
$to = "[email protected]";
$headers = "From: website mail \r\n";
$email_body = "has just toped up his account.\n".
$email_subject = "User Topup Alert";
mail($to,$email_subject,$email_body,$headers);
?>
回答by alwaysLearn
I think you can try load()
:
我想你可以试试load()
:
$("#mailDiv").load('mail.php');
According to me $.get
and $.post
are to send the data. You cannot include files using this.
根据我$.get
和$.post
是发送数据。您不能使用它包含文件。
回答by Nimesh Vagadiya
you can use
您可以使用
$('selector').load()
OR
或者
$.ajax();
$.ajax();
is most preferable.
$.ajax();
是最可取的。