用ajax请求调用.php文件-Wordpress [英] Call .php file with an ajax request - wordpress
问题描述
我正在尝试从wordpress中的ajax请求调用php文件.我遇到的问题是ajax请求需要php文件的路径.我不确定在我的wordpress安装中该php文件的位置.此外,此文件不能内联,因为仅当用户决定调用此php文件时,才需要调用此php文件.我现在不使用jquery,但是可以使用它,因为我很确定那只是客户端,因此不必涉及服务器.
I am trying to call a php file from an ajax request in wordpress. The problem I am encountering is that the ajax request requires a path to the php file. I am unsure of where to place this php file in my wordpress installation. Further, this file cannot be included inline because I need to call this php file only when the user decides to call it. I don't use jquery right now but would be open to using it as I'm pretty sure that's client side only so the server wouldn't have to be involved.
作为我想做的事情的一个例子,让我们用表格尝试一下.此示例摘自http://thisinterestsme.com/ajax-form-submission-php/
.
As an example of something I would like to do let's try this with a form. This example was taken from http://thisinterestsme.com/ajax-form-submission-php/
.
我会将其包含在网页中.
I would include this in the webpage.
<html>
<head>
<meta charset="UTF-8">
<title>Example Ajax PHP Form</title>
</head>
<body>
<form id="my_form_id">
Your Email Address: <input type="text" id="email" /><br>
<input type="submit" />
</form>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).ready(function(){
$('#my_form_id').on('submit', function(e){
//Stop the form from submitting itself to the server.
e.preventDefault();
var email = $('#email').val();
$.ajax({
type: "POST",
url: 'submission.php',
data: {email: email},
success: function(data){
alert(data);
}
});
});
});
</script>
</body>
</html>
然后在服务器上的其他地方,我将拥有此文件.问题是我不知道将文件放置在何处或上面给出ajax请求的路径.
Then elsewhere on the server I would have this file. The problem is I don't know where to place this file or what path to give the ajax request above.
<?php
$emailAddress = false;
if(isset($_POST['email'])){
$emailAddress = $_POST['email'];
}
echo 'Received email was: ' . $emailAddress;
?>
推荐答案
假设您有以下php:
<?php
$emailAddress = false;
if(isset($_POST['email'])){
$emailAddress = $_POST['email'];
}
echo 'Received email was: ' . $emailAddress;
?>
您应该将此文件命名为page-submission.php,并将其保存在您的functions.php所在的位置,然后创建一个空白页面名称"submission".现在,您可以在ajax中将此文件称为/submission.
You should name this file as page-submission.php and save it where your functions.php is located, then create a blank page name "submission". You can now then call this file in your ajax as /submission.
以下是视觉层次结构的工作原理:
Here's the visual hierarchy how it works:
这篇关于用ajax请求调用.php文件-Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!