0

I've tried this and I do not know if it is the right way:

HTML Code:

<div class="container-fluid">
 <form class="form-inline">
    <div class="form-group">
        <label>Choose Records Per Page:</label>
            <select class="form-control input-sm num_rec_per_page">
                <option>5</option>
                <option>10</option>
                <option>50</option>
                <option>100</option>
                <option>150</option>
                <option>200</option>
            </select>
        </div>
    </form>
</div>   

JAVASCRIPT Code:

<script>
    $(document).ready(function(){
        $("select.num_rec_per_page").change(function(){
            $(".num_rec_per_page option:selected").val();
            window.location="?num_rec_per_page=" + num_rec_per_page;
        });
    });
</script>

PHP Code

$num_rec_per_page=$_GET['num_re_per_page'];

So this code, when the variable changes, a refresh is made to the webpage, but I do not want that, can you help me??

3

1 Answer 1

0

You didn't specify your exact end goal with getting the JS variable to PHP, but I assume it's to get the value of the select so you can change the number of records displayed per page (inferring from variable names).

If you want to avoid a page refresh, you could do this via AJAX and pull in additional records to display using JavaScript. If you assign a new value to window.location, it will refresh the page.

You would want something like

$(selector).load('/get_results.php?num_rec_per_page=' + num_rec_per_page);
Sign up to request clarification or add additional context in comments.

4 Comments

Can you give me an example of that??
Updated. I don't have the full HTML or API endpoints for your application, but adjust the selector and url and you should be good. If you want to be more efficient on network bandwidth (i.e. if you expect to have to reload results a lot) you should consider having your API return JSON output that is formatted client-side. (exactly how falls outside the scope of this question)
what do I put in "$(selector)"
Whatever selector is required to select the container DOM element. For example, if you have a <div id="someId"> you would use $('#someId'). The exact selector depends on your HTML which I don't have.

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.