简体   繁体   中英

I can't get my ajax request to work

I cannot make the following AJAX request work:

I want to make the text entered in the field to be displayed in the "p" tag

Server: Apache 2.2 PHP 5

My HTML:

 <html> <head> <title>Test for reponder</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $function send() { $('body').on('click', 'button', function(event) { var namee = document.getElementById('name').value; //var dataString='name ='+ name; $.ajax({ type: 'POST', url: 'responder.php', data: namee, success: function(html) { $('#msg').html(html); } }) </script> </head> <body> <input type="text" id="name"> <!--<input type="submit" id="button" value="Send" onclick="return send();">--> <button type="button">Send</button> <p id="msg"></p> </body> </html> 

My PHP file:

 <?php $name=$_POST[ 'name']; echo "response:" . $name; ?> 

You can do something like this:

HTML

<input type="text" id="name">
<button type="button">Send</button>

<p id="msg"></p>

jQuery

<script>
    $(document).ready(function(){
        $(document).on('click', 'button', function(){
            var name = $("#name").val();
            var param = {name: name};

            $.ajax({
                type: 'POST',
                url: 'responder.php',
                cache: 'false',
                data: param,

                beforeSend: function(){
                    // before send
                },

                success: function(data){
                    // success
                    $('#msg').html(data);
                },

                error: function(){
                    // error
                }
            });
        });
    });
 </script>

responder.php

<?php

    if(isset($_POST['name'])){
        $name=$_POST['name']; 
        echo "response:" . $name; 
    }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM