jquery - php function call with ajax -
i trying call php function when html button clicked.i have done searching , found impossible directly , should use ajax. attempt far not working.this test.php , function in page.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { $('.nextpage').on('click', function() { $.ajax({ url: 'test.php', data: {x: 1}, type: 'post', datatype: 'json', success: function(response) { alert(response); } }); }); }); </script> </head> <body> <button type="button" class="nextpage">go nextpage</button> <?php if (isset($_post['x'])) { if ($_post['x'] == 1) { $data = function1(); echo json_encode($data); exit; } } function function1() { return 'hi user! im function #1'; } function function2() { return 'hi user! im function #2'; } ?>
get value of x ajax call.
$x = $_post['x'];
then use it.
edit
first have check weather variable set or not..
if(isset($_post[x])) { $x = $_post['x']; }
try this
Comments
Post a Comment