[英]How do I open a “get” php window from jquery without a parent page refresh?
I know the question itself may seem a little vague. 我知道问题本身似乎有点含糊。 Let me elaborate. 让我详细说明。
I am trying to post data to a php file from a page controlled by jquery. 我正在尝试将数据从jquery控制的页面发布到php文件。
Here is what I have on the jquery end: 这是我在jquery端的内容:
var openURL = "http://derp.com/restr/js/addfood.php?fname=" + encodeURI(food_n) + "&fcals=" + encodeURI(food_c);
window.open(openURL);
The Problem is not that it wont go to the page, the problem is that the parent page reloads once the code is executed. 问题不在于它不会转到该页面,而是在于一旦执行了代码,父页面就会重新加载。 Is there any way to keep this from happening? 有什么办法可以防止这种情况发生?
You need to look into using jQuery's ajax() methods . 您需要研究使用jQuery的ajax()方法 。 They have special functions that will run when data from your PHP script is loaded, without refreshing the page. 它们具有特殊功能,这些功能将在加载PHP脚本中的数据时运行,而无需刷新页面。
The basic idea is to use something like... 基本想法是使用类似...
$.ajax({
url: "file_to_load.php",
data: {},
success: function(data){
//it worked!
alert(data);
}
});
...using the data
object to hold your get or post params. ...使用data
对象保存您的get或post参数。
I believe your only options is to reload the content into the current page. 我相信您唯一的选择是将内容重新加载到当前页面中。 In other words, the entire second page has to replace the original via ajax. 换句话说,整个第二页必须通过ajax替换原始页面。 Depending on where you want the performance, you could either load the entire second page with "display:none;" 根据需要的性能,可以使用“ display:none;”加载整个第二页。 off the bat, minus the new content, Then add that in after the ajax. 立即删除新内容,然后将其添加到ajax之后。 Or pass the "new page," of course minus anything that doesn't change, with ajax and replace accordingly. 或通过ajax传递“新页面”,当然减去所有不变的内容,并相应地进行替换。
I haven't mastered ajax inside and out, so there possibly be another method, but I've sure never heard of one. 我还没有完全掌握ajax,所以可能还有另一种方法,但是我确定从未听说过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.