0

I am using a jquery calendar script that assigns the content of a div using ajax.

$('#details-event-id').html(id);

The div tag:

<div id="details-event-id"></div>

is in another file and displays the id correctly.

<div id="details-event-id">91</div>

Is it possible to get the id, 91, to be assigned to a PHP variable?

4
  • JavaScript runs in the browser, PHP runs on the server Commented Aug 11, 2016 at 13:07
  • Why do you want to do this? Commented Aug 11, 2016 at 13:15
  • Trying to get that value to pass in a link for a registration script. Commented Aug 11, 2016 at 13:20
  • everything is possible, nearly... Look at my answer ;) Commented Aug 11, 2016 at 14:23

2 Answers 2

2

Using ajax you can send the value to the server and then assign to the php variable.

If you think you assign php variable in any javascript event in client side, its not possible without any asynchronous calling of the server script.

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand correctly you would like to send the variable id to a PHP file. You can achieve this by using AJAX. Bellow I'm showing two ways to get it done.

var id = $('#details-event-id').html(id);

AJAX with good old JS

ajax(myfile.php, {id:id}, function() {
  // the following will be executed when the request has been completed
  alert('Variable id has been sent successfully!');
});

function ajax(file, params, callback) {

  var url = file + '?';

  // loop through object and assemble the url
  var notFirst = false;
  for (var key in params) {
    if (params.hasOwnProperty(key)) {
      url += (notFirst ? '&' : '') + key + "=" + params[key];
    }
    notFirst = true;
  }

  // create a AJAX call with url as parameter
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      callback(xmlhttp.responseText);
    }
  };
  xmlhttp.open('GET', url, true);
  xmlhttp.send();
}

AJAX with JQuery

$.ajax({
  url: 'ajax.php', //This is the current doc
  type: "GET",
  data: ({
    id: id
  }),
  success: function(data) {
    // the following will be executed when the request has been completed
    alert('Variable id has been sent successfully!');
  }
});

Offtopic: you can see why some prefer JQuery...

When either function (Jquery or plain JS) is launched it will send the variable id to the file myfile.php. In order to retrieve the variable from the call you won't use $_GET[...] but $_REQUEST[...].

myfile.php

<?php

if(!isset($_REQUEST['id'])) {
  echo "no variable was sent";
}

$id = $_REQUEST['id'];
// process data, save in db, etc ....

You may return a value to the JS callback function by using echo() not return

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.