0

How do I retrieve the value I receive from the php file in json (echoed in id="show_result" from $show) as a php variable? I wanna be able to do further calculations in php on that variable in first.php

Algernop K.
  • 477
  • 2
  • 19
  • Once you've $('#show_result').text(data['show']); done that, you can have another button to take that value and send it through to first.php – Coz Oct 28 '15 at 16:29
  • Possible duplicate of [Using a javascript variable to set PHP variable](http://stackoverflow.com/questions/5948314/using-a-javascript-variable-to-set-php-variable) – user5325596 Oct 28 '15 at 16:30
  • Can't continue to manipulate PHP Variables once the page has loaded. What is it you wish to do exactly? – Twisty Oct 28 '15 at 18:17
  • @Twisty I would if I knew how Twisty. Even better would be if I can somehow save it in a javascript variable from {show: 246} that I get in my log. Any idea on how to do that and document.write it to check if it works? – Algernop K. Oct 28 '15 at 18:19
  • (where 246 could be any doubled number, of course) – Algernop K. Oct 28 '15 at 18:20
  • @Twisty Save the echoed text (the doubled number, {"show":246} or {"show":100}) in a JavaScript variable. – Algernop K. Oct 28 '15 at 18:23

1 Answers1

1

Given the expected result of:

{"show":246}
{"show":100}

Use $.getJSON() to better handle the results:

<script>
    var localShow;
    $('#number').on('keyup',function(e){
        if(e.which == 13){
        var get_var_name = $(this).val();
            $.getJSON(
                'result.php',
                { number:get_var_name },
                function(data,status){
                    console.log(data);
                    if(status == 'success'){
                        $('#show_result').text(data.show);
                        localShow = data.show;
                    } else{
                        alert('Status: ' + status);
                    }
                }
            });
        }
    });
</script> 

The script should read the JSON and create data as an object. You can then call show using dot notation: data.show should result in the value 246 or 100 depending on the result.

More details: http://api.jquery.com/jquery.getjson/

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • How do I save data.show in a variable which I can use to, say, multiply it once again in a JavaScript function? – Algernop K. Oct 29 '15 at 14:44
  • Edited and added the variable. Define it up front, and then populate it when you get the data. Now you can use it later. – Twisty Oct 29 '15 at 15:25
  • If I try to document.write it outside the script I get the error: Uncaught ReferenceError: localShow is not defined Must I parse it or something? – Algernop K. Oct 29 '15 at 15:46
  • A) If you're using JQuery, why are you using `document.write()`? B) If you define the variable in the wrong place, it's scope will only be for that function. Make sure you're defining outside `$(document).ready();` etc – Twisty Oct 29 '15 at 16:00
  • How about you link to a pastebin of your code. I feel like something is being lost in translation. – Twisty Oct 29 '15 at 16:03