0

I have a use case where there will be a filter. Based on the filter, the page content has to change. Now I want to use that filter condition somewhere else in the page.

To do this, I can't afford to use the AJAX. That is why am using a simple JS variable. So is there any way to pass JS variable's value to PHP variable which can be used without loading the page?

I have a JavaScript variable with some value. and i need to assign that value to the PHP variable without loading the page or without using Session and cookies.

 <script>
        var myJavascriptVar = $('#phase').val();
        document.cookie = "myJavascriptVar = " + myJavascriptVar 
        <?php
        $myPhpVar= $_COOKIE['myJavascriptVar'];
        ?>
</script>

//i tried this but it was not usefull without page load.

PHP code:

 <a href="tickets.php?project=<?php echo $myPhpVar; ?>">
       <div class="col-md-3">
          <div class="example">
              <div class="panel widget"   style="margin-bottom: 15px;">
                 <div class="panel-body  text-center padding-6" tabindex="0" style="background-color:MediumSeaGreen">
                    <div class="m0 Currenttotal" style="font-size: 20px;color:white;"></div>
                       <div class="m0 totaltickets" style="font-size: 20px;color:white;"><?php echo $tickets_count;?></div>
                         <h5 style="margin-top: 3.5px;margin-bottom: 3.5px;color:white;">Current Month Tickets count</h5>
                              <div class="col-xs-7 text-right">
                                 <em class="fa fa-tags fa-1x" style="color:white;"></em>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                    </a>

Consider the screenshot added.Here is the reference image

In the above picture, there is a filter. I need to pass that value to the cards below so that, after clicking on a card I can redirect to respective pages and show the filtered records by using the selected filter in the drop down

  • 1
    You'd need an ajax call – Cid Aug 31 '20 at 13:14
  • Hi @Cid, thanks for the response. I am trying to add this ```var myJavascriptVar = $('#phase').val();``` value to a PHP variable without loading the page. Is it possible to add the JS value to a PHP variable? – chandrasekharreddy kotha Aug 31 '20 at 13:38
  • 2
    Yes, using an ajax call. – Cid Aug 31 '20 at 13:40
  • Hi @Cid, please consider the updated question. I am going to add the code I have used so far. And can you please help me with updating my code to fix my issue? – chandrasekharreddy kotha Aug 31 '20 at 13:49
  • 2
    What does that mean, you "can't afford an AJAX call"? Have you read and understood the linked duplicate? What you're trying *cannot* work. You can pass data using a cookie, which is set on page load and sent back to the server on the next page load. Whether you can make that mechanism work for you we don't know. Apart from that, you'd send data using URL query parameters, or perhaps POST data. Apart from *that*, there's AJAX. We don't know which of those does or doesn't work for you or why. – deceze Aug 31 '20 at 13:52
  • Hi @deceze, can you please help me to get the ```$('#phase').val(); ``` to the php variable ```$myPhpVar;``` without page loading – chandrasekharreddy kotha Aug 31 '20 at 13:56

0 Answers0