2

I have a file called lightstatuspage.php and within it, I have HTML, JavaScript and PHP code. I have used some variables within the JavaScript part of the code and I am trying to send these variables to the server by passing them to the PHP part of the code. However, this is not working.

I am using $.post("lightstatuspage.php", slider_val); and then in the PHP part, I am calling the variable by doing $_GET['rangeslider_val'];.

What am I doing wrong and what can I do differently to get the variable from JavaScript and send it to the server?

    function show_value(x)
    {
       document.getElementById("slider_value").innerHTML=x;

       event.preventDefault();
       var slider_val = x;
       var query = new Parse.Query(Post);
       query.first({
          success: function(objects){
            objects.set("slider_val", slider_val);
            objects.setACL(new Parse.ACL(Parse.User.current()));

            return objects.save();

            window.location.href = "lightstatuspage.php?rangeslider_val=" + slider_val;
          }
       })
    }

The PHP code is:

<?php
    $_GET['rangeslider_val'];

    print($rangeslider_val);
?>
6
  • return leaves the function immediately. Nothing will be executed after a return. Also you cannot return anything from a function that uses a callback. Just remove the word return. Commented Nov 22, 2015 at 6:46
  • The return part is used to save my variable in parse.com. If I remove it, it will not save. Commented Nov 22, 2015 at 7:41
  • Remove return - keep the objects.save(); Commented Nov 22, 2015 at 7:53
  • Thank you for your help. This allowed it to keep storing the values. Commented Nov 22, 2015 at 8:25
  • I see you are using parse - that means you should be able to access your stuff from php VIA parse and not via $.get as I suggested. I do not know parse framework so I cannot help. Commented Nov 22, 2015 at 10:08

4 Answers 4

5

First Add Jquery

<script src='https://code.jquery.com/jquery-1.11.3.min.js'></script>

to the end of page before closing body tag. To send Javascript variable to PHP. the best way is to use Ajax. and add the following code to your javascript. Do not forget that the below code should be on an event. For example on a button click or something like that

$( document ).ready(function() {
    var x = $('#input1').val();
    //or var x= 15;
    $.post("lightstatuspage.php",
    {
      'rangeslider_val': x
    },
    function(data, status){
    //alert("Data: " + data + "\nStatus: " + status);
   // you can show alert or not
    });
});

and in php, you can use:

$value = $_POST['field1'];

now your variable is in $value in php.

P.S: Backend Page and HTML page should be on same domain or you have to check Cross-Origin Resource Sharing


Second Solution as the User accepted this solution here would be the code:

$.get("lightstatuspage.php?rangeslider_val=" + slider_val,function(res) {
      console.log(res);
});

the second way is only the difference between POST and GET method


Third Solution if you don't want to use Jquery in your project and you need pure javascript you can use below code

    var str = "Send me to PHP";
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.responseText);
        }
    };
    xmlhttp.open("GET", "lightstatuspage.php?rangeslider_val=" + str, true);
    xmlhttp.send();
Sign up to request clarification or add additional context in comments.

11 Comments

Please mention that you are adding jQuery to his code dependencies.
I tried this but unfortunately it's still not working. Is the #input1 part of it calling a variable from HTML?
#input1 is the input that holds your data. it can also be var x = 15
$.get("lightstatuspage.php?rangeslider_val=" + slider_val,function(res) { console.log(res);});
@Traveling Tech Guy Yes, I already had jQuery in my code, thank you for the reminder.
|
1

Change the order of the last 2 lines of your JS function. You are returning from the function before changing the page's location.

5 Comments

I tried that but unfortunately it didn't change anything. The return part is returning the function to parse.com to save my variables there, so it's not dealing with changing the page's location. But should I include a return function for that part as well?
Also, if the .save operation is asynchronous, you should let it finish before browsing away. See if it accepts a callback as a parameter, returns a Promise, or provides any other async mechanism. Currently, your logic does not support the functionality you're trying to achieve.
Is there a better way to send the slider_val variable to PHP and use it there?
That depends entirely on what you need to achieve. You could, for example, transfer the value to PHP, preferably using AJAX, so you won't navigate away from the current page. On the PHP page, you can update your Parse objects, and return a success/error reply to the original page.
Thank you for your help.
0

you forgot to store variable on print

<?php

     $rangeslider_val = $_GET['rangeslider_val'];
     print($rangeslider_val);

?>

Comments

0

You call $_GET but you don't assign the value to the variable $rangeslider_val in PHP and you are returning in JavaScript before calling the PHP script. Also you mentioned that you want to use $.post from clentside if you do it this way you have to use PHP $_POST to get it from there.

1 Comment

The returning part of it is to store the variable in parse.com, it doesn't have to do with the PHP part of the code. Also, unfortunately even after assigning the value to the variable in PHP and using $_POST, it isn't working.

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.