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.phpCoz– Coz2015-10-28 16:29:24 +00:00Commented Oct 28, 2015 at 16:29
-
Possible duplicate of Using a javascript variable to set PHP variableuser5325596– user53255962015-10-28 16:30:31 +00:00Commented 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?Twisty– Twisty2015-10-28 18:17:25 +00:00Commented 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?Algernop K.– Algernop K.2015-10-28 18:19:35 +00:00Commented Oct 28, 2015 at 18:19
-
(where 246 could be any doubled number, of course)Algernop K.– Algernop K.2015-10-28 18:20:06 +00:00Commented Oct 28, 2015 at 18:20
|
Show 1 more comment
1 Answer
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/
5 Comments
Algernop K.
How do I save data.show in a variable which I can use to, say, multiply it once again in a JavaScript function?
Twisty
Edited and added the variable. Define it up front, and then populate it when you get the data. Now you can use it later.
Algernop K.
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?
Twisty
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(); etcTwisty
How about you link to a pastebin of your code. I feel like something is being lost in translation.