too long

I have a table data [Columns: id,question,answer each question have answer]; In frontend/UI i have a textarea while i paste a question to this field which search for exact question in db and show the result. I want ajax no need to click any search button. I want this to work when I paste question in to the text area.

Code i am using

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">       
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CSS3</title>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  </head>
      <body>
        <div class="container">
          <div class="jumbotron">
            <h1>PHP5</h1>
    <form class="form-inline">
      <div class="form-group">

         <input size="100" type="text" id="searchid"  class="form-control" rows="10" cols="100" />
      </div>

     <div id="resultdiv"></div>
    </form>
          </div>
        </div> <!-- /container -->
        <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
      </body>
    </html>

jQuery:

<script type="text/javascript">
        $(document).ready(function() {

            $('#searchid').keydown(function (e){ // Event for enter keydown.

            if(e.keyCode == 13){

            var idvalue = $("#searchid").val(); // Input value.

                $.ajax({ //Ajax call.

                    type: "GET",
                    url: "search.php",
                    data: 'id=' + idvalue , 
                    type: 'json',
                    success: function(msg){
                        // Show results in textareas.
                        msg = JSON.parse( msg );  // Line added 

                        alert (msg);

                         $('#resultdiv').val(msg.answer);

                        }

                    }); // Ajax Call
                } //If statement

        }); //document.ready
    </script>

My Search.php

      <?php

     if ($_GET['id']):   

        $dataid = json_decode($_GET['id']);


        // Connect to database.
        $con = mysqli_connect("localhost","root",""); 
        mysqli_select_db ($con,'exam_css3'); 

        // Get the values from the table.
        $sql = "SELECT answer FROM exam_css3 where question LIKE '$dataid' ";
        $result = mysqli_query($con,$sql);

        while($row = mysqli_fetch_assoc($result)) 
        {   
        $answer = $row[answer]; 

        }

    $rows = array('answer' => $answer); 

    echo json_encode($rows);

    endif;

    ?>

This code is not working, can anyone help on this?

You are defining twice the type in your ajax. json is the dataType not simple the type. type is get, what you do not need to set, that is the default.

The second problem is, you pass your data as a string, not as a json object, so on your server side, that will be an array, what you can not json_decode.

There are, among other things, some issues in your PHP.

First of all you search for $dataid, which means an exact match. You need to do

"SELECT answer FROM exam_css3 where question LIKE '%{$dataid}' ";

Then you always save only one answer, and you do not specify quote marks around 'answer', which might cause a PHP warning, which would corrupt the JSON output:

    while($row = mysqli_fetch_assoc($result)) 
    {   
        $answer = $row[answer]; 
    }
    $rows = array('answer' => $answer); 

   echo json_encode($rows);

endif;

So you might want to rewrite that as

<?php

if (array_key_exists('id', $_GET)) {

    $dataid = json_decode($_GET['id']);
    // Here it would be good to check whether the decoding succeeded.
    // I'd also try doing in HTML:   data: { id: idvalue }

    // Connect to database.
    $con = mysqli_connect("localhost", "root", ""); 
    mysqli_select_db ($con,'exam_css3'); 

    // Get the values from the table.
    // Only interested in one match.
    $sql = "SELECT answer FROM exam_css3 where question LIKE '%{$dataid}%' LIMIT 1";
    $result = mysqli_query($con,$sql);

    $answer = mysqli_fetch_assoc($result);
    if (null === $answer) {
        $answer = array('answer' => 'nothing found', 'status' => 'error');
    }

    // Since we're putting this into HTML...
    $answer['answer'] = HTMLspecialChars($answer['answer']);

} else {
    $answer = array('answer' => 'no query was supplied', 'status' => 'error');
}
Header ('Content-Type: application/json');
die(json_encode($answer));

In the code above I have added a 'status' variable so that in the jQuery you can do

if (msg.error) {
    alert("Error: " + msg.answer);
    return;
}

and further differentiate between correct and incorrect answers.

Other issues exist (for example you ought to use PDO and switch to prepared queries; as things stand, if the question contains a quote sign such as

What's a SQL injection?

your SQL search would throw an error. This is not limited to SQL injection. NO QUERY CONTAINING QUOTE MARKS WILL WORK. You need at least to escape the string dataid before placing it in the query.