简体   繁体   中英

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
                    }
                });
}

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

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>

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>

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'];

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM