0

How to echo the text inside the text box on button click? This is what I am trying to do:

<script type="text/javascript">
$(function() {  
    $('#button').click(function(){
        $.ajax({
            type: "POST",
            url: "index.php",
           data: {text:$('#name').val()}
        });
    });
});
</script>

<button id="button" type="button">submit</button>


<form method="post">
          <input type="text" id="name" name="name" size="31" placeholder="name ..." />
</form>

<?php echo $_POST['text']; ?>
Bobi Qnkov
  • 29
  • 1
  • 2
  • 9

3 Answers3

1

If what you want to display is the response from the server, you could do it that way:

$(function() {  
    $('#button').click(function(){
        var xhr = $.ajax({
            type: "POST",
            url: "index.php",
           data: {text:$('#name').val()}
        });
    });

    xhr.done(function(response){
        $('body').append(response);
    });
});

Here, I appended it to the body, as I don't know where you want to insert it.


If you want to display what is in the text input, replace that line with

$('body').append($('#name').val());
blex
  • 24,941
  • 5
  • 39
  • 72
0

This you need

$(function() {  
  $('#button').click(function(){
    var name = $('#name').val();
    $.ajax({
        type: "POST",
        url: "index.php",
        data: {text:name},
        success:function(){ $("#result").html(name); }
    });
  });
});

and u need to put these html tag bellow form <div id="result"></div>

user3349436
  • 151
  • 5
0
 $.ajax({
        type: "POST",
        url: "index.php",
        data: "text="+$('#name').val()
 });
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123