...

View Full Version : Using a parameter as part of the function



MikeCampo
08-25-2008, 10:58 AM
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.


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

Philip M
08-25-2008, 11:14 AM
You could use something like the following on your submit button or hyperlink:-


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.

abduraooft
08-25-2008, 11:16 AM
Try
document.forms[item].submit();

MikeCampo
08-25-2008, 11:33 AM
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.

Cranford
08-25-2008, 12:06 PM
@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:


<!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>

Philip M
08-25-2008, 12:17 PM
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?

MikeCampo
08-25-2008, 10:16 PM
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.

MikeCampo
08-25-2008, 10:26 PM
@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,

Cranford
08-25-2008, 11:18 PM
Just change the style of the submit button to resemble an anchor link:


<!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>

MikeCampo
08-26-2008, 02:21 AM
Awesome :D Thanks a lot man!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum