Spy
Spy

Reputation: 149

Get input field value without refreshing page

<form action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <input type="submit" value="Submit">
</form> 

I have this form. One text field and one submit button. I want to store the string that the user will enter in the text field in a php variable to use it later. Is that possible?

Upvotes: 1

Views: 2611

Answers (1)

Vasiliki M.
Vasiliki M.

Reputation: 372

You could use ajax in combination with jquery if you'd like and pass the response from your request immediately to the input. Something like below:

$(document).ready(function() {
 $("a").on('click', function(){
   var inputval = $("#changeText").val();
   var postdata = {inputval: inputval};
   var url = "http://...";

   $.ajax({
       url: url,
       method: "POST",
       data: JSON.stringify(postdata),
       success: function(response) {
         $("#changeText").val(response.data);
       }
   });
});

Upvotes: 1

Related Questions