...

View Full Version : Ajax request not working?



kernelgpf
01-20-2009, 09:59 PM
I've made a couple of these work, but it is so hard to bug test them!

My script:
<script type="text/javascript" src="prototype.js"></script>
<script>
function sendRequest() {

new Ajax.Request("ajaxshowsearch.php",
{
method: 'post',
postBody: 'Showtype='+$F(Showtype)+'&dog_id='+$F(dog_id),
onComplete: showResponse
});

}

function showResponse(req){
$('foo').innerHTML= req.responseText;
}
</script>


<form id=test action=shows.php? onsubmit="return false;">
Dog:
HERE;
$query=mysql_query("select name,dog_id,breed from dogs where player_id='$player_id' and age < 108 and train_complete=1 and weight >= 90 and weight <= 110 and trust >= 50");
if(mysql_num_rows($query) != "0"){
print "<select name=dog_id>";
while($row=mysql_fetch_array($query)){
print "<option value=$row[dog_id]>$row[name] (#$row[dog_id]), $row[breed]</option>";
}
print "</select><br>";
}
else{
print "No dogs eligible for showing.<br>";
}

print "<select name='Showtype[]' multiple>
<option>Flyball</option>
<option>Obedience</option>
<option>Agility</option>
<option>HTM</option>
</select><br>";
if(mysql_num_rows($query) != "0"){
print "<input type=submit name=submit value='Search Shows' onClick=sendRequest()>";
}
print "</form>";
?>


<div id="foo"></div>

Absolutely nothing happens when the form button is pressed. Help? TIA.

PS- most of my script is cut out, this is just the vitals. In "ajaxshowsearch.php" the word "test" resides, so I know the error is not there.

-Alyssa

ohgod
01-20-2009, 10:40 PM
as far as hard to debug, grab firebug for firefox... you can watch the whole transaction. or i guess the lack thereof...

kernelgpf
01-20-2009, 11:25 PM
Alright, I got Firebug. How do I use it?

kernelgpf
01-20-2009, 11:58 PM
I figured out how to use Firebug, it is saying "Showtype is not defined" though it looks like it is. Any suggestions?

ohgod
01-21-2009, 02:43 PM
for the record... this should probably be in the framework section since you're working with prototype.


change $F(Showtype) to $F('Showtype[]')

when using the $F utility method the element needs to be in single quotes (unless the element name is a variable passed as an arg or something). Also, you need the [] as it is part of the name.


And just another thought... if you know you're going to take all values from the form, then
$('yourform').serialize() (http://www.prototypejs.org/api/form/serialize)
would\could replace
'Showtype='+$F(Showtype)+'&dog_id='+$F(dog_id)

but for that matter, since you're using this to update another section you might just use Ajax.Updater (http://www.prototypejs.org/api/ajax/updater). it's very easy to use, and would take:

function sendRequest() {

new Ajax.Request("ajaxshowsearch.php",
{
method: 'post',
postBody: 'Showtype='+$F(Showtype)+'&dog_id='+$F(dog_id),
onComplete: showResponse
});

}

function showResponse(req){
$('foo').innerHTML= req.responseText;
}

and turn it into:

new Ajax.Updater( 'foo', 'ajaxshowsearch.php',
{ method: 'post', parameters: $('test').serialize()
} );



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum