Alex Emile
Alex Emile

Reputation: 15

Jquery .load function. Passing a php variable to .load()

Im trying to pass a PHP variable, ($name) through the .load function. Every time i try to call similar.php, instead of search for whatever is in $name, its searching for

Heres my java code;

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("similar.php?search="+"&name");
  });
});
</script>

Ive also tried, orginally:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("button").click(function(){
        $("#div1").load("similar.php?search=<?php $name ?>);
      });
    });
    </script>

Any tips?

Upvotes: 0

Views: 532

Answers (1)

adeneo
adeneo

Reputation: 318352

You forgot the last quote and to echo the $name, change this :

$("#div1").load("similar.php?search=<?php $name ?>);

to:

$("#div1").load("similar.php?search=<?php echo urlencode($name) ?>");

And use the DOM inspector to view the code and see that the PHP is outputted properly.

Upvotes: 1

Related Questions