简体   繁体   中英

Jquery ajax call from datepicker onSelect disappears textbox

I have a textbox with datepicker jquery plugin. When i select a date ,post ajax call is made and i want to return some data from database and textbox should remain on top of the page so that furthur requests can also be made.

    <script>
        $(document).ready(function(){
        //create date pickers
            $("#datepicker").datepicker(
            { 
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy-mm-dd',
                onSelect: function(date)
                {
                    $.ajax({
                        type:"POST",
                        url:"seestuffs.php",
                        data:date,
                        success: function(result)
                        {
                              document.write(result);
                        }
                    });
                }
            });
        });
    </script>
    <body>
<p>Date: <input type="text" id="datepicker" name="date" /></p><br/>
    </body>

And here is my seestuffs.php

<?php 

if(isset($_POST['date']))
{
    $date = $_POST['date'];
    echo 'hello';

}


?>

But hello is not displayed on the page.Where am I going wrong!!Thanx in advance..

document.write will create a new document if called on a document that has already loaded. This is explained in this Question: JavaScript Document.Write Replaces All Body Content When Using AJAX .

So instead you need to add the result to the document either by appending a new node or using innerHTML eg:

success: function(result) {
   $('body').append('<p>'+result+'</p>');
}

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