Send variabiles ajax
1.php
...
<script src="/jquery-1.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var a = $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';
$retur开发者_如何学运维n['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 to a1.php and receive a,b,c in 1.php
The code provided had several syntax errors, you should have fixed them before posting it.
Anyway, here is the working code for you:
<script type="text/javascript">
var a = "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 the 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);
?>
data: { 'a': 'a', 'b': 'b', 'c': 'c' },
type: 'POST'
Try that (with the quotes on the data and type set to POST.)
thumbs up for rsmoorthy but I woudln't use $_REQUEST
decide your request method and set the type as Gazler suggested accordingly.
精彩评论