0

I need to display the content(per page) based on the option(per page) selected by the user

for which i need the selected option value in the same php page to display the content

but i cannot receive the value in php code ,Kindly give some solution to this problem

I've tried Pass Javascript variable to PHP via ajax http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_get2 http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_option_value

HTML Code for selecting option :

     <select id="page_count" >                                          
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="30">30</option>
     </select>

JavaScript when option changed :

     var total_items;
        $(document).ready(function(){
            $("#page_count").change(function(){
                var x = document.getElementById("page_count").selectedIndex;                    
                total_items=document.getElementsByTagName("option")[x].value;
                alert(total_items);

AJAX for posting the selected option :

            $.ajax({

                        type : "POST",                  
                        url : "start_landing_page.php",                                     
                        data : {total_items: total_items},                  
                        success : function(data){
                                alert("sucess!");
                                }
                        });     

received via AJAX in php :

           <?php 
              if(isset($_POST['total_items'])){                 
                $uid = $_POST['total_items'];
                var_dump($uid);
                  } 
               >
Community
  • 1
  • 1
madhavan
  • 50
  • 7

2 Answers2

0

The POST request should send data like this:

    data : 'total_items='+encodeURIComponent(total_items),
rave
  • 1,022
  • 1
  • 12
  • 23
  • I'm getting the value of javascript to php on $var_dump ,how to use that value with php variable within a same page – madhavan Jul 02 '14 at 16:11
0

I feel you can use many options to get and pass the value, following is the simplest way you can access the selected value and pass it in your ajax function:

$("#page_count").change(function(e){
 total_items = $(this).val();
 ....
 ...
});

Full Example: Jquery Receive selected option and Passing it in Ajax by using $.param()

Austin N
  • 233
  • 2
  • 3
  • 11
  • Hi Austin thank you , but i need the javascript variable value on the same page via php variable to display content from server – madhavan Jul 02 '14 at 16:04
  • Yes exactly you will need to pass the values in query format in your ajax function and then you can access the value in PHP via $_POST @madhavan `var queryParams = {"total_items": total_items}; //in ajax use dataParams in data parameter var dataParams = $.param(queryParams);` and in PHP `echo $_POST["total_items"]` . Check out my example for more info. – Austin N Jul 03 '14 at 05:41
  • Austin The value came which i checked it via inspect element > network > preview total_items=20 but i cannot use that value in my for loop – madhavan Jul 03 '14 at 15:47
  • Could you give an example for the same – Austin N Jul 13 '14 at 10:44