How to write a variable value in a text file with php -


i have problem php code. it's simple web page hosted on computer 2 buttons , text box. when click + or - button number in text box increase or decrease. works suppose to, except when want write textbox.value text file using php code. result "value = textbox.value" when should "value = 0". thank you. code below:

<!doctype html> <html> <body>  <input id="increasenumber" type="button" value="+" style="font-size:24pt; width: 50px; height: 50px;"><br> <input id="textbox" type="text" value="0" style="font-size:24pt; width: 40px; height: 40px;"> <label style="font-size:24pt;">&#8451;</label><br> <input id="decreasenumber" type="button" value="-" style="font-size:24pt; width: 50px; height: 50px;"><br>  <script>  decreasenumber.onclick = function() {    textbox.value = parseint(textbox.value) -1 } increasenumber.onclick = function() {    textbox.value = parseint(textbox.value) + 1 }  </script>  <?php $myfile = fopen("/home/pi/test/test.txt", "w") or die("unable open file!");  $txt = "value = "; fwrite($myfile, $txt); $v = textbox.value; fwrite($myfile, $v); fclose($myfile); ?>   </body> </html> 

brief explanation:

you trying combine 2 separate languages if both client-side "as needed" scripting languages.

javascript client-side, , such, run based on event listeners or other defined parameters. php server-side, , such, run when file parsed. means php processed , out of picture time javascript comes picture.

what happening in script presently:

the file parsed, php identified, , runs first. upon running, file gets written to. php defaults text-value of "textbox.value" not actual variable (which preceded $ if were). in honesty, should returning error "textbox.value" means nothing, (php not strict, makes lot of assumptions). it's not, however, because it's assuming referencing constant. after php processed, dom processed , sent browser. php not exist @ point , has been stripped away dom (rightfully - you'd never want php visible client).

what do:

you cannot run php snippet every time want increment/decrement value php being inside of same file (at least not without form submit, or "catch" request -- response based on fact want write when clicking, instead of submitting form every time). must change code. 1 thing i'd suggest placing php inside of own file. then, upon incrementing/decrementing value, use ajax hit file (thus triggering file write).

example:

index.html:

<!doctype html> <html> <head>     <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> </head> <body>  <input id="increasenumber" type="button" value="+" style="font-size:24pt; width: 50px; height: 50px;"><br> <input id="textbox" type="text" value="0" style="font-size:24pt; width: 40px; height: 40px;"> <label style="font-size:24pt;">&#8451;</label><br> <input id="decreasenumber" type="button" value="-" style="font-size:24pt; width: 50px; height: 50px;"><br>  <script>  $("#decreasenumber").on("click", function() {    $("#textbox").val(parseint($("#textbox").val()) -1);    writetofile($("#textbox").val()); }) $("#increasenumber").on("click", function() {    $("#textbox").val(parseint($("#textbox").val()) +1);    writetofile($("#textbox").val()); })  function writetofile(value){      $.ajax({         url: 'write.php?v=' + value,         type: 'get',         datatype: 'json',         success: function(ret){             alert('success!');         },         error: function(ret){             alert('error!');         }     });  }   </script>  </body> </html> 

write.php:

<?php $myfile = fopen("/home/pi/test/test.txt", "w") or die("unable open file!");  $txt = "value = " . $_get['v']; fwrite($myfile, $txt); fclose($myfile); ?>  

note changes:

i using jquery in example. apologize if wanted strictly native javascript.

also please note, changed php. had 2 writes, unnecessary , take more time. file input/output costly, , should used minimally. in fact, in example, i'd have written database. however, did not ask separate solution, provided example similar had written.

random thoughts

you can use decrement/increment instead of you're doing well. so:

<script>  decreasenumber.onclick = function() {     textbox.value--;     alert(textbox.value); } increasenumber.onclick = function() {     textbox.value++;     alert(textbox.value); }  </script> 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -