code_legend
code_legend

Reputation: 3285

Retrieving JS data in PHP

I am recording the information using the data() method

   optionScope.data().stage = 'a';
    where optionScope = $(this);

where I would like to store this value in my php code:

<?php

  include("functions/db.php");
  $format = "Online Course";
  $stage = GETTING THIS DATA;
  $topic = "Idea Generation";
  $resources = "select * from resources where format LIKE  '".$format."' and stage LIKE '%".$stage."%' ";

  $run_query = mysqli_query($con, $resources);

  while($row = mysqli_fetch_array($run_query)) {


     $stage = $row['stage'];


   echo "$stage  <br>";

 }
 ?>

Update: How the data is being sent

optionScope.data().stage = 'b';
  $.ajax({
        url: "functions/contact.php",
        type: "post",
        data: {stage: optionScope.data().stage}
    });

How the data is being retrieved

<?php

      $stage = $_POST['stage'];
      echo $stage;

?>

Upvotes: 1

Views: 64

Answers (2)

guychouk
guychouk

Reputation: 681

You can't directly transfer data between JS and PHP (unless you use string interpolation inside <script> tags which is a big no-no), since JS deals in the client-side and PHP on the server-side.

You need to make a request to the server and then manipulate the data there.

Take a look at AJAX requests. For the easiest implementation of this, see JQuery's AJAX method.

Upvotes: 1

Sachin Tyagi
Sachin Tyagi

Reputation: 143

you can use the AJAX method. This is the only way you can transfer your JS data into PHP script.you will get the Jquery data in GET or POST variables. jQuery AJAX has the various method you can choose as per your requirement. http://api.jquery.com/category/ajax/

Upvotes: 1

Related Questions