在php页面之间传递jquery数据

时间:2019-02-14 04:44:28

标签: php jquery ajax

我有一个php类处理程序,用于为我的exercise-group.php页面创建html输出。但是,使用Jquery / AJAX调用此输出(项目数组)并将其添加到页面。但是,有些数据值未显示,因为它们将传递到exercise-single.php页面上。我如何使用jquery收集这些数据值,将它们加载到php值中并将它们存储到$ _Session变量中,以便exercise-single.php可以在用户单击href标记后显示这些var。抱歉,很长的帖子,但这是我能解释的最佳尝试。

Exercise.class.php     

class Exercises {

public $vidSource;

public function displayExercises($result) {        
    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {                                         
             echo   "<div class='media'>" .
                    "<div class='media-object pull-left'>" .
                        "<a href='exercise-single.php'><img src='".$row["ImgResource"]."' class='img-responsive' alt='curl'></a>" .
                    "</div>" .
                    "<div class='media-body'>" .
                        "<h4 class='media-heading'><a href='#'>".$row["Exercise"]."</a></h4>" . 
                    "</div>" .
                "</div>";
$vidSource = $row["VidResource"];
        }
    } else {
        echo "<img src='https://media.giphy.com/media/cwTtbmUwzPqx2/giphy.gif' class='img-responsive'>"; 
        echo "<h3 class='media-heading'>No workouts exist for this muscle yet.<br>Please try another one.</a></h3>";
    }
 }
}
?>

ExerciseHandler.php

 <?php

include 'Exercises.class.php';
include 'dbconnect.php';

if(isset($_POST['muscle'])){

$muscle =$_POST['muscle']; 

$connect = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT * FROM exercises WHERE Muscle = '".$muscle."'";
$result = $connect->query($sql);

$exercises = new Exercises();

$exercises->displayExercises($result); 

}
?>

loadExercises.js

var muscle_id;

function getMuscle(clicked_muscle){
muscle_id = clicked_muscle;


$.post("ExerciseHandler.php", {
  muscle: muscle_id
},
function(data, status){
    $("#exercise-list").html(data);
});

}

2 个答案:

答案 0 :(得分:2)

//处理程序

 echo $exercises->displayExercises($result); 

//运动课

  public function displayExercises($result) {        
   if ($result->num_rows > 0) {
      return json_encode(
       array(
       'status' => 'success',
       'data' => $result->fetch_assoc())
      );
   } else {
      return json_encode(
         array(
           'status' => 'error',
           'data' => array(
             'url' => "https://media.giphy.com/media/cwTtbmUwzPqx2/giphy.gif",
             'class' => 'img-responsive',
             'prompt' => 'Please try another one.'
           )
         )
      );
    }
  }

// Jquery Here

$.ajax({
  url : "ExerciseHandler.php",
  method : "POST",
  success : function(response){
    var result = JSON.parse(response);
    if(result.status == 'error'){
      $('img').attr('src',result[0].url);
      $('img').attr('class',result[0].class);
      $('h3').text(result[0].prompt);
    }else{
      $.each(result.data,function(index,value){
          // do the html append thing here
      });
    }
  }
});

答案 1 :(得分:1)

如果您想在每个会话的所有页面中全局访问数据,则应在 while阻止中像这样创建会话,

        if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
             $_SESSION["name"] = $row["column_heading"];//create session                           
             echo   "<div class='media'>" .
                    "<div class='media-object pull-left'>" .
                        "<a href='exercise-single.php'><img src='".$row["ImgResource"]."' class='img-responsive' alt='curl'></a>" .
                    "</div>" .
                    "<div class='media-body'>" .
                        "<h4 class='media-heading'><a href='#'>".$row["Exercise"]."</a></h4>" . 
                    "</div>" .
                "</div>";
$vidSource = $row["VidResource"];
        }
    } else {
        echo "<img src='https://media.giphy.com/media/cwTtbmUwzPqx2/giphy.gif' class='img-responsive'>"; 
        echo "<h3 class='media-heading'>No workouts exist for this muscle yet.<br>Please try another one.</a></h3>";
    }
 }

这将起作用。并且不要忘记在您的php文件中启动会话。记住,您应该在要设置或获取会话的php文件的每个页面上启动会话。您只需添加

即可完成此操作

session_start();