-1

I am very new to all this and have spent the last few hours looking at multiple sections of code on various pages. I am trying to pass a single variable from a PHP page to a javascript function page. The PHP variable will be filled from a database entry. On the javascript function page, I am going to use the variable in an if statement to decide what to do. Below is the section of code I have in the PHP file.

if($UserName == $username && $UserPass == $password){
    $_SESSION['useris'] = $UserID;
    $_SESSION['access_level'] = $Authlevel;
    echo json_encode($Authlevel);
    header('Location:../HTML/AdminModal.html'); //removed for testing of the auth level
}

I do not think that I have actually sent the data anywhere as yet.

Then in the javascript file, I am totally lost as to how to get the information I have passed.

I know there is a lot of big examples of various data being passed back and forth but I am struggling to understand or work out how to adapt to my needs.

Thanks

blackandorangecat
  • 1,246
  • 5
  • 18
  • 37
bmunro
  • 1
  • 1
  • I'm assuming you've already read through this question... http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – blackandorangecat Apr 02 '17 at 17:43
  • Yes thanks. As I said I am very new to this and i could not see how the data was being passed. I am not looking to get the variable in another PHP or HTML page, I am looking to call, and use it directly in a javascript function page as a controller in an IF statement. – bmunro Apr 02 '17 at 18:04

2 Answers2

0

You need to make an AJAX call to your server.

Javascript must be like

$.ajax({
    type: "POST",
    url: "ajax_post.php",
    data: {user: userid, pass: password}, // any data you would like to send with request
    dataType: "JSON",
    beforeSend: function(){
        // you may want to show a loader or any message before sending request
    },
    success: function(response){
        console.log(response);
        // do further processing of your response and carry out any redirections from here, if required, using window.location.href = "../HTML/AdminModal.html";
    }
});

You server side file i.e. ajax_post.php would contain

..
if($UserName == $username && $UserPass == $password){
    $_SESSION['useris'] = $UserID;
    $_SESSION['access_level'] = $Authlevel;
    echo json_encode($Authlevel);
}
Yogesh Mistry
  • 2,082
  • 15
  • 19
  • thanks for that. I tried it and was able to see the request being made(simple alert ), however there was nothing coming back to the page. I have never used ajax before yesterday and am still trying to find my way round it. – bmunro Apr 02 '17 at 19:33
  • if you've just started learning, i would suggest you to make a simple ajax call with the code i've posted above (replace `data: {user: userid, pass: password},` with `data: {user: "dummy user id", pass: "dummy password"},`), and then on the post page i.e. `ajax_post.php`, just write `echo json_encode("this is the response");`. Just do this to find whether you are able to make a successful ajax request and check whether `console.log(response);` is printing out our message `this is the response` in console or not – Yogesh Mistry Apr 02 '17 at 19:44
0

Another option is to echo the variable directly into your Javascript function, this assumes the variable is available at the time. Something like:

function someFunction()
{
 if(<?php echo variableName;?> == value)
}
CharlesEF
  • 608
  • 1
  • 15
  • 25
  • May I assume that this is only acceptable when the javascript and php are on the same page. I have tried that and start getting alot of red warnings. My javascript page and PHP are seperate. – bmunro Apr 02 '17 at 18:43
  • No, the only requirement is that the PHP variable is available. The value can be echoed in any Javascript page. Or, you could echo it as a global variable and use it in any Javascript code. I would need more details about the errors you get before I can comment on them. Since they are on different pages you may need to set the variable as a Session variable in order to use it on a different page. – CharlesEF Apr 02 '17 at 19:11
  • Thanks for the help, as soon as I type if ( == "elevated") it goes red and says: expression expected, statement expected, : expected and a few more. that made me think that it was not mean to be put into a javascript page at all. – bmunro Apr 02 '17 at 19:26
  • First, Authlevel is not a valid PHP variable. All PHP variables should start with the $ symbol. Second, it appears Authlevel is a string so you need quotes in Javascript, like this: if ("" == "elevated"). Do you use the header command to redirect to the other page? If yes, then you need to use Sessions to hold the variable name and value. Based on your description of the errors I would try inserting the quotes first. – CharlesEF Apr 02 '17 at 20:01
  • thanks, I did try with the $ before as well, but it was the same. i have added in the quotes as you suggested but it does not return the variable. if i print out the content of the variable it is undefined. I am wondering if the session idea is better, as to ge to the modal call javascript page i do indeed use header('Location:../HTML/AdminModal.html'); this in turn goes to my javascript page. it is at this point that i am looking for a variable to direct the path of which set of options should load. each set has its own modal. i was looking to use an if statement to choose which – bmunro Apr 02 '17 at 20:26
  • Once you use the header command the $_POST array is cleared. So this means you should use Session to hold the variable. Another option is to add the variable to your URL as a query string. Then in Javascript you can check to see if the query string variable is present and use it. There are lots of Javascript examples of getting query string values so I won't go into that. – CharlesEF Apr 02 '17 at 20:37
  • If you do go the query string route then I should mention that you can use the PHP $_GET array to echo the value, you don't need Javascript to query the query string. Like this: if ("" == "elevated"). Also, it is good practice to test that the value exists, like this: if ("" == "elevated"). That line checks Authlevel to be sure it exists and is not blank, then it echos the value in lower case. – CharlesEF Apr 03 '17 at 01:24