MEM
MEM

Reputation: 31397

basic quick ajax question with jquery

First try with ajax, please have patience.

<html>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function() {
        function hello() {
            $.post("testCaseAjaxServerSide.php", { ola: "hello"}, function(data){
                if (data) {
                    alert('the data is: '+data);
                }
                else {
                    alert('there were no data');
                }
            });
        }

        $("#inputAmigo").click(function(){
            hello();
        });
    });
    </script>

    <form method="post" action="">
        <input id="inputAmigo" type="submit"/>
    </form>
</html>

On the server side I have:

if (isset($_POST['ola']))
{
  echo "we got a post";
} 
else
{
  echo "no post buddy";
}

I'm always getting "there were no data". Any help please? :)

Thanks a lot, MEM

Upvotes: 0

Views: 90

Answers (1)

villecoder
villecoder

Reputation: 13494

Works for me if I prevent the submit button's default behavior. Try changing the click function to:

$("#inputAmigo").click(function(e){
    e.preventDefault();
    hello();
});

or change your form so that you're not using a submit button. Use a simple <input type='button'> instead.

You could also try using the jQuery form plugin.

Upvotes: 5

Related Questions