简体   繁体   English

在php页面之间传递jquery数据

[英]Pass jquery data beween php pages

I have a php class handler that I am using to create html output for my exercise-group.php page. 我有一个php类处理程序,用于为我的exercise-group.php页面创建html输出。 However, this output (an array of items) is called on by using Jquery/AJAX and added to the page. 但是,使用Jquery / AJAX调用此输出(项目数组)并将其添加到页面。 However, there are some data values that are not displayed because they will be passed onto the exercise-single.php page. 但是,有些数据值未显示,因为它们将传递到exercise-single.php页面上。 How can I gather these data values using jquery, load them into a php value and store them into a $_Session variable so the exercise-single.php can display these vars after the user clicks on on href tag. 我如何使用jquery收集这些数据值,将它们加载到php值中并将它们存储到$ _Session变量中,以便exercise-single.php可以在用户单击href标记后显示这些var。 Sorry for the long post but this is the best I can do to explain what im trying to do. 抱歉,很长的帖子,但这是我能做的最好的解释我试图做的事情。

Exercise.class.php 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 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 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);
});

}

//Handler //处理程序

 echo $exercises->displayExercises($result); 

//Exercise Class //运动课

  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 // jQuery在这里

$.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
      });
    }
  }
});

if you want to access data globally in all page per session you should create session like this in while block like this, 如果您想在每个会话的所有页面中全局访问数据,则应在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>";
    }
 }

this will work. 这将起作用。 and don't forget to start session in your php files. 并且不要忘记在您的php文件中启动会话。 remember, you should start session on every page of your php files in which you are going to set or get session. 记住,您应该在要设置或获取会话的php文件的每个页面上启动会话。 You could do this by simply adding 您可以通过简单地添加

session_start(); 在session_start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM