0

I encountered a problem. I have a list of restaurants, ids are named with number at the end strong_restName1 strong_restAddress1 strong_restName2 strong_restAddress2 ... At first I tried doing this href="?count=<?php echo $count] ?>" so I could get value with $_GET['count'] when I press on that <a> tag, I want to use element with that specific id. Basically, my code should work like this: there is list of restaurants, if I press on one of them, it is selected, and should now display that name and address as selected restaurant. I will put in some code of creating this list:

restaurantlist.php

    $count = 0;
while ($row = mysqli_fetch_assoc($result)) 
{
    
    if ($row['name'] != trim(urldecode($_COOKIE['restaurant_name'])) && $row['address'] != trim(urldecode($_COOKIE['restaurant_address'])))
    {
    $count++;
    $strongRestName = "strong_restName".$count;
    $strongRestAddress = "strong_restAddress".$count;
    
?>
    <li class="acc" style="padding: 0;margin: 0;max-width: 100%;word-break: break-all;">
        <a class="acc-link" href="" style="position: relative;display: block;width: 100%;padding-bottom: 10px;outline: none;text-decoration: none;padding-right: 0px;padding-left: 10px;padding-top: 10px;" onclick="myAjax()">
            <div class="acc-text" style="max-width: 100%;">
                <small class="form-text text-muted" style="margin: 0;font-size: 18px;color: rgb(0,0,0);line-height: 18px;">
                <strong id="<?php echo $strongRestName ?>" > <?php echo $row['name'] ?></strong></small>
            </div>
            <div>
                <small class="form-text text-muted" style="color: rgb(0,0,0);" id="<?php echo $strongRestAddress ?>">
                    <?php echo $row['address'] ?>
                </small>
            </div>
        </a>
    </li>
<?php     
        }
    }

?>

On every <a> tag there is onclick="myAjax()"

index.php

 <?php include('restaurantlist.php'); ?>
                    
                    
                <script>
                    function myAjax() {
                        var count = "<?php echo $_GET['count'] ?>";
                        var strongRestName = "#strong_restName" + count;
                        var strongRestAddress = "#strong_restAddress" + count;
                        alert($(strongRestName).text());
                          $.ajax({
                               type: "POST",
                               url: '/restaurants/ajax.php',
                               data:
                                {
                                     action:'cookies_change', 
                                     restName: $(strongRestName).text(), 
                                     restAddress: $(strongRestAddress).text()
                                },
                               success:function(html) {
                                   alert($('#strong_restName1').text());
                               }

                          });
                     }
                </script>

but the problem with $_GET['count'] that it won't read when pressing first time, I tested it out with alert(). It only captures value from $_GET when I press second time on a link. In ajax.php I just use variables sent from ajax and assign them to COOKIES, according to my goal.

  • When you do `var count = "";` you hardcode it to whatever `$_GET['count']` is at *page load*. It is not fetched dynamically *after you click*. But there is no need for this. Javascript has mechanisms to obtain an attribute of a clicked element or its parent/children/siblings. – El_Vanja Dec 01 '20 at 17:42
  • What would be better way to approach this? – Laimonas Rupeika Dec 01 '20 at 18:21
  • Start by reading [how to get id of clicked element](https://stackoverflow.com/questions/48239/getting-the-id-of-the-element-that-fired-an-event/40043918). Expand your search to parent/children/siblings per your needs. – El_Vanja Dec 01 '20 at 18:24

0 Answers0