Click here to Skip to main content
15,886,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to get the value of a javascript textbox in some php code and then save the php txt on a server.

What I have tried:

Name: <input type="text" id="myText" value="Mickey">

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  document.getElementById("myText").value = "Johnny Bravo";
}
</script>


   <?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "myText\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>


$txt = *******?;


what is the code from javascript textbox = *******
Posted
Updated 21-Apr-20 2:42am
Comments
ZurdoDev 21-Apr-20 7:36am    
You have the code for setting it, document.getElementById("myText").value = "Johnny Bravo";

so, getting it is just as easy.

var name = document.getElementById("myText").value;
Mark Rene Jensen 21-Apr-20 16:19pm    
I got it, I found a swedish page: https://www.sweclockers.com/forum/trad/1013247-hjalp-php-fwrite

It work fine
Mark Rene Jensen 23-Apr-20 7:41am    
I need help again as sweden link use:

I have this input and wanna to save it with a var but how?


1 solution

PHP runs on the server, and generates a response which is sent back to the client. Javascript runs on the client, long after the PHP code has finished.

To send values from the client back to the server, you either need to submit a <form>, reload the page with the values in the querystring, or make an AJAX request.

Ajax - Developer guides | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900