SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to create a confirm box upon clicking a link

    hi,

    Im not too hot on javascript ( ive just ordered DOM scripting by jeremy keith to help remedy this).

    I usually use this kind of thing to do a confirm on a submit button:

    PHP Code:
    <input type="submit" name="" value="" onclick="javascript: if(confirm('Are you sure you want to so this?')) { return true; } return false;" 
    Now this works fine...i have no idea if its good practice or not ( probably better to seperate the javascript from the html) but it works!

    Now i have this problem.....i need to do exactly the same thing but this time its not a submit button; Its a link. IM doing this:

    PHP Code:
    <a href="entrylist.php?<?php echo $_SERVER['QUERY_STRING']; ?>&blogdelete_id=<?php echo $row['blog_id']; ?>"><img src="delete.gif" /></a>
    So i want to run the javascript ive got above if the delete.gif is clicked upon ans if the user confirms i want to run this link so to speak!

    Any ideas javscript gods from above
    Last edited by elduderino; Mar 8, 2007 at 03:17.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even though your onclick attribute in your first example is less than ideal, if you copy and paste it into your <a> tag it will do exactly what you want.

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    err ive done this but it clearly isnt right because its not working...any ideas?

    PHP Code:
    <a onclick="javascript: if(confirm('Are you sure you want to so this?')) { return true; } return false;" href="entrylist.php?<?php echo $_SERVER['QUERY_STRING']; ?>&blogdelete_id=<?php echo $row['blog_id']; ?>"><img src="delete.gif" /></a>

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the double set of (...(...)) looks kind funny to me.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i dont know....i just copied that bit of code from my submit button. anyone got any ideas how to format this line?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This definitely works:
    Code:
    <a onclick="if(confirm('Are you sure you want to so this?')) { return true; } return false;" href="blah.html">Delete</a>
    The only substantial thing I did was take out the javascript: which you don't need and shouldnt use.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    This definitely works:
    Code:
    <a onclick="if(confirm('Are you sure you want to so this?')) { return true; } return false;" href="blah.html">Delete</a>
    The only substantial thing I did was take out the javascript: which you don't need and shouldnt use.
    Yup, javascript: is a scheme like http: or ftp: or (albeit out of date) gopher:, to be used in URIs

    Though, href="javascript:alert('asd');" is an old, poor way of calling JS, but people still use it.

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice one jimfraser...that works a treat.

    Thankyou


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •