Forum Moderators: open

Message Too Old, No Replies

trouble in sending $post request

         

dulldull

2:30 pm on May 18, 2009 (gmt 0)

10+ Year Member



Hi all,

I'm running a simple ajax script to submit a form.

When submit button is clicked, loader.gif is loaded.

When php is executed, the result is supposed to show on the same page.

But when i run the code, something seems wrong with the #something wrong section below.

The ajax request doesn't work.

Can anyone please take a look on it?
A million and billion thank!

<head>
<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
$(function() {
//trigger ajax on submit
$('#phpForm').submit( function(){

//show the loading bar
$('.loader').append($('.bar'));
$('.bar').css({display:'block'});

// ***********[something wrong] send the ajax request***********
$.post('go.php',{name:$('#name').val()},

//return the data
function(data){

//hide the graphic
$('.bar').css({display:'none'});
$('.loader').append(data);
});

//stay on the page

return false;
});
});
</script>
</head>

<body>

<form id="phpForm" method="post" action="go.php">

<input id="name" name="search" value="testing" />

<input class="submit" type="submit" value="submit"/>

</form>

<br><br>
<div class="loader"></div>
<div class="bar"></div>

</body>
</html>

whoisgregg

7:07 pm on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not too familiar with jQuery syntax, but I'll do what I can to help. :)

First, are there any error messages? Have you tried running the page in Firefox with Firebug installed?

If it fails silently, I would start changing the code to see what may be affecting it. The first thing I'd do is hardcode a value into the parameters object:

{'name' : 'test'}

After that, I'd put in an alert to test if the function is returning anything at all:

function(data){
alert(data);

Hopefully one of those changes can illuminate what's wrong with the script. Post back with what you find. :)

dulldull

1:55 pm on May 19, 2009 (gmt 0)

10+ Year Member



Thanks super helpful whoisgregg!

I figured out the problem now. it's basically a syntax error.

Now I send my data in this way.

$.post("go.php", { name: $('#name').val() },

And when the data is returned, the data is shown in this way:
function(data){
process(data);
});

The documentation on this : [docs.jquery.com...]

whoisgregg you're so nice!

whoisgregg

2:28 pm on May 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you dulldull. Glad I could help. :)