【发布时间】:2016-03-01 15:20:16
【问题描述】:
我在将工作中的 php/javascript/ajax 应用程序放入我的 wordpress 主题时遇到了问题。我为我的用户使用它,所以不在管理面板中。
我已经做过的事情: 我在functions.php 中调用了JavaScript 文件。而且它的加载很好。但是当我打开我的主 php 文件时,它不会进行 ajax 调用。
我有 4 个页面:response.php
- response.php(这个工作正常。)
- single-page.php(这个也可以。)
- voedingsschema.js(这个在没有 wordpress 的情况下工作。)
- function.php(我不知道我这样做对不对。)
我希望我已经提供了足够的信息。
我的模板中的 function.php 代码如下所示:
function fitnesszone_child_scripts(){
wp_enqueue_script( 'voedingsschema js', get_stylesheet_directory_uri() . '/js/voedingsschema.js');
}
add_action( 'wp_enqueue_scripts', 'fitnesszone_child_scripts');
javascript代码:
<script type="text/javascript">
$(document).ready(function() {
//##### send add record Ajax request to response.php #########
$("#FormSubmit").click(function (e) {
e.preventDefault();
if($("#contentText").val()==='')
{
alert("Please enter some text!");
return false;
}
$("#FormSubmit").hide(); //hide submit button
$("#LoadingImage").show(); //show loading image
var myData = {
content_txt: $('#contentText').val(),
content_txt2: $('#contentText2').val(),
};
jQuery.ajax({
type: "POST", // HTTP method POST or GET
url: "http://website.com/wp-content/themes/fitnesszone-child/response.php", //Where to make Ajax calls
dataType:"text", // Data type, HTML, json etc.
data:myData, //Form variables
success:function(response){
$("#responds").append(response);
$("#contentText").val(''); //empty text field on successful
$("#FormSubmit").show(); //show submit button
$("#LoadingImage").hide(); //hide loading image
},
error:function (xhr, ajaxOptions, thrownError){
$("#FormSubmit").show(); //show submit button
$("#LoadingImage").hide(); //hide loading image
alert(thrownError);
}
});
});
//##### Send delete Ajax request to response.php #########
$("body").on("click", "#responds .del_button", function(e) {
e.preventDefault();
var clickedID = this.id.split('-'); //Split ID string (Split works as PHP explode)
var DbNumberID = clickedID[1]; //and get number from array
var myData = 'recordToDelete='+ DbNumberID; //build a post data structure
$('#item_'+DbNumberID).addClass( "sel" ); //change background of this element by adding class
$(this).hide(); //hide currently clicked delete button
jQuery.ajax({
type: "POST", // HTTP method POST or GET
url: "http://website.com/wp-content/themes/fitnesszone-child/response.php", //Where to make Ajax calls
dataType:"text", // Data type, HTML, json etc.
data:myData, //Form variables
success:function(response){
//on success, hide element user wants to delete.
$('#item_'+DbNumberID).fadeOut();
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
});
});
</script>
【问题讨论】:
-
抱歉,function.php 文件长这样。[code][/code]
-
你的控制台有错误吗?
-
不,没有错误。 :(
-
这意味着javascript什么都不做,没有错误,也没有执行你的代码。你可以从你的functions.php中添加代码吗,你上面的例子是空的;)
-
代码已添加。如果我转到我的 single-page.php,我看到我的 javascript 正在加载谷歌浏览器。
标签: javascript php jquery ajax wordpress