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.
javascript php html
add a comment |
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.
javascript php html
1
Your code will create multiple definitions ofconfirm_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
add a comment |
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.
javascript php html
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
javascript php html
asked Nov 9 at 11:23
Rhys Williams
84
84
1
Your code will create multiple definitions ofconfirm_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
add a comment |
1
Your code will create multiple definitions ofconfirm_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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 at 11:32
Robin Zigmond
1,717410
1,717410
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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