EDIT: My function was broken, with the /n
in it. Thanks woofmeow for leading me to question my function!
I am new to programming. I have been trying my best and I have learned a lot the past few months (a bunch here!) but I am stumped.
I have a PHP script that calls from another PHP script and I cannot get the code to actually run. It used to run, then I changed some things and didn't save the changes, I'm not sure what I did. I know, I know, rookie mistake(I learned from it!). The Javascript shows up fine on view page source but doesn't run anymore.
Here is the page source, maybe it is that simple:
<script type="text/javascript">
function delete_user(user_id)
{if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!")) {window.location = "delete_user.php?user_id=" + user_id;}}</script>
Here is the PHP: The show_users script sends this into the viewer:
$delete_user_script = <<<EOD
function delete_user(user_id)
{
if (confirm("Are you sure you want to delete this user?"
+ "\nThere's really no going back!"))
{
window.location = "delete_user.php?user_id=" + user_id;
}
}
EOD;
The PHP in the HTML was NOT changed from when it was working:
<?php
while ($user = mysql_fetch_array($result))
{
$user_row = sprintf("<li><a href='show_user.php?user_id=%d'>%s %s</a>(<a href='mailto:%s'>%s</a>)<a href='javascript:delete_user(%d);'><img class='delete_user' src='../images/delete.png' width='15' /></a></li>",
$user['user_id'],
$user['first_name'],
$user['last_name'],
$user['email'],
$user['email'],
$user['user_id']);
echo $user_row;
}
?>
And finally, the viewing script that gives us our page source (Note: $embedded_javascript is $delete_user_script):
if (!is_null($embedded_javascript))
{
echo '<script type="text/javascript">' . $embedded_javascript . '</script>';
}
When I mouse over the image to delete the user, it still shows the correct script link ("Javascript:delete_user(%d)", where %d is the user_id) but it's like the function isn't defined anymore, nothing happens. Any ideas are greatly appreciated! Thanks!
Basically your if
statement is wrong (even in the $delete_user_script
variable). Since it is starting on a new line the interpreter will assume a ;
at the end of it and thus your if
statement breaks.
Your function has this
if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!"))
It should be this way
if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!"))
Sometimes its just a teeny weeny mistake. Hope that helps :)
EDIT 1: this was an error in the origianally posted question in the javascript and php code. Now its been edited to reflect that the if
is not broken.
EDIT 2: I have been told this helped to solve the problem so I'll keep this question here. Hope it helps others too.
PS: If someone wants this removed let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With