Variable from JavaScript to PHP

503

Here is the code:

    $('#sousmenu a').click (function (){
        startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
        return false;
    });

The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the?folder=portraits works.

Here is the page.

**** Simpler ***** Maybe I am not clear, it happens sometimes!

I want the link href to be send to this PHP function,

<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?> 

so clicking on the linkanimaux will sendanimaux to theglob() PHP function and will get all the .jpg files in thephotos-animaux folder.

Clicking on the portraits will send the photo-portraits, etc.

366

Answer

Solution:

If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.

If your site is onhttp://example.com and you wish amyparam with valuetest to be passed to PHP you should add something like this in your JS:

document.location = 'http://example.com?myparam=test';

This will reload your page adding a new param which can be accessed in PHP by simply using$_GET['myparam'] variable.

You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.

739

Answer

Solution:

Look at the source in your browser.

Php is server-side, and that means you need to use ajax or reload whole page to get a response.

There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax

People are also looking for solutions to the problem: php - search string with preg_replace

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.