SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript confirmation box not showing

    Hi all i am having a strange problem with my javascript ajax script and what i am wanting to do is before the content gets submitted to the server to delete data from a database i want the user to confirm it if yes then it will be confirmed if not then it wont be.

    this is my code

    Code HTML4Strict:
    ?>
            <script type="application/javascript">
    		function term()
    		{ 
    		   //show the confirmation box 
    		   var t=confirm('Are you sure you want to delete this account?');     
    		} 
    		</script>
    		<form method="post" action="">
            <table>
            	<input type="hidden" name="usr" value="<?php echo $user; ?>"/>
                <tr> 
                	<th> <input type="submit" name="sus" value="Terminate User" onclick="term()" ></th>
                </tr>
            </table>
            </form>
    		<?

    What am i doing wrong??

    My confirmation box doesnt show.

    Thanks,William

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can stop a form submission by returning false from the onsubmit event handler
    Code:
    myFormElement.onsubmit = function(){
        return confirm("hi");
    }

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is better to put onsubmit attribute for form element and true/false return from confirm will allow submission or block it. Something like this should work:
    Code:
    <form method="post" action="" onsubmit="return confirm('Are you sure you want to delete this account?');">
            <table>
                <input type="hidden" name="usr" value="<?php echo $user; ?>"/>
                <tr>
                    <th>
                         <input type="submit" name="sus" value="Terminate User" />
                    </th>
                </tr>
            </table>
            </form>
    you can make onsubmit call a separate function of course, as you had before if you need to put more code in there. But this gives you an idea.


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
  •