如何将值从javascript传递到php变量?

时间:2018-08-28 14:47:55

标签: javascript php jquery

我想根据访问者的屏幕大小将值从javascript变量传递给php变量。 但是价值没有过去。

JavaScript代码:    

jQuery( document ).ready( function( $ ) {

 $(window).resize(function() {
  var $window = $(window);
  if (jQuery(window).width() < 900) {
  var exampleJS = 3;
  //alert(‘<? echo $postcol;?>’);
  }
 else {
 var exampleJS = 4;
//alert(‘<? echo $postcol;?>’);
 }
 });
 //var exampleJS = “hi!”;
$.ajax({
url: window.location, //window.location points to the current url. change   is needed.
 type: ‘POST’,
data: {
examplePHP: exampleJS;
 },
 success: function( response){
  console.log(“Successful! My post data is: “+response);
 },
error: function(error){
   console.log(“error”);
  }
  });

  });
 </script>

PHP代码- 这是php代码。

<?php
$postcol = $_POST[‘examplePHP’];
if ( $postcol== 3){
    postContent=’mega-grid-posts-content’;
    $ajaxPost=’Publisher::bs_pagin_ajax_mega_grid_posts’;
} else{
    postContent=’mega-grid-4posts-content’;
    $ajaxPost=’Publisher::bs_pagin_ajax_mega_grid_4posts’;
}
?>

0 个答案:

没有答案