Echo PHP Query results in javascript confirm











up vote
0
down vote

favorite












I am building a visitor management system. On the admin back end I am trying to add the ability for manual sign-outs. I have visitor cards that show the visitor information along with a button to view their profile and a button which will sign them out. - https://ibb.co/f7y5yV



I have done a PHP foreach to pull the visitor's data from the database and then display them in their own card. This is the code for the cards



<?php foreach ($result as $row) : ?>

<div class="visitor-tile">
<div class="visitor-tile-container">
<div class="my-photo-container">
<img src="../images/visitor-photos/<?php echo htmlspecialchars($row['id']);?>-<?php echo htmlspecialchars($row['image_path']);?>" class="profile-img img-circle center-block"></td>
</div>
<h2 class="profile-visitorname">
<?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>
</h2>
<p class="profile-label">Vehicle Reg: <span class="profile-data"><?php echo htmlspecialchars($row['vehicle_reg']) ?></span></p>
<p class="profile-label">Visiting: <span class="profile-data"><?php echo htmlspecialchars($row['visiting']);?></span></p>
<p class="profile-label">Arrival Time: <span class="profile-data"><?php echo substr(htmlspecialchars($row['sign_in_time']), 11, -3);?></span></p>
</div>
<div class="visitor-tile-buttons">
<a href="visitor_profile.php?id=<?php echo htmlspecialchars($row['person_id']);?>" class="view-profile"><i class="fas fa-user"></i></a>
<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>
</div>

</div>
<?php endforeach ?>


For the sign out button I am trying to do a confirm in javascript to ask the user if they are sure they want to sign the visitor out, but want to pull the visitor's name from the result and put it into the confirm message. I have tried to do this here but it only appears to pull the first result from the query for all of the visitor cards



<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>


I think I need to push the result to the JS somehow but am not entirely sure.










share|improve this question


















  • 1




    Your code will create multiple definitions of confirm_delete which isn't going to work. Have one function and pass the name in from e.g. a data- attribute on a tag.
    – Jon Stirling
    Nov 9 at 11:30















up vote
0
down vote

favorite












I am building a visitor management system. On the admin back end I am trying to add the ability for manual sign-outs. I have visitor cards that show the visitor information along with a button to view their profile and a button which will sign them out. - https://ibb.co/f7y5yV



I have done a PHP foreach to pull the visitor's data from the database and then display them in their own card. This is the code for the cards



<?php foreach ($result as $row) : ?>

<div class="visitor-tile">
<div class="visitor-tile-container">
<div class="my-photo-container">
<img src="../images/visitor-photos/<?php echo htmlspecialchars($row['id']);?>-<?php echo htmlspecialchars($row['image_path']);?>" class="profile-img img-circle center-block"></td>
</div>
<h2 class="profile-visitorname">
<?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>
</h2>
<p class="profile-label">Vehicle Reg: <span class="profile-data"><?php echo htmlspecialchars($row['vehicle_reg']) ?></span></p>
<p class="profile-label">Visiting: <span class="profile-data"><?php echo htmlspecialchars($row['visiting']);?></span></p>
<p class="profile-label">Arrival Time: <span class="profile-data"><?php echo substr(htmlspecialchars($row['sign_in_time']), 11, -3);?></span></p>
</div>
<div class="visitor-tile-buttons">
<a href="visitor_profile.php?id=<?php echo htmlspecialchars($row['person_id']);?>" class="view-profile"><i class="fas fa-user"></i></a>
<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>
</div>

</div>
<?php endforeach ?>


For the sign out button I am trying to do a confirm in javascript to ask the user if they are sure they want to sign the visitor out, but want to pull the visitor's name from the result and put it into the confirm message. I have tried to do this here but it only appears to pull the first result from the query for all of the visitor cards



<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>


I think I need to push the result to the JS somehow but am not entirely sure.










share|improve this question


















  • 1




    Your code will create multiple definitions of confirm_delete which isn't going to work. Have one function and pass the name in from e.g. a data- attribute on a tag.
    – Jon Stirling
    Nov 9 at 11:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am building a visitor management system. On the admin back end I am trying to add the ability for manual sign-outs. I have visitor cards that show the visitor information along with a button to view their profile and a button which will sign them out. - https://ibb.co/f7y5yV



I have done a PHP foreach to pull the visitor's data from the database and then display them in their own card. This is the code for the cards



<?php foreach ($result as $row) : ?>

<div class="visitor-tile">
<div class="visitor-tile-container">
<div class="my-photo-container">
<img src="../images/visitor-photos/<?php echo htmlspecialchars($row['id']);?>-<?php echo htmlspecialchars($row['image_path']);?>" class="profile-img img-circle center-block"></td>
</div>
<h2 class="profile-visitorname">
<?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>
</h2>
<p class="profile-label">Vehicle Reg: <span class="profile-data"><?php echo htmlspecialchars($row['vehicle_reg']) ?></span></p>
<p class="profile-label">Visiting: <span class="profile-data"><?php echo htmlspecialchars($row['visiting']);?></span></p>
<p class="profile-label">Arrival Time: <span class="profile-data"><?php echo substr(htmlspecialchars($row['sign_in_time']), 11, -3);?></span></p>
</div>
<div class="visitor-tile-buttons">
<a href="visitor_profile.php?id=<?php echo htmlspecialchars($row['person_id']);?>" class="view-profile"><i class="fas fa-user"></i></a>
<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>
</div>

</div>
<?php endforeach ?>


For the sign out button I am trying to do a confirm in javascript to ask the user if they are sure they want to sign the visitor out, but want to pull the visitor's name from the result and put it into the confirm message. I have tried to do this here but it only appears to pull the first result from the query for all of the visitor cards



<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>


I think I need to push the result to the JS somehow but am not entirely sure.










share|improve this question













I am building a visitor management system. On the admin back end I am trying to add the ability for manual sign-outs. I have visitor cards that show the visitor information along with a button to view their profile and a button which will sign them out. - https://ibb.co/f7y5yV



I have done a PHP foreach to pull the visitor's data from the database and then display them in their own card. This is the code for the cards



<?php foreach ($result as $row) : ?>

<div class="visitor-tile">
<div class="visitor-tile-container">
<div class="my-photo-container">
<img src="../images/visitor-photos/<?php echo htmlspecialchars($row['id']);?>-<?php echo htmlspecialchars($row['image_path']);?>" class="profile-img img-circle center-block"></td>
</div>
<h2 class="profile-visitorname">
<?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>
</h2>
<p class="profile-label">Vehicle Reg: <span class="profile-data"><?php echo htmlspecialchars($row['vehicle_reg']) ?></span></p>
<p class="profile-label">Visiting: <span class="profile-data"><?php echo htmlspecialchars($row['visiting']);?></span></p>
<p class="profile-label">Arrival Time: <span class="profile-data"><?php echo substr(htmlspecialchars($row['sign_in_time']), 11, -3);?></span></p>
</div>
<div class="visitor-tile-buttons">
<a href="visitor_profile.php?id=<?php echo htmlspecialchars($row['person_id']);?>" class="view-profile"><i class="fas fa-user"></i></a>
<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>
</div>

</div>
<?php endforeach ?>


For the sign out button I am trying to do a confirm in javascript to ask the user if they are sure they want to sign the visitor out, but want to pull the visitor's name from the result and put it into the confirm message. I have tried to do this here but it only appears to pull the first result from the query for all of the visitor cards



<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete()"><i class="fas fa-sign-out-alt"></i></a>
<script type="text/javascript">
function confirm_delete() {
return confirm('Are you sure you want to sign out <?php echo htmlspecialchars($row['first_name']) ?> <?php echo htmlspecialchars($row['last_name']);?>');
}
</script>


I think I need to push the result to the JS somehow but am not entirely sure.







javascript php html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 11:23









Rhys Williams

84




84








  • 1




    Your code will create multiple definitions of confirm_delete which isn't going to work. Have one function and pass the name in from e.g. a data- attribute on a tag.
    – Jon Stirling
    Nov 9 at 11:30














  • 1




    Your code will create multiple definitions of confirm_delete which isn't going to work. Have one function and pass the name in from e.g. a data- attribute on a tag.
    – Jon Stirling
    Nov 9 at 11:30








1




1




Your code will create multiple definitions of confirm_delete which isn't going to work. Have one function and pass the name in from e.g. a data- attribute on a tag.
– Jon Stirling
Nov 9 at 11:30




Your code will create multiple definitions of confirm_delete which isn't going to work. Have one function and pass the name in from e.g. a data- attribute on a tag.
– Jon Stirling
Nov 9 at 11:30












1 Answer
1






active

oldest

votes

















up vote
0
down vote













This is happening because you are making your <script> inside the PHP foreach loop - so you get one script for each user. And since the script defines one function, which is the same in each function, the functions are over-writing each other.



You should just make one <script> tag, outside of the loop. And in order to get it to show the correct details for the user clicked on, you can pass in parameters, like this.



Eg. the function can be:



    function confirm_delete(firstName, lastName) {
return confirm('Are you sure you want to sign out ' + firstName + ' lastName;?>');
}


And define your link like this in the loop:



<a href="#" class="sign-out" id="myLink" onclick="return confirm_delete(<?php echo htmlspecialchars($row['first_name']);?>, <?php echo htmlspecialchars($row['last_name']);?>)"><i class="fas fa-sign-out-alt"></i></a>


Or use data attributes as mentioned by @JonStirling - this would lead to neater code.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224810%2fecho-php-query-results-in-javascript-confirm%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This is happening because you are making your <script> inside the PHP foreach loop - so you get one script for each user. And since the script defines one function, which is the same in each function, the functions are over-writing each other.



    You should just make one <script> tag, outside of the loop. And in order to get it to show the correct details for the user clicked on, you can pass in parameters, like this.



    Eg. the function can be:



        function confirm_delete(firstName, lastName) {
    return confirm('Are you sure you want to sign out ' + firstName + ' lastName;?>');
    }


    And define your link like this in the loop:



    <a href="#" class="sign-out" id="myLink" onclick="return confirm_delete(<?php echo htmlspecialchars($row['first_name']);?>, <?php echo htmlspecialchars($row['last_name']);?>)"><i class="fas fa-sign-out-alt"></i></a>


    Or use data attributes as mentioned by @JonStirling - this would lead to neater code.






    share|improve this answer

























      up vote
      0
      down vote













      This is happening because you are making your <script> inside the PHP foreach loop - so you get one script for each user. And since the script defines one function, which is the same in each function, the functions are over-writing each other.



      You should just make one <script> tag, outside of the loop. And in order to get it to show the correct details for the user clicked on, you can pass in parameters, like this.



      Eg. the function can be:



          function confirm_delete(firstName, lastName) {
      return confirm('Are you sure you want to sign out ' + firstName + ' lastName;?>');
      }


      And define your link like this in the loop:



      <a href="#" class="sign-out" id="myLink" onclick="return confirm_delete(<?php echo htmlspecialchars($row['first_name']);?>, <?php echo htmlspecialchars($row['last_name']);?>)"><i class="fas fa-sign-out-alt"></i></a>


      Or use data attributes as mentioned by @JonStirling - this would lead to neater code.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is happening because you are making your <script> inside the PHP foreach loop - so you get one script for each user. And since the script defines one function, which is the same in each function, the functions are over-writing each other.



        You should just make one <script> tag, outside of the loop. And in order to get it to show the correct details for the user clicked on, you can pass in parameters, like this.



        Eg. the function can be:



            function confirm_delete(firstName, lastName) {
        return confirm('Are you sure you want to sign out ' + firstName + ' lastName;?>');
        }


        And define your link like this in the loop:



        <a href="#" class="sign-out" id="myLink" onclick="return confirm_delete(<?php echo htmlspecialchars($row['first_name']);?>, <?php echo htmlspecialchars($row['last_name']);?>)"><i class="fas fa-sign-out-alt"></i></a>


        Or use data attributes as mentioned by @JonStirling - this would lead to neater code.






        share|improve this answer












        This is happening because you are making your <script> inside the PHP foreach loop - so you get one script for each user. And since the script defines one function, which is the same in each function, the functions are over-writing each other.



        You should just make one <script> tag, outside of the loop. And in order to get it to show the correct details for the user clicked on, you can pass in parameters, like this.



        Eg. the function can be:



            function confirm_delete(firstName, lastName) {
        return confirm('Are you sure you want to sign out ' + firstName + ' lastName;?>');
        }


        And define your link like this in the loop:



        <a href="#" class="sign-out" id="myLink" onclick="return confirm_delete(<?php echo htmlspecialchars($row['first_name']);?>, <?php echo htmlspecialchars($row['last_name']);?>)"><i class="fas fa-sign-out-alt"></i></a>


        Or use data attributes as mentioned by @JonStirling - this would lead to neater code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 11:32









        Robin Zigmond

        1,717410




        1,717410






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224810%2fecho-php-query-results-in-javascript-confirm%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud