jquery button using $.post()

11.5k Views Asked by At

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").fadeIn();
        });
        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").fadeIn();
        });
        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.

2

There are 2 best solutions below

2
On

Your line

$.post('../vote_plus.php', { user: "user_id.val()", ad: "ad_id.val()", ip: "ip_address.val()" }, function(){

should be

$.post('../vote_plus.php', { user: user_id.val(), ad: ad_id.val(), ip: ip_address.val() }, function(){

Here's a streamlined version of your code.

$(function () {
  $('#submitadd').click(function (event) {       
    $.post(
      '../vote_plus.php', { 
        user: $('input[name=user_id]').val(), 
        ad:   $('input[name=ad_id]').val(),
        ip:   $('input[name=ip_address]').val() 
      }, function () {
        $('.success_msg').append("Vote Successfully Recorded").fadeOut();
      }
    );
    event.preventDefault();
  });
});

General tips:

  • It's not jQuery script, it's JavaScript.
  • Do not use comments that state the obvious, like //if submit button is clicked. They add no value.
  • Use proper, consistent indentation.
  • Use event.preventDefault(); instead of return false;
  • You can chain actions in jQuery, like .append() and .fadeOut(). No need to create two lines of code for that.
  • $(document).ready() can safely be replaced by $().
1
On

Use this instead:

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

        //if submit button is clicked
        $('#submitadd').click(function() {       

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

            $.post('../vote_plus.php', { user : user_id, ad : ad_id, ip : ip_address }, function(){
            $('.success_msg').append("Vote Successfully Recorded");
                $('.success_msg').fadeOut();
        });        
            return false;
         });
    });     
    </script>