javascript call php in backend
I have a facebook login button in my index.php
page.
<f开发者_高级运维b:login-button onlogin="OnLogIn()"></fb:login-button>
when user click on the button, it calls a function OnLogIn()
in javascript.
<script type="text/javascript">
function OnLogIn() {
if(FB.getSession()!=null){
//pass some value to save.php
}
}
</script>
What should i do to let the OnLogIn()
call save.php
page to store some data in the database?
You will need to use some form of AJAX to make a call to the server from JavaScript. See the jQuery AJAX libraries. However, you will want to make sure this call is secured, i.e., cannot be made manually by malicious users who want to add data to your database.
The answer you need is AJAX. I recommend spending some time learning how it works here.
AJAX will allow you to call a remote page on the server and get responses from it. That remote page can do anything that any normal php script could do, including as you need, save info to the database.
This is a skeleton for you:
<script type="text/javascript">
function OnLogIn() {
if(FB.getSession()!=null){
$.ajax({
url: 'save.php'
data: { }
}).success(function(data, status, xhr) {
});
}
}
</script>
精彩评论