SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    confirm box doesn't work with php...

    I created a function for an confirm box when someone clicks on the 'delete' link. But when i use php to echo the link, the box doesnt show up...

    Some1 help..

    HTML Code:
    <script type="text/javascript">
    function del()
    {
    confirm("Test")
    }
    </script>

    PHP Code:
    echo "<a href='deletemember.php?id=$mid' onclick='del()'>Delete</a>"

  2. #2
    SitePoint Enthusiast pento's Avatar
    Join Date
    Jan 2006
    Location
    Russia, Novosibirsk
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    popendot
    Are you sure JavaScript with function has included into script where you show link?
    GetDown->MakeLove();

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the deal: a browser has no idea whether a page it receives from the server ever had any php code in it.

    So, if you can write some html with the link hard coded in it, and you can write a javascript function that works when the link is clicked, then you can make php output that exact same html and javascript to produce the same effect.

    1) Therefore, the first thing that I suggest you do is write a minimal but complete html page that has a link you can click that calls a javascript function when it is clicked. There should be no php variables. The page should be pure html and javascript, and anyone should be able to take the html page you write, save it to their disk drive, and then open it in their browser and see that it works.

    2) Then write a php page that echo's exactly what is on the page in 1). Once again there should be no php variables--just echo statements. Anyone should be able to take that php page and put it in their root directory on their server and request the page with their browser and have the page work exactly as it did in 1).

    3) Then write a page that modifies the page in 2) by defining a php variable at the top of the page, e.g.

    $mid = 12345;

    Insert that php variable into the html in the appropriate place, and see if the page works as it did in 2).

    4) The final version: eliminate the php variable definition at the beginning of the code, and call the php page with a page that supplies the value for that variable or get the value of the php variable from your database.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by popendot
    I created a function for an confirm box when someone clicks on the 'delete' link. But when i use php to echo the link, the box doesnt show up...
    You're almost there (note "return" appearing twice):

    Code:
    <script type="text/javascript">
    function del()
    {
    return confirm("Test")
    }
    </script>
    
    <?php
    echo "<a href='deletemember.php?id=$mid' onclick='return del()'>Delete</a>";
    ?>
    or, simpler:
    Code:
    <a href="deletemember.php?id=<?=$mid?>" 
    onclick="return confirm('Sure?')">Delete</a>

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still isnt working...i don't know whats wrong...

    <script type="text/javascript">
    function del()
    {
    confirm("Test")
    }
    </script>
    PHP Code:
    echo "<a href='deletemember.php?id=$mid' onclick='return del()'>Delete</a>"
    anyone?

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post a complete html page that has a link you can click that calls your javascript function when it is clicked. There should be no php variables. The page should be pure html and javascript

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but the link is displayed as part of a 'while loop' in php...i cant make a separate html page...it has to be in an echo statement in the 'while loop'

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless you can write working javascript by itself, how do you expect to write javascript intermingled with php?

  9. #9
    SitePoint Enthusiast sarahG's Avatar
    Join Date
    Apr 2006
    Location
    Wirral, UK
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you've missed the 1st return in the javascript function. it should be

    return confirm("Test")

    and not just

    confirm ("Test")

    add that 'return' in and then see what happens


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
  •