0

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
                    }
                });
}
7
  • are getting any errors? Commented Mar 27, 2014 at 10:42
  • Notice: Undefined index: aa in C:\xampp\htdocs\site\assets\src\exporttodoc.php on line 5 Commented Mar 27, 2014 at 10:45
  • What exactly is your error? Have you checked your PHP script that's accepting $_POST['aa']? Commented Mar 27, 2014 at 10:45
  • If a variable is undefined $.ajax will NOT send it over. Make sure you select it properly. Commented Mar 27, 2014 at 10:46
  • actually the aa may be not passed.. So what can i do? Commented Mar 27, 2014 at 10:47

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

You can't assign file name like this, need to add quotation '<?php echo $filename; ?>'
but i want to pass the value of id#w. because it is content editable
i also call this function and the tag. but nothing happen
0

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>

Comments

0

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>

Comments

0

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

?>

Comments

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.