alicefreak Posted October 14, 2010 Share Posted October 14, 2010 Hello guys is it possible to create a single php file to contain all the functions to be called via AJAX and get AJAX to call the specific function required rather than having to create individual pages for each AJAX request you need to make? Many Thanks Link to comment https://forums.phpfreaks.com/topic/215861-call-php-functions-using-ajax/ Share on other sites More sharing options...
kenrbnsn Posted October 14, 2010 Share Posted October 14, 2010 Yes, just use a flag variable to indicate what to do, for example -- using jQuery's $.post $.post("phpfile.php",{flag: 1, var: 'test1'}, function(data) { if(data.ret != 'ok') { alert('Error: ' + data.error_msg); } else { alert('Success'); } },"json"); $.post("phpfile.php",{flag: 2, var: 'test2'}, function(data) { if(data.ret != 'ok') { alert('Error: ' + data.error_msg); } else { alert('Success'); } },"json"); in your PHP file: <?php if (isset($_POST['flag'])) { switch ($_POST['flag']) { case '1': if (isset($_POST['var']) && $_POST['var'] != 'test1') { exit(json_encode(array('ret'=>'not ok','error_msg'=>'Input not correct'))); } // // do case 1 work // exit(json_encode(array('ret'=>'ok'))); break; case '2': if (!isset($_POST['var'])) { exit(json_encode(array('ret'=>'not ok','error_msg'=>'Input not found'))); } // // do case 2 work // exit(json_encode(array('ret'=>'ok'))); break; default: exit(json_encode(array('ret'=>'no ok','error_msg'=>'Invalid flag'))); } } ?> Note: not checked for syntax. Ken Link to comment https://forums.phpfreaks.com/topic/215861-call-php-functions-using-ajax/#findComment-1122134 Share on other sites More sharing options...
alicefreak Posted October 14, 2010 Author Share Posted October 14, 2010 Thanks you soo much. You made my day. :-) one thing more is there any tutorial. so i can clear the doubt. thanks any way Link to comment https://forums.phpfreaks.com/topic/215861-call-php-functions-using-ajax/#findComment-1122192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.