Reputation: 1
I have a simple question about jquery passing variable to php, here is my code, it doesnot work, Can some one helpe me?
javascript:
$(function(){
var txt_value='aa';
$.post("test1.php", {txt_value:txt_value});
});
test1.php
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.post demo</title>
<script src="./report_js/jquery-1.10.2.min.js"></script>
<script src="./a.js"></script>
</head>
<body>
<form action="test1.php" id="txt_value">
<?php
$a=$_POST['txt_value'];
echo "$a";
?>
</form>
</body>
</html>
There is no value printed in test1.php I also tried use callback function, if I use following javascript, it always shows the code of test1.php, not the variable I want to pass javascript:
$(function(){
var txt_value='aa';
$.post("test1.php", {txt_value:txt_value},
function(data){
alert(data);
});
});
Even I change my php code as follows, it still doesnot output correct value in php
<?php
$a=$_POST['txt_value'];
echo "$a";
?>
And I merge jquery js with my own js. Is there anything I need to do with my js? Thanks.
Upvotes: 0
Views: 107
Reputation: 57105
you also need some house keeping stuff
Your script should only be returning what part you want it to return not the total HTML.
use the below code
test1.php
<?php
if(isset($_POST['txt_value'])){
$a=$_POST['txt_value'];
echo "$a";
}
?>
Upvotes: 0
Reputation: 102793
test1.php
should only output the data you want returned -- right now it's returning an entire HTML page, so that's what you get. The script part should be its entirety:
<?php
$a=$_POST['txt_value'];
echo "$a";
?>
Upvotes: 1