简体   繁体   中英

Get php variable from url on reload without refresh

I'm working on a hobby project (usually working as a designer, so not all that familiar to php – please have oversight with all or any redundant code), trying to learn new things. Now I've bumped into a problem that I don't quite seem to get the hang of. I have an index.php used to display random sentences from data.php, this works fine – however I want to be able to sort out specific types of sentences for different people if necessary. This is done with a dropdown containing Designer, Illustrator and Developer.

If for example you choose Developer from the dropdown menu, the page reloads with index.php?yrke=developer in the URL as a result. This is all fine and as expected, and when i echo $_GET['yrke']; from data.php it displays the text "developer" fine the first load, but upon clicking the randomizerButton button (note that the content is loaded from data.php without refreshing the page in the browser when clicking this button) $_GET['yrke']; does not seem to be able to get a read on the value in the url (putting $_GET['yrke']; in index.php obviously works regardless, but I need to access the url variable in data.php).

If there's a way to do this while maintaining the "update-content-without-browser-refresh" function that'd be awesome, the other easiest solution would perhaps be to remove said "update-content-without-browser-refresh" and go for good old refreshes and thus solving the problem – but why make it that easy right?

index.php (excerpt)

<button data-href="data.php" class="randomizerButton">Randomize sentences</button>

<form action="index.php" method="get">
    <select name="yrke" onchange="this.form.submit()">

        <option value="designer"<?=$_GET['yrke'] == 'designer' ? ' selected="selected"' : '';?>>Designer</option>
        <option value="illustrator"<?=$_GET['yrke'] == 'illustrator' ? ' selected="selected"' : '';?>>Illustrator</option>
        <option value="developer"<?=$_GET['yrke'] == 'developer' ? ' selected="selected"' : '';?>>Developer</option>

    </select>
</form>

<?php include('data.php'); ?>

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('button.randomizerButton').click(function(){
                scriptUrl = $(this).attr('data-href');
                $.post(scriptUrl, function(response){
                    $('#results').html(response);
                });
            });
        });
    </script>

data.php (excerpt)

    echo $_GET['yrke'];

If you want $_GET['yrke'] on data.php you would have to use $.get() and just use the full URL, like:

$.get('data.php?yrke='+encodeURIComponent($('[name=yrke]').val()), function(response){
  $('#results').html(response);
}

I would use

$.post('data.php', {yrke:encodeURIComponent($('[name=yrke]').val())}, function(response){
  $('#results').html(response);
}

with $_POST['yrke'] on data.php . Usually, I would not have a form that submits in the traditional manner. It's all about the AJAX.

Not sure if this will help but you could do something like this...

        <div id ="content">

        <?php
        $pages_dir ='pages';
        if(!empty($_GET['p'])){

            $pages =scandir($pages_dir, 0);
            unset($pages[0], $pages[1]);

            $p = $_GET['p'];

            if (in_array($p.'.inc.php', $pages)){
            include($pages_dir.'/'.$p.'.inc.php');
            }else {
                echo'Page not found';
            }

            }else{
                include($pages_dir.'/home.inc.php');

            }

        ?>
HERE
        </div>

Make a folder called pages then name the files within the folder "aboutus.inc.php" ect. the important part of the file extension is .inc.php , you can name anything before .inc.php anthing you like here is an example of how to write the navigation "About us"

IF need be I can send you a blank template

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