John John - 29 days ago 10
Javascript Question

Assign javascript variable to php variable and post it on another page

Comment: Ajax added
Assign javascript variable to php variable and post it on another page

I use the Fullcalendar (javascript). When you click on the event it opens a form with its content.

eventClick: function(calEvent, jsEvent, view) {

content.val(calEvent.content); //content of event
$('#event').show();
formOpen('edit');

},


I want to do that when you click on the event its opens another php page and displayed its data in it.I want to post value through the session,but i dont know how to assign javascript variable to php variable.

eventClick: function(calEvent, jsEvent, view) {

var content = content.val();

<?php
print <<<HERE
location.href = 'anotherpage.php?id=$_SESSION[id]';
HERE;
?>

<?php
$content = content;
$_SESSION['content'] = $content;
?>

},


P.S: ajax did not work because it seems that i have a bit of a particular situation.

$.ajax({

type: "POST",
url: "anotherpage.php",
data: {content: content.val()},

});
Anotherpage.php

<?php
$content = $_POST['content'];
?>

Answer

Use AJAX :

ajax will send data under the hood.

Ajax framework :

Add this frame work under <script> on your calender page.

function ajaxObj(meth, url) {
    var x = new XMLHttpRequest();
    x.open(meth, url, true);
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState === 4 && x.status === 200){
        return true;    
    }
}

How to use :

On your calendar page :

eventClick: function(calEvent, jsEvent, view) {

var content = content.val();

var ajax = ajaxObj("POST", "Currunt_page_url.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {

            }
        }
//sends data by POST method to ur url page
        ajax.send("contents="+contents);

}

Add this at start of your calendar page :

<?php

if(isset($_POST['contents'])){
//save data of contents into session
$SESSION['contents']=$_POST['contents'];
location.header='URL_of_php_page.php';
//access session['id']/session['contents'] on your php page though session variables.
exit();

}
?>
Comments