0

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?

4
  • 2
    I really don't understand what you want to. But you make weird things in your code like load('string'+'string') or syntax error load("string). And Java != JavaScript Commented May 11, 2013 at 19:24
  • sorry, the post about filtered out php tags. similar.php takes an argument, ie, similar.php?search=snoop%dog snop%dog is stored in $name im calling similar.php from video.php to display similar artists. But when i attempt to call the above code(which you mentioned are both strings) which then returns a php page as a result, which would make sense. So to review, what i need to do, is call the string, similar.php?search= followed by the variable $name Can anyone, or you help me out please? Commented May 11, 2013 at 19:29
  • So, that should work $("#div1").load("similar.php?search=<?php $name ?>"); Maybe it's all about that syntax error (missed "). Commented May 11, 2013 at 19:33
  • Oh, and type echo as at answer below. Commented May 11, 2013 at 19:35

1 Answer 1

1

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.

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

10 Comments

Thanks, but now it doesnt load anything, as can be seen here, soundwavez.net/video/sVaRHJn_dgM Any other suggestions?
That's because your URL is not valid, you need to URL encode the strings when there are spaces and special characters.
Well, going by what you said, ive removed the spaces, $("#div1").load("similar.php?search=<?$name?>"); and now its back to square one, returning a php related page. see the same link
The spaces are in your strings, you can't remove the echo and the spaces inside the php code, as that has nothing to do with it ?
oh im following you now! So i'd have to call $name before the function, switch the space with % assign it to a new variable, then call the new variable? You sir, are a genious, Thanks
|

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.