PDA

View Full Version : realtime transfer of variables to php without refresh



kobo1d
06-22-2012, 06:49 PM
hello there!

i was facing this problem whole day and i did not come up with a solution, so i ask you guys for help.

my code looks like this: (using jquery)



(more code here)


$.post("test.php", {encryptedPassword2: encryptedPassword}, function(data){
console.log(data); //DEBUG: print the result of this action to debug console
});
</script>

<?php
$rsapassword = $_POST['encryptedPassword2'];
print_r($_REQUEST); //DEBUG: print out all get/post
echo $rsapassword;


(more code here)


now when i load the page, above this code the script calculates "encryptedPassword" in javascript and in the same page load i want to transfer this variable, so my "$_POST['encryptedPassword2']" is set and can be used further on in the php script without the need of a page refresh...

in this above example the output of the page load is an error, since the variable isnt set when php calls for it. but in the console output of javascript "console.log(data);" the output looks right. over there the variables are set and print out. so the ajax response comes way later, when page is allready finished loading.


sorry im not really good in this, so i would appriciate any help!!

thx in advance

kobo1d
06-22-2012, 09:16 PM
since i believe this is kinda impossible without calling another page or recalling the same, i ended up with sumething else. for those who are intrested ->

i made it so the calculation i need from javascript plus only the really requiered php code are at top of the script and before the php post variable isnt set by recalling the page via ajax, the rest of the script dies (will be ignored). for example it can dies with a string like "Refreshing..."

so with this


$('body').replaceWith(http.responseText)

placed in the ajax-success-function from jquery, it simply replaces the "old" content with the "new" one, that was executed by ajax with the php post variable correctly set.


this might be a nasty way to do it, but it works... :)