I cant make it work. I have a php script on a website (not so good in php, so if something is wrong, please point me). And i want to call it from a js document.
<?php
function doit($option){
if ('getit' == $option){
$value = '318273918739182739179';
return $value;
}else{
return 0;
}
}
?>
and i want to call it from a js file. How do i pass an argument to the php script via Ajax?
var getanswer ={
php: function(){
$.ajax({
url:'mywebsite.com/php/return.php',
data: {action, 'doit'},
type: 'post',
success: function(output){
alert(output);
}
})
}
}
in javascript:
$.get('yourPHPscript.php?argument='+some_arg,function(data){
//data contain result from php script
});
in yourPHPscript:
$option = $_GET['argument'];
if ('getit' == $option){
$value = '318273918739182739179';
echo $value;
}else{
echo "0";
}
You can use ajax to call php whenever you need it
for example I have used it for calling a function glob
in php
here is some of the jquery code
$('#search_query').keyup(function() {
var search_input = $("#search_query");
var output = search_input.val();
if(output.length > 0){
$('#app_menu').hide();
$('#note').show(1000);
$.ajax({
type: "POST",
url: "search_node.php",
data:{item:output}
}).done(function( msg ) {
if(msg!='error')
{
$("div#note").html(msg);
}
else
{
$("#debug").append("<br />refresh function failed: ");
$("#debug").scrollTop("30");
}
});//ajax
}
if (output.length == 0){
$('#note').hide();
$('#app_menu').show(250);
}
});
});
There are some extra stuf in this code that you might not need, hope this helps