Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP to load Javascript

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!

like image 864
Rod Apernum Avatar asked Oct 22 '22 03:10

Rod Apernum


1 Answers

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.

like image 154
woofmeow Avatar answered Oct 24 '22 04:10

woofmeow