php - Send variabiles ajax -
1.php
... <script src="/jquery-1.3.1.min.js" type="text/javascript"></script> <script type="text/javascript"> var = $a var b = $b var c = $c apclick = function() { $.ajax({ url: 'a1.php', data: { a: a, b: b, c: c }, datatype: json, success: function(results) { if (results.msg == 'success') { alert(a) alert(b) alert(c) } else { alert(results.msg) } }, error: function(results) { alert("data returned: " + results.msg ) } }); settimeout("location.reload(true);", 3000) return false; } </script> ..... <strong><br><a href="#" onclick="apclick();return false;">afiseaza </a></strong>
a1.php
<?php $return = array(); $a = $_post['a']; $b = $_post['b']; $c = $_post['c'] if ($a == "hello") { $return['msg'] = 'success'; $return['a'] = "buna"; }; if ($b == "say") { $return['msg'] = 'success'; $return['a'] = "spune"; }; if ($c == "man") { $return['msg'] = 'success'; $return['a'] = "om"; }; header("content-type: application/json"); echo json_encode($a); echo json_encode($b); echo json_encode($c); ?>
questions is: how send a,b,c a1.php , receive a,b,c in 1.php
the code provided had several syntax errors, should have fixed them before posting it.
anyway, here working code you:
<script type="text/javascript"> var = "hello"; var b = "say"; var c = "man"; var res; apclick = function() { $.ajax({ url: 'a1.php', data: { a: a, b: b, c: c }, datatype: 'json', type: 'post', success: function(results) { res = results; if (results.msg == 'success') { alert(results.a) alert(results.a) alert(results.a) } else { alert(results.msg) } }, error: function(results) { alert("data returned: " + results.msg ); } }); settimeout("location.reload(true);",30000); return false; }; </script> </head> <body> <strong> <br> <a href="#" onclick="apclick();return false;">afiseaza </a> </strong>
and a1.php:
<?php $return = array(); $a = $_post['a']; $b = $_post['b']; $c = $_post['c']; if ($a == "hello") { $return['msg'] = 'success'; $return['a'] = "buna"; }; if ($b == "say") { $return['msg'] = 'success'; $return['a'] = "spune"; }; if ($c == "man") { $return['msg'] = 'success'; $return['a'] = "om"; }; header("content-type: application/json"); echo json_encode($return); ?>
Comments
Post a Comment