Rahmuna
Rahmuna

Reputation: 232

how to pass variable from jquery to php in jquery

I try to pass jquery variable to other php page in php code. but i can't. so Please help me.

    <div class="span4" style="padding-top: 4px;">                    
     <h3><a name="works" id="w"> <?php echo $filename; ?></a></h3>
      </div>
<input name="filename" type="hidden"   id="filename"/>
function formtext() {

                 var aa = $('#w').text();
                alert(aa);
                $.ajax({
                    url   : "exporttodoc.php",
                    type  : "POST",
                    cache : false,
                    data  : {
                        aa : aa
                    }
                });
}

Upvotes: 0

Views: 111

Answers (4)

Rutger Speksnijder
Rutger Speksnijder

Reputation: 31

You were missing a script tag, and it might have been better to include your PHP code as well.

Here is an example of what you might do:

HTML File:

<a href="#" id="w" onclick="return formtext();"><?php echo $filename; ?></a>

<script type="text/javascript">
$(document).ready() {
function formtext() {
    var aa = $('#w').text();
    alert(aa);
    $.ajax({
        url   : "exporttodoc.php",
        type  : "POST",
        cache : false,
        data  : {
            aa : aa
        }
    });
}
}

</script>

PHP File:

<?php

// Use the data like this:
$_POST['aa'];

?>

Upvotes: 0

Chetan Gawai
Chetan Gawai

Reputation: 2401

Try this

  <div class="span4" style="padding-top: 4px;">                    
     <h3><a name="works" id="w">  <?php echo $filename; ?></a></h3>
      </div>
<input name="filename" type="hidden"   id="filename"/>
<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>

<script>
function formtext() {
                 var aa = $('#w').text();
                alert(aa);
                $.ajax({
                    url   : "exporttodoc.php",
                    type  : "POST",
                    cache : false,
                    data  : {
                        'aa' : aa
                    }
                });
}   
</script>

Upvotes: 0

Nikunj Kabariya
Nikunj Kabariya

Reputation: 840

You just need to add <script> tag:

<script type="text/javascript">
function formtext() 
{

                var aa = $('#w').text();
                alert(aa);
                $.ajax({
                    url   : "exporttodoc.php",
                    type  : "POST",
                    cache : false,
                    data  : {
                        aa : aa
                    }
                });
}
</script>

Upvotes: 0

Ricardo Barros
Ricardo Barros

Reputation: 205

You can do this and it will work

<script type="text/javascript">
function formtext() {

                $.ajax({
                    url   : "exporttodoc.php",
                    type  : "POST",
                    cache : false,
                    data  : {
                        aa : '<?php echo $filename; ?>'
                    }
                });
}
</script>

In the other file you just go get it like so

<?php

$filename = $_POST['aa'];

...

But your method seems to work fine, I think, but you need to include the script tags before and after function formtext, just see my example

EDIT: You need to call the function formtext() so it can do the ajax post

Upvotes: 1

Related Questions