SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,380
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Jumping to another page through a Confirm dialog, with variable values

    Is it possible to pass along variables in a JS confirm dialog? Right now, the variables are passing literally instead of their variable values being passed. In this case, the $id variable is not being passed:

    Code:
    <input type='button' onclick='confirmation()' value='Delete'>
    At bottom of page, right above the closing </body> tag:

    Code:
    <script>
    <!--
    function confirmation() {
    	var answer = confirm("Delete the information? This action cannot be undone.")
    	if (answer){
    		window.location = "delete.php?id=' . $id . '";
    	}
    	else{
    		window.location = "stay-on-same-page.php";
    	}
    }
    </script>
    If I do it this way, with no alert, then the variables are passed just fine:

    Code:
    <a href='delete.php?id=" . $id . "'> Delete</a>
    I like to use the confirm dialog to make sure someone hasn't clicked on the delete function accidentaly.
    Steve Husting

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,438
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Assuming you have a button for each record, just pass the ID as an argument to your confirmation function like this:
    HTML Code:
    <input type='button' onclick='confirmation(103)' value='Delete'>
    and in your function, build the url like this:
    Code:
    function confirmation($id) {
        var answer = confirm("Delete the information? This action cannot be undone.")
        if (answer){
            window.location = "delete.php?id=" + $id;
        }
    }

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,380
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I assume
    onclick='confirmation(103)'

    should be
    onclick='confirmation($id)'
    ?

    If there is more than one variable, then I do
    onclick='confirmation($id, $var1, $var2)'
    ?

    Thanks a lot!
    Steve Husting

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,438
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Are you talking about using variables that you set in your PHP code? If you're passing values to JS functions, then you need to pass either the values themselves (i.e strings or numbers etc.) or variables that you've already assigned elsewhere in JS.

    Assuming that you're generating the links using PHP, then you'd be doing something like this:
    PHP Code:
    <input type='button' onclick='confirmation(<?php echo $id ?>)' value='Delete'>

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,380
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, I was talking about PHP here. Sorry that wasn't clear. Your last example makes more sense. I'm grateful!
    Steve Husting

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Just remember that confirm() is for debugging only and should be removed before your script goes live. You don't want your visitors disabling JavaScript for your page (the extra option that the confirm displays in some browsers) or disabling all fututre dialogs on your page (the extra option in most other browsers).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,380
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Felgall; good point there!

    This is for an Admin page at our company, so it should be fine.
    Steve Husting


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
  •