0

I'm new for Web sites.I have a text box in my page and what i need is when onBlur that text box, call a php method that include sql query.I found something like on the web, but still does net work it.Am i doing wrong?

<?php
if(isset($_POST['Greet']))
   {
      echo $_POST['Greet'];
  }
?>
<html>
<body>
<script type="text/javascript">

function sayHi()
{
     var value = $.ajax({
        type: "POST",
         url: "page.php",
        data: "Greet="+$("#Greeting").val(),
        async: false
        }).responseText;
}

</script>

<input type="text" name="Greeting" id="Greeting" onblur="sayHi()">

</body>
</html>
iJay
  • 4,205
  • 5
  • 35
  • 63

2 Answers2

2

exit your php for usage in same script, ajax will return entire html.

<?php
    if(isset($_POST['Greet']))
    {
        echo $_POST['Greet'];
        die;
    }
?>

value will have your response alert(value)

later edit:

<?php
    if(isset($_POST['Greet']))
    {
        echo $_POST['Greet'];
        die;
    }
?><html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
function sayHi()
{
    var value = $.ajax({
        type: "POST",
        url: "index.php",
        data: "Greet="+$("#Greeting").val(),
        async: false
    }).responseText;
    $('#result').html(value);
}

</script>
<span id="result"></span>
<input type="text" name="Greeting" id="Greeting" onblur="sayHi()">
</body>
</html>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

You should include the jQuery script to your page to make $.ajax work.

Like this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
OldEr
  • 51
  • 2
  • Thanks http://stackoverflow.com/users/1600892/older ! I missed it, but problem still the same,:isset($_POST['Greet']) is false. – iJay Aug 16 '12 at 11:01