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

6
  • 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 Commented Oct 28, 2015 at 16:29
  • Possible duplicate of Using a javascript variable to set PHP variable Commented Oct 28, 2015 at 16:30
  • Can't continue to manipulate PHP Variables once the page has loaded. What is it you wish to do exactly? Commented Oct 28, 2015 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? Commented Oct 28, 2015 at 18:19
  • (where 246 could be any doubled number, of course) Commented Oct 28, 2015 at 18:20

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

5 Comments

How do I save data.show in a variable which I can use to, say, multiply it once again in a JavaScript function?
Edited and added the variable. Define it up front, and then populate it when you get the data. Now you can use it later.
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?
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
How about you link to a pastebin of your code. I feel like something is being lost in translation.

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.