1

I need a better solution then this, I'm using <span id="<?php echo $row['art_id']" name="<?php echo $row['art_featured']"></span> to get the values from fields from my database so I could send them through jQuery Ajax, but there has to be a better way to use the values from my database and storing them in a jQuery VAR then this, any suggestions would be great thanks in advance!

jQuery

        $(document).ready(function(){

    $(".star").click(function(){

        var art_id = $(this).attr('id');
        var art_featured = $(this).attr('name');

        $.ajax({
        type: "POST",
        data: {art_id:art_id,art_featured:art_featured},
        url: "ajax-feature.php",
        success: function(data){
            if(data != false) {

            } 
            else {

            }  
        }
        });

    });

});

PHP

    <section class="row">
<?php
$sql_categories = "SELECT art_id, art_featured FROM app_articles"; 

    if($result = query($sql_categories)){
        $list = array();

        while($data = mysqli_fetch_assoc($result)){
            array_push($list, $data);
        }

        foreach($list as $i => $row){ 
        ?>
            <div class="row">
                <div class="column one">
                     <span id="<?php echo $row['art_id']; ?>" class="icon-small star"></span>
                </div>
            </div>
        <?php
        }
    }
    else {
        echo "FAIL";
    }
?>
</section>

So would I be able to use the data-object thing with the ajax data: ?

3
  • You could try inputs of type "hidden" Commented Mar 26, 2013 at 2:00
  • Is <span/> a list of buttons? Commented Mar 26, 2013 at 2:05
  • @MarCejas its in a div <div class="column one"><span id="<?php echo $row['art_id']; ?>" name="<?php echo $row['art_featured'];?>" class="icon-small star"></span></div> Commented Mar 26, 2013 at 2:09

1 Answer 1

1

I prefer, using something like this:

<span 
     data-object='{"art_id":<?=$row['art_id']?>,
                  "art_featured":"<?=$row['art_featured']?>"}'
     onclick="ajaxFunction($(this))"
>
</span>

function ajaxFunction(o){
      var data = o.data('object');
      /* you can send:
       * data.art_id
       * data.art_featured
       */
}

http://jsfiddle.net/M4AFQ/1/

Update for follow-up question:

$(".star").click(function(){
      var data = $(this).data('object');
      /* you can send:
       * data.art_id
       * data.art_featured
       */
  });
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, thats very interesting but I'm getting o.data is not a function
Please check out the jsfiddle link.
Ok, don't hesitate to ask if you have questions.
Sorry just one question, is it possible to use jQuery .click() with this so I can just target a class instead on having the onclick="" ?
Thank you very much, your my savor ahah!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.