0

I have a form that will go through AJAX to set a session variable and back to the original page.

My problem is that I cannot set the session variable, thus, it is not working.

my main PHP page

<?php
            if ($_SESSION['adminFunction'] == 'adminfunction'){
            echo "<form id='userOptionRequest' action='request.php'>";
            echo "<button name='adminFuncUserOption' onclick='adminFuncOption(1)'>User Option</button> <br /> <br />";
            echo "<button name='adminFuncUserOption' onclick='adminFuncOption(2)'>Subject Option</button> <br /> <br />";
            echo "<button name='adminFuncUserOption' onclick='adminFuncOption(3)'>Test Option</button> <br /> <br />";
            echo "</form>";
        }

        if ($_SESSION['adminFunction'] == 'addusers'){
            echo "hello world";
        }
?>

my JS file

function adminFuncOption(option){
if (option == 1){
    var userOptionRequest = new XMLHttpRequest();
    userOptionRequest.open('POST','request.php',false);
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    userOptionRequest.send('adminFuncUserOption=' + option);
    return(1);
} }

the request.php

<?php
session_start();
$option = $_POST['option'];

if ($option != NULL){
    if ($option == 1){
        $_SESSION['adminFunction'] = 'addusers';
        header('Location: http://rsc_naga_isd/admin');
    }
}

var_dump($_POST);
var_dump($_SESSION);
var_dump($_GET);

?>

when I go back to my main PHP page, the session should now be addusers and should display 'hello world'

I have research but still had a hard time understanding AJAX or PHP Submitting HTML form using Jquery AJAX http://www.ajax-tutor.com/post-data-server.html http://tutorialzine.com/2009/09/simple-ajax-website-jquery/

Oh, am still not familiar with jquery and still new to javascript, but would prefer javascript before diving in to jquery.

Community
  • 1
  • 1
jaa2013
  • 171
  • 1
  • 2
  • 11

3 Answers3

1

Hi jquery helps you to do more with less for example an AJAX call using jquery is like this:

$.ajax({
            url: url,
            type: 'POST',
            data: data,
            beforeSend: funtion(){
               //DO Something before send like print a Loading text
            },
            success: function(data){
               //DO SOMETHING IF DATA IS RETURNED
            },
            error: function(data){
               //DO SOMETHING ON ERROR
            }
        });

Then you can check on you php scipt for the values sent on data for example:

if you send this data value:

 data: {"adminFunction" : "mortal user not worthy" }

on your php script just do something like this:

switch($_POST['adminFunction'])
{
    case 'superAdmin':
       //....
    break;
    default:
      //DO Something
      break;
}

More info here:

AJAX JQUERY

DeividKamui
  • 312
  • 1
  • 3
  • 13
0

In javascript you send:

userOptionRequest.send('adminFuncUserOption=' + option);

but in php:

$option = $_POST['option'];

it should be

$option = $_POST['adminFuncUserOption'];

I know you want javascript, but jquery so much easier ;)

$.post("request.php", { adminFuncUserOption: option },  function(response) {
  console.log(response); 
});

that's it then just check for $_POST['adminFuncUserOption']

meda
  • 43,711
  • 13
  • 85
  • 120
  • I already have the session in the upper part of the PHP page. What I cannot do, or the page won;t do is the AJAX Request not setting the session varibale to what I want. – jaa2013 Dec 12 '14 at 08:22
  • @jaa2013the name is not the same `adminFuncUserOption` but you put `option`. fix it `$option = $_POST['adminFuncUserOption'];` – meda Dec 12 '14 at 08:24
  • still not working. at request.php, there is an error saying unidentified index `adminFuncUserOption` and I am trying to use post but the url says: `http://rsc_naga_isd/admin/request.php?adminFuncUserOption=` – jaa2013 Dec 12 '14 at 08:46
  • then its a query stirng `$_GET`. or use `$_REQUEST` to be safe – meda Dec 12 '14 at 08:47
  • @jaa2013 I have made an edit to my answer, does your script get data – meda Dec 12 '14 at 08:59
0

The server side of the code (PHP) executes prior to the javascript side of the code. The server creates HTML (with javascript) to the client browser. The client browser then executes the code and javascript.

While your Session variable was updated, the requesting page was already created. You are going to need to reload the page that made the request in order to recognize the change to the session variable.

You could have the AJAX pass back non-sensitive data that you can process with javascript. If this is secure information that you don't want to be hacked, then I recommend avoiding AJAX until you get a better handle of it.

Rick Paul
  • 76
  • 6