JavaScript confirm popup not working - javascript

I have a php file which is part of a basic Content Management System. Inside this file I have a table which contains a "Delete" link in every row.
What I am trying to do is create a choice popup in a javascript function of which I call when the link is clicked.
My function code (which appears in the head part of my php file) is below:
<script type="text/javascript">
function delete(skillID)
{
var answer = confirm("Are you sure you want to delete this record?");
if(answer == true)
{
window.location.href = "process/deleteRecord.php?skillID=" + skillID;
}
}
The code which calls this function is below:
echo "<td>Delete</td>";
The problem is that the box doesn't even popup, so the function isn't called properly.
Can anyone help?
If there are any other details you need to know please let me know.

You can try something else :
echo "<td>Delete</td>";

You need to use the "onClick" attribute, try that :
echo "<td>Delete</td>";
Hope it'll help!

Related

Is it possible to include a javascript function call into php?

I am stuck with this issue. I have a js function into a scripts.js file:
var userLoggedIn;
function openPage(url) {
if(url.indexOf("?") == -1) {
url = url + "?";
}
var encodedUrl = encodeURI(url + "&userLoggedIn=" + userLoggedIn);
$('#mainContent').load(encodedUrl);
}
I call the javascript function from a php file like this:
<?php
//This file should check if the requested url is sended by AJAX or it's manaually typed by the user into the browser
if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
include 'includes/config.php';
include 'classes/Artist.php';
include 'classes/Album.php';
include 'classes/Song.php';
} else {
include 'includes/header.php';
include 'includes/footer.php';
$url = $_SERVER['REQUEST_URI'];
echo "<script>openPage('$url')</script>";
exit();
}
?>
It works fine when it's called from an onclick event into the HTML document, but it doesn't works when I enter manually the url. It prints a string into the screen. However this code is taken from some sample coding pages and it seems to work this way. What am I missing here?
Thanks in advance for the help and be patient... i'm a beginner on this!!
First of all you need to have the mentioned above js code loaded somewhere. Do you have it loaded in header or footer? (Directly or by loading your scripts.js?)
Be sure to have:
<script type="text/javascript" src="scripts.js">
Somewhere printed in your code. Do you have it in your footer or header?
Assuming that you have your js code in scripts.js now you can execute after page loads, but be sure print the <script> part WITHIN your <html>...</html>. So if you print out </html> in your footer.php then be sure to print:
echo "<script type="text/javascript">openPage('$url')</script>";
BEFORE loading of footer (but after loading of scripts.js);
While you learn a bit more you will get to know that actually it is a bad thing to execute code parts like that: you should separate your markup from code which executes things directly from it.
Modern frameworks will assist you with that, there is also defer attribute of the script element (https://www.w3schools.com/tags/att_script_defer.asp) which helps to execute the code after the page actually loads.
So, after reviewing all the code i found the problem. There was a missing ">" in the html closing tab. Now it works!! Thank you for your help!

Calling JS function from html/php file without button

I'm trying to call my JS function which is in a file called Scripts.js. I have a php function, but I'm unsure as to whether I need to come out the php function or not as I can't get it to work either way and nothing I've googled is giving a definitive answer as to which it should be so any help would be appreciated!
<?php if(isset($_REQUEST['renameFile']))
{
//uses checkboxes from table
if(isset($_POST['checkbox']))
{
$checkedboxes = $_POST['checkbox'];
$count = count($checkedboxes);
//echo ("$count");
/*if($count == 0)
{
throw error that something needs to be selected. If implement checkbox hidden then no need to implement
}*/
if($count == 1)
{?>
<script type="text/javascript">
showUpload();
</script>
<?php
}
?>
So I'm calling the function showUpload() in my Scripts.js file which I have defined in my main screen using
<script src ="Scripts.js"> </script>
Which I know works as I've used it elsewhere.
function showUpload()
{
document.getElementById('fileUpload').style.display = "block";
}
And then this goes on to call a pop up form which I can get to work on a button click so it's just calling it without a button which seems to be an issue. Thanks!
check following things.
include your js file at the top of the file
write the script tag out side the php mode.
make sure the if conditions are true/false as you expect
then the code should work fine

Execute a PHP code inside jQuery

That's what I have:
LinkName
Here <?=$arItem["LINK"]?> I'm getting some link, and I want to add to the end of this link some extra hashtag parameter #nav-link.
But the main problem is that PHP is executing on the server side, so, when I click the link, I get only link from the <?=$arItem["LINK"]?> (blabla.com). After second click, I have necessary code blabla.com/#nav-link.
So, is it possible to get blabla.com/#nav-link after first click?
Try something to the tune of this:
<script>
var data = <?php echo $arItem["LINK"]; ?>
var yourLink = "blabla.com/"+data;
//more of your code etc....
$('#yourDiv_where_you_want_the_link').html(yourLink);
</script>

Pass data into JQuery then HTML (multi-step)

I am trying to allow a global function to be called and pass text into an alert box, which is set at the top of the page.
My code is:
<script>
$(document).ready(function () {
$('.fadehelper').fadeIn('slow');
function msg(data, type) {
$('.warning').slideDown("slow").delay(1500).slideUp("slow");
}
});
</script>
And the message box is:
<div class="warning">Help me!</div>
I am not really sure how to do this..
I want to pass a type and a message. The class will be the type, and the message will do where the "Warning" goes.
So if I go somewhere in the page and go msg(warning, "help me!"); I want that to be translated like above.
Can you help? Thank you.
First of all, put an id attribute on the div so that it can be found easily. I will assume you are using alertbox as the id in the following function
Add the following to the end of the msg function:
var box=document.getElementById('alertbox');
box.setAttribute('class',type);
box.innerHTML=data;
It can probably be done shorter in jquery but I don't use jquery
function msg(type, data){
var box=document.getElementById('alertbox');
box.setAttribute('class',type);
box.innerHTML=data;
$('#alertbox').slideDown("slow").delay(1500).slideUp("slow");
}
Got it!

Delete Comment popup

I'm wanting to popup a confirm/cancel div in which I can style when my users click the delete button in my feed. But what I have isn't working. And I'm wondering if its because I don't have anything in delete.php to tell it to direct it into a popup. It just goes to the delete.php page atm. Could someone give me some direction please?
Javascript:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/JavaScript">
function confirmDelete(){
var agree=confirm("Are you sure you want to delete this file?");
if (agree)
return true ;
else
return false ;
}
</script>
Button:
echo 'X ';
You need to return the value returned by confirmDelete.
The attribute is onclick, not onClick.
The attribute is missing a closing quote.
JavaScript
function confirmDelete(){
return confirm("Are you sure you want to delete this file?");
}
PHP
echo '<a ... onclick="return confirmDelete();" ...';
That said, it looks like you're using jQuery (albeit a super old version). If that's really the case, use jQuery and start writing unobtrusive JavaScript already.
$(function ()
{
$('a.delete').click(confirmDelete);
});
Get yourself a reasonably recent version of jQuery as well.
This doens't regards jQuery but anyway you could do in this way:
<script>
function confirmDelete(idPassed){
if (confirm("Are you sure you want to delete this file?"))
window.location = "mysite/raw/sn-extend/theme/default/delete.php?id=" + idPassed;
}
</script>
X

Categories