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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.