[英]Get done a php file with a simple click with jquery
I am Totally Newbie, i just want a very very simple example for my question. 我完全是新手,我只想问一个非常简单的例子。
Think i have this piece of PHP code : 认为我有这段PHP代码:
<?php
echo "Test";
?>
And a piece of jquery like this : 和一块这样的jQuery:
$(document).ready(function() {
$('.div').click(function(){
$('.loading').show();
// DO PHP NOW
$('.loading').hide();
});
});
And HTML : 和HTML:
<div class="div">Click here</div>
I want a simple code , to connect Jquery to PHP file. 我想要一个简单的代码,将Jquery连接到PHP文件。
When the .div
clicked, then php code get happen with No refreshing the page. 单击
.div
,将发生php代码,而没有刷新页面。 Just this. 只是这个。
How should i do this ? 我应该怎么做?
(i need a simple way to ajaxify a PHP !) (我需要一种简单的方法来使PHP无效!)
+ If i want to do Ajax, i have to write code for every every PHP code i write or one piece of Ajax code for ALL PHP codes ? +如果我想做Ajax,我必须为我编写的每个PHP代码编写代码,或者为所有PHP代码编写一个Ajax代码?
thanks 谢谢
If you want to display some value from php in your div use $(".div").load("/url/to/your/php"); 如果要显示div中php的某些值,请使用$(“。div”)。load(“ / url / to / your / php”); Or use
或使用
$.post("url/to/php",{val:val,val:val},function(callback){
alert("callback");
$(".div").html(callback);
}
Or AJAX 或AJAX
$.ajax({
url: "/php/code/sd.php",
type: "POST",
date: {
username: "asdasd",
password: "asdasd"
},
success: function(callback){
$(".div").html(callback);
}
});
$(function() {
$('.div').click(function(){
var $this = $(this);
$('.loading').fadeIn(200);
$.ajax({
url: 'url_to_your_file.php',
type: 'GET',
data: {},
success: function(data) {
//do smthing OR
$(data).insertAfter($this);
},
error: function(e) {
//do smthing when error
alert('Error happen!');
console.log(e);
},
complete: function(data) {
$('.loading').fadeOut(200);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.