Roman
Roman

Reputation: 37

Take value from input field using Jquery and assign it to PHP

I have small input field with button. It looks similar like this:
enter image description here
HTML

<input type="text" id="my_input"><span class="btn">Save</span>

JQUERY

$(".btn").click(function(){
        // send data to $_POST['#my_input']
    });

If user press "save", can i use jquery to assign PHP isset ?

    if(isset($_POST['#my_input'])){
      //action
    }

Upvotes: 1

Views: 4423

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

You can do it in following manner:-

<script>
    $( document ).ready(function() {
      $('.btn').click(function(){
        $textboxval = $('#my_input').val();
         $.ajax({
            url: 'page.php', // page name where you want to get value of that textbox
            type: 'POST',
            data : {'my_input':$textboxval},
            success: function(res) {
               // if you want that php send some return output here
                }
          });
    });
});
</script>

And on page.php

<?php

if(isset($_POST['my_input'])){
   echo $_POST['my_input'] ;
}

Note:- if you want to return to ajax back then you need to write return rather than echo and then you can get the value in your success response.

Upvotes: 2

empiric
empiric

Reputation: 7878

You can use the post or ajax-method of jQuery to submit your form-data asynchronous:

$(".btn").click(function(){
    var input = $('#my_input').val();
    $.post('url/to/file.php', {data: input });
);

In your php-file you can access the data like:

if(isset($_POST['data'])){
    //action
}

Reference

$.post()

Upvotes: 3

Related Questions