0

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.

2
  • Actually, you are getting things wrong and the scripts work as you designed them, test1.php should be a script that handles data it receives, so (in most cases) there is not place for html code, instead you will have things like sql queries. If test1.php show you the value it received, then you variable was transmitted successfully, and the next step is to process it... Commented Aug 16, 2013 at 2:09
  • test1 should return only text. You can remove all html tags in test1.php. Anyway I think the code is correct. Have you inspected it on your javascript debugging console like firebug in case you have just some syntax errors? Commented Aug 16, 2013 at 2:11

2 Answers 2

1

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";
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I got it. But I still have another question. If I keep your code as php code. What do I need to change to my javascript code? I mean how to include jquery-1.10.2.min.js file in my js. Thanks.
I am a new guy to javascript
@YijiangHuang no -- mainpage.php should have the Javascript file, like you have it now. The test1.php doesn't need any Javascript -- it just returns data, right?
0

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";
    }
?>

1 Comment

Hi, I got it. But I still have another question. If I keep your code as php code. What do I need to change to my javascript code? I mean how to include jquery-1.10.2.min.js file in my js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.