jQuery form not being submitted. Firebug no errors
This jQuery isn't working. When i click Vote nothing happens. The div ratecontainer should disappear and nothing is inserted into MySQL when i check. In Firebug i get no errors. What did i do wrong?
echo "<div class='ratecontainer'>
<form id='rateform' action=''>
<input type='hidden' id='rateid' value='$stackid' />
<input type='hidden' id='type' value='1' />
<a cl开发者_如何学Pythonass='vote'>Vote</a>
</form>
</div>";
$(function() {
$('.vote').click(function() {
var rate= $("#rateid").val();
var type= $("#type").val();
var vote = "0";
var dataString = 'id=' + rate + '&type=' + type + '&v=' + vote;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "/rate.php",
data: dataString,
success: function() {
$('.ratecontainer').hide();
}
});
return false;
});
});
I'm assuming that your jquery code is inside
<script type="text/javascript">
</script>
?
I don't see it right away. But I know there is a really nice jQuery plugin to make this easier for you: http://jquery.malsup.com/form/
You should have rate.php do some debugging... for example print_r($_POST);
Your code should probably be like this:
<div class='ratecontainer'>
<form id='rateform' action=''>
<input type='hidden' id='rateid' value='<?php echo $stackid ?>' />
<input type='hidden' id='type' value='1' />
<a class='vote'>Vote</a>
</form>
</div>
<script>
$(function() {
$('.vote').click(function() {
var rate= $("#rateid").val();
var type= $("#type").val();
var vote = "0";
var dataString = 'id=' + rate + '&type=' + type + '&v=' + vote;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "/rate.php",
data: dataString,
success: function() {
$('.ratecontainer').hide();
}
});
return false;
});
});
</script>
And don't have that wrapped in a big <?php ?>
tag. Assuming your rate.php
is written correctly (which is a stretch) this should work. (jsFiddle)
精彩评论