Monday, May 21, 2012

jquery button using $.post()

I'm trying to use jquery to create a vote up, vote down button for my website. What i did was creating a form and have it processed in an external php script using $.post(). I thought this will allow the form to be processed in the quietly in the backend but when i click on the buttons, the jquery function doesn't seem to work and it just loads the whole script in the front-end.



Appreciate if anyone can provide some help here. Thanks. Following is my scripts.



Main PHP script



<div class="voting_class">

<?php
$user =& JFactory::getUser();
$ip = getenv('REMOTE_ADDR');
if($user->id == 185){
?>

<form method="post" action="../vote_plus.php">
<div class="form_element">
<label>user_id</label>
<input type="text" name="user_id" class="votetext" value="<?php echo $user->id;?>" />
</div>
<div class="element">
<label>ad_id</label>
<input type="text" name="ad_id" class="votetext" value="<?php echo $this->content->id;?>"/>
</div>
<div class="element">
<label>ip_address</label>
<input type="text" name="ip_address" class="votetext" value="<?php echo $ip;?>"/>
</div>
<div class="submit_element">
<input type="submit" class="button" id="submitadd" value="Recommend"/>
<div class="loading"></div>
</div>
<div class="submit_element">
<input type="submit" class="button" id="submitminus" value="Don't Recommend" />
<div class="loading"></div>
</div>
</form>
<p class="success_msg" style="display:none"></p>

<?php }?>

</div>


JAVASCRIPT SCRIPT



<script type = "text/javascript">
$(document).ready(function() {

$('#submitadd').click(function(event) {

//Get the data from all the fields
var user_id = $('input[name=user_id]');
var ad_id = $('input[name=ad_id]');
var ip_address = $('input[name=ip_address]');

$.post('../vote_plus.php', { user: "user_id.val()", ad: "ad_id.val()", ip: "ip_address.val()" }, function () {
$('.success_msg').append("Vote Successfully Recorded");
$('.success_msg').fadeOut();
});
event.preventDefault();
});
});
//if submit button is clicked
$('#submitminus').click(function(event) {

//Get the data from all the fields
var user_id = $('input[name=user_id]');
var ad_id = $('input[name=ad_id]');
var ip_address = $('input[name=ip_address]');

$.post('../vote_minus.php', { user: "user_id.val()", ad: "ad_id.val()", ip: "ip_address.val()" }, function () {
$('.success_msg').append("Vote Successfully Recorded");
$('.success_msg').fadeOut();
});
event.preventDefault();
});

});
</script>


I won't be posting the external script as i think the problem doesn't lie in the external php script as all mySQL updates can be loaded and properly and any info can be properly displayed. My problem lies where i can't get the button to work in jquery.





No comments:

Post a Comment