Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using a parameter as part of the function

    Hi

    I have a javascript function that takes in the name of a form from the page and I want to call submit for this form, but I don't know how to use the parameter with that call.
    Code:
    function deleteEvent(item) {
        var answer = confirm ("Are you sure you want to delete this event?")
    	
        if (answer) {
            document.item.submit();  <-- i want to submit the form that goes by the name stored in item
        }
    }
    If my explanation makes sense (I'm really tired haha) then can anyone tell me if this is possible? I was originally submitting the form from a hyperlink and using PHP to put in the appropriate form name, but I want to have a confirm box first.

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You could use something like the following on your submit button or hyperlink:-

    Code:
    onclick = "if (deleteEvent) {this.form.submit()}" 
    
    function deleteEvent() {
    var answer = confirm ("Are you sure you want to delete this event?")
    if (!answer) { return false}
    }
    We are sorry that as a result of a typographical error we referred to the well-known English actor Denis Prick. This should of course have read Pe*is Price.

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Try
    Code:
    document.forms[item].submit();
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the code guys, but it doesn't work. I tried both of yours and they SHOULD work, but for some reason the form doesn't submit. I tried adding in an alert to check the name being used and it all matches up. Very strange.

  • #5
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    @Mike Campo:

    You don't need to know the "name" of the form. The form doesn't even need a "name." It's very poor practice to attach an "onclick" event to a submit type button.

    Multiple forms, multiple messages:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function validate(nMsg){
    
    		if (confirm(nMsg))
    			{
    			 return true;
    			}
    		return false;
    	}
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	 form {width:620px;margin:auto;font-family:times;font-size:12pt}
    	 fieldset {width:610px;background-color:#f0fff0;border:1px solid #87ceeb}
    	 legend {font-family:times;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px}
    	.submitBtn {font-family:tahoma;font-size:10pt;display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}
    
    </style>
    </head>
    	<body>
    		<form action="" method="post" onsubmit="return validate('Are you sure you want to delete this event?')">
    		   <fieldset>
    			<legend>Form</legend>
    				
    				<input type="submit" name="submit" value="Submit" class="submitBtn">
    		   </fieldset>
    		</form>
    		<br><br>
    		<form action="" method="post" onsubmit="return validate('Are you sure you want to insert this event?')">
    		   <fieldset>
    			<legend>Form</legend>
    				
    				<input type="submit" name="submit" value="Submit" class="submitBtn">
    		   </fieldset>
    		</form>
    	</body>
    </html>
    Last edited by Cranford; 08-25-2008 at 01:58 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by MikeCampo View Post
    Thanks for the code guys, but it doesn't work. I tried both of yours and they SHOULD work, but for some reason the form doesn't submit. I tried adding in an alert to check the name being used and it all matches up. Very strange.
    What is the actual name of your form? Have you inadvertantly chosen a reserved word?

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    What is the actual name of your form? Have you inadvertantly chosen a reserved word?
    I had it named "delete" with a count concatenated to the end. Every event in the database being displayed would increase the count by one. That way each event had its own form.

    I'm going to try out your code Cranford. I'm pretty new to this stuff, so thanks for the advice.
    Last edited by MikeCampo; 08-25-2008 at 11:27 PM.

  • #8
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Cranford View Post
    @Mike Campo:

    You don't need to know the "name" of the form. The form doesn't even need a "name." It's very poor practice to attach an "onclick" event to a submit type button.
    I added the onsubmit to the form and took out the name, but is there any way I can submit the form without using a button? I'd rather have it be a plaintext link that says "Delete". If that can't be done, I'll just go with the button.

    Thanks,
    Last edited by MikeCampo; 08-25-2008 at 11:35 PM.

  • #9
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    Just change the style of the submit button to resemble an anchor link:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	 form {width:620px;margin:auto;font-family:times;font-size:12pt}
    	 fieldset {width:610px;background-color:#f0fff0;border:1px solid #87ceeb}
    	 legend {font-family:times;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px}
    	
    
    	.submitBtn
    	{
    		border:0;
    		cursor:pointer;
    		color:#00f;
    		text-decoration:underline;	
    		background: transparent;	
    	}
    
    
    </style>
    </head>
    	<body>
    		<form action="">
    		   <fieldset>
    			<legend>Form</legend>
    				
    				<input type="submit" name="Submit" class="submitBtn">
    		   </fieldset>
    		</form>
    	</body>
    </html>

  • #10
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome Thanks a lot man!


  •  

    Posting Permissions

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