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?

arghtype
  • 4,376
  • 11
  • 45
  • 60
DragonFire
  • 3,722
  • 2
  • 38
  • 51
  • 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. – DragonFire Jan 30 '17 at 09:49
  • your code is vulnerable to sql injection. use prepared statments as they are intended to use instead and consider the following lecture: https://laurent22.github.io/so-injections/ – Joshua Jan 30 '17 at 09:58
  • @Joshua - do I need to even prepare the select statements, or only the insert, update and delete statements – DragonFire Jan 30 '17 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: http://stackoverflow.com/questions/15758185/php-mysql-injection-example You should consider reading the linked question there too – Joshua Jan 30 '17 at 10:17

3 Answers3

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']; ?>
Kaushik solanki
  • 438
  • 3
  • 15
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']; ?>
Furqan Aziz
  • 1,094
  • 9
  • 18
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.

DragonFire
  • 3,722
  • 2
  • 38
  • 51