简体   繁体   中英

how to pass the javascript variable in to php in same page

How can i assign the javascript variable in to php.

$("button").click(function() {

        var val = (this.id);

$.ajax ({
            url: "date.php",
            //data: { val : val },
            data:'q=' + val,
            type: "GET",
            success: function(result) {

             if(result==1)
             {
            page = "<?php echo $today;?>";
             }
            else if(result==2)
            {
            page = "<?php echo $this_week;?>";
            }
            else if(result==3)
            {
            page="<?php echo $this_month;?>";
            }   
            else if(result==4)
            {
            page="<?php echo $previous_month;?>";
            }   
            else
            {
            "<?php echo $today;?>";
            }   
           }
        });
    });

Here i want to pass the variable 'page' to php,for to assign the export excel filename.I done like this in php

<?php
$page = ('<script type="text/javascript">page</script>');
?>

$page variable i want to assign here.

<script>
    saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "<?php echo $name[0] ?>- Report-<?php echo $page?>.xlsx");
    };
</script>

But am getting error,otherwise any other method to assign the variable.

my date.php

<?php
    $var=$_GET['q'];
    if($var == 'getToday')
    {
        echo "1";
    }
    elseif($var =='getWeek')
    {
      echo "2";
    }
    else if($var =='getMonth')
    {
      echo "3";
    }
    else if($var =='getpremon')
    {
      echo "4";
    }
    else
    {
      echo "5";
    }
?>

Basically you cannot run the client script or assign values to the server script but to achieve our goal we can use the ajax to assign the jquery variable to php

$.ajax ({
        url: "date.php",
        //data: { val : val },
        data:'q=' + val,
        type: "GET",
        success: function(result) {

         if(result==1)
         {
        page = "<?php echo $today;?>";
         }
        else if(result==2)
        {
        page = "<?php echo $this_week;?>";
        }
        else if(result==3)
        {
        page="<?php echo $this_month;?>";
        }   
        else if(result==4)
        {
        page="<?php echo $previous_month;?>";
        }   
        else
        {
        "<?php echo $today;?>";
        }
       $.ajax({
           url:'url',
           type:'GET/POST',
           data:{page:page} <-----This send the value from js to php 

           })   
       }
    });
});

here URL is the location where you want to get the variable page value

Just use the java script variable "page",

<script>
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "<?php echo $name[0] ?>-       BI Usage Report-"+page+".xlsx");
};

Don't need to mix java script variable to php.

else you can return values directly from mydate.php,See the code below

<?php 
$var=$_GET['q'];


    if($var == 'getToday')
        {
         echo $today;
        }
    elseif($var =='getWeek')
        {
          echo $this_week;
        }
    else if($var =='getMonth')
        {
           echo $this_month;
        }
    else if($var =='getpremon')
        {
          echo $previous_month;
        }
    else
        {
          echo "5";
        }

 ?>

And $("button").click(function() {

    var val = (this.id);

     $.ajax ({
        url: "date.php",
        //data: { val : val },
        data:'q=' + val,
        type: "GET",
        success: function(result) {
          page = result
         }
    });
});

Now you can use java script variable "page".

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