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:

  1. the upper textbox data sent php file when 'send' button clicked. this works!

  2. the php file opens text file , appends data. this works!

  3. on clicking 'receive' button, php file opens text file , reads data. this works!

  4. 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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -