Return String from Server PHP File to Client Web Page -
there's lots on can't find works in case. following 2 test files php , html. html file has 2 text boxes button each. following occur:
the upper textbox data sent php file when 'send' button clicked. this works!
the php file opens text file , appends data. this works!
on clicking 'receive' button, php file opens text file , reads data. this works!
now i'm stumped. how data php file lower textbox in in html file?
note: i'm using jquery.redirect
plugin transferring data.
html file:
<!doctype html> <html lang='en'> <head> <meta charset="utf-8"> <script src='jquery-2.1.4.js'></script> <script src="jquery.redirect.js"></script> <title>test</title> </head> <body> <input id='send' type='text'></input> <div id='btnsend'><center>send</center></div> <input id='receive' type='text'></input> <div id='btnreceive'><center>receive</center></div> <style> #btnsend { width: 60px; border: solid 1px; cursor: pointer; } #btnreceive { width: 60px; border: solid 1px; cursor: pointer; } </style> <script type="text/javascript"> $('#btnsend').click(function(){ var dataout = $('#send').val(); $.redirect('http://troncon.ca/eso/test.php',{ userout: dataout}); }); $('#btnreceive').click(function(){ $.redirect('http://troncon.ca/eso/test.php',{ userin: ''}); }); </script> </body> </html>
php file:
<?php if(isset($_post['userout'])) { senddata(); } elseif(isset($_post['userin'])) { getdata(); } function senddata() { $handle = fopen('test.txt', "a"); $test = $_post['userout']; fwrite($handle, $test); fclose($handle); } function getdata() { $filename = "test.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); //echo $contents; } ?>
do data fetch , send using ajax:
an example based on code be:
$('#btnsend').click(function() { var dataout = $('#send'); var data = { userout: dataout.val() }; $.post("http://troncon.ca/eso/test.php", data, function(data, status) { alert("data saved "); }); }); $('#btnreceive').click(function() { var datain = $('#receive'); var data = { userin: '' }; $.post("http://troncon.ca/eso/test.php", data, function(data, status) { datain.val(data); }); });
Comments
Post a Comment