5

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!

13
  • your mixing php and js up. one is server side, the other is client side Commented Aug 6, 2013 at 21:54
  • Could post a live link, or a link to a fiddle (jsfiddle.net) that shows the rendered code -- with anything sensitive bleeped out, or the rendered code copied from View Source. I personally can't visualize the whole picture this way. Commented Aug 6, 2013 at 22:03
  • 1
    Your js is broken @RodApernum. Check answer below .. see if it helps :) Commented Aug 6, 2013 at 22:12
  • 2
    Everytime I see usage of regular mysql_ extension, I kill a kitten. Commented Aug 6, 2013 at 22:22
  • 1
    I know you have to, but bad education hurts, I know how hard it was for me to get rid of plain old mysql_, here's a basic reference for better mysql ;) stackoverflow.com/tags/pdo/info Commented Aug 6, 2013 at 22:29

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

15 Comments

@ChuckUgwuh one is with a linebreak and the other is not. In javascript semicolon is optional. So its assuming a ; at the end and breaking the function.
@woofmeow Please don't delete the answer
@woofmeow Lol. At 132 rep I don't think you can afford downvotes so I would reluctantly say you should delete it, even though your tip might be helpful to someone in an unrelated way #ToughBreak. I actually find the process of folks trying to help find an answer almost as useful as the answer itself
@RodApernum your welcome buddy :). Btw I am still confused about your $delete_user_script that has a breakline too in it !!
To be fair, I edited your question to make it more readable. it did not occur to me that your code could break due to word wrap.
|
0

Try this instead of what you have now in your script:

$delete_user_script = "".
"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;
    }
 }";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.