1

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
    }
2
  • 1
    no. jquery cannot "assign" anything in PHP. but you CAN have jquery perform a regular http form submission (either directly or via ajax), which would cause the browser and PHP to create key=value pairs in _GET/_POST for you. Commented May 8, 2015 at 21:56
  • Can you give me some tutorials or some code where i can learn that from? Thanks. Commented May 8, 2015 at 22:01

2 Answers 2

3

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()

Sign up to request clarification or add additional context in comments.

2 Comments

Can i do that on same page? I mean not using any additional php files? Is that possible? Thanks
Yes you can do it. just define a form and submit button . form without action . and on bottom the php code what i gave. no jquery is required
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.