0

I am trying to pass variables to jquery for ajax from html/php.

This is the html/php:

<?php
    $propertyid = $data['property_id'];
    $select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
    $select5->setFetchMode(PDO::FETCH_ASSOC);
    $select5->execute();

    while($data5=$select5->fetch()){
        echo $data5['favorite_properties_id'];
        $favorite_properties_id = $data5['favorite_properties_id'];
    }
?>

<a href="#">
    <img class="addtofavoritebutton" pid="<?php echo $propertyid; ?>" fpid="<?php echo $favorite_properties_id;?>" src="../images/system/addtofavorite.png">
</a>
<?php echo $data['property_id']; ?>

This is the jquery:

$('.addtofavoritebutton').click(function() {    
    var property_id = $(this).attr('pid');
    var favorite_properties_id = $(this).attr('fpid');

    alert(property_id);
    alert(favorite_properties_id);      
});

There are two rows in the tbl_favorite_properties, table however jquery is picking up the last row for fpid, even when there is no data. When I echo in php then the value is blank (as it should be) but in jquery it is taking the last row and repeating the value, when it can't find. How can I get jquery to pick up the blank or null value just like php?

4
  • echo $data5['favorite_properties_id']; is giving the right value - in php but var favorite_properties_id = $(this).attr('fpid'); in jquery is repeating the last value when it should also pick up blank value. Commented Jan 30, 2017 at 9:49
  • your code is vulnerable to sql injection. use prepared statments as they are intended to use instead and consider the following lecture: laurent22.github.io/so-injections Commented Jan 30, 2017 at 9:58
  • @Joshua - do I need to even prepare the select statements, or only the insert, update and delete statements Commented Jan 30, 2017 at 10:12
  • you should prepare all statements and use the bindParam methods of PDOStatement to set values on the queries. A good read is this question: stackoverflow.com/questions/15758185/… You should consider reading the linked question there too Commented Jan 30, 2017 at 10:17

3 Answers 3

0

Actually you are using $favorite_properties_id after finishing the while loop and variable $favorite_properties_id will contain the value of the last iteration of while loop.

<?php
    $propertyid = $data['property_id'];
    $select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
    $select5->setFetchMode(PDO::FETCH_ASSOC);
    $select5->execute();

    $favorite_properties_id = null;
    while($data5=$select5->fetch()){
        echo $data5['favorite_properties_id'];
        $favorite_properties_id = $data5['favorite_properties_id'];
    }
    ?>

    <a href="#"><img class="addtofavoritebutton" pid="<?php echo $propertyid; ?>" fpid="<?php echo $favorite_properties_id;?>" src="../images/system/addtofavorite.png"></a>

    <?php echo $data['property_id']; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

If i do that then the image disappears
I have edited code, You can check it now please. I think you need to reset $favorite_properties_id = null; before loop
0
<?php
                $propertyid = $data['property_id'];
                $select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
                $select5->setFetchMode(PDO::FETCH_ASSOC);
                $select5->execute();
                ?>

                <a href="#"><img class="addtofavoritebutton" pid="<?php echo $propertyid; ?>" fpid="<?php while($data5=$select5->fetch()){echo $data5['favorite_properties_id'];} ?>" src="../images/system/addtofavorite.png"></a>

This is working now.

1 Comment

It was the problem with loop only, thanks guys for pointing it out.
0

PLease put your tag inside loop

<?php
$propertyid = $data['property_id'];
$select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
$select5->setFetchMode(PDO::FETCH_ASSOC);
$select5->execute();

while($data5=$select5->fetch()) {
    echo $data5['favorite_properties_id'];
    $favorite_properties_id = $data5['favorite_properties_id'];
    echo '<a href="#"><img class="addtofavoritebutton" pid="'.$propertyid.'" fpid="'.$favorite_properties_id.'" src="../images/system/addtofavorite.png"></a>';
}
?>
<?php echo $data['property_id']; ?>

2 Comments

If i do that then the image disappears
can u please explain what exactly you want?

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.