Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: open

Message Too Old, No Replies

JavaScript form action change

a simple thing...why's it not working?

   
8:34 pm on May 28, 2002 (gmt 0)

10+ Year Member



I'm writing a simple field validator script that checks just a few things (like if the customer wants to be contacted via email, they have an email in the email field; if they want to be contacted via phone, they have a phone number in the phone field). Simple. The validation works great, but I can't seem to change the action of the form depending on what the validator discovers.

The script is triggered by a graphical submit button. It runs through it's various tests, giving error messages if necessary, and then at the end of the script, it submits (document.form.submit()). The last test it runs is if there are no errors, then assign the action of the form (document.formname.action.value=) to the cgi script on the server. If an error occurs, there is no action, so the page reloads itself. Nothing fancy.

The problem is, the page always reloads itself, even if there are no problems and the action is never assigned and the form never submitted. The last test is being run but it seems to be ignoring the action.

I've tried doing it backwards too. Assigning the form an action, and then changing the action if any of the initial tests come back with an error. That doesn't work either; it just takes me to the cgi script after giving me an error.

It's just a bone simple validator javascript. I'm trying to create all my own scripts instead of using other folks' and I'm not the best, so I may be just doing something wrong. But all the references I've read don't seem to indicate that I'm doing anything wrong. Anybody that is wiser than I in the world of scripting have some advice?

8:36 pm on May 28, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



hi Kit. The best thing is for someone to have a look at the script. If you can put it in a TXT file and post it on your server, then put the URL in your profile, someone can peek at it.

I've been able to modify the action of a form based on several different variables, so the concept is correct.

It could be a simple oddball error.

8:37 pm on May 28, 2002 (gmt 0)

10+ Year Member



It would be helpful to see the relevant section of the script.
10:54 pm on May 28, 2002 (gmt 0)

10+ Year Member



URL of posted text file: [gobozeman.com...] . Using this one, there is no action for the form on the HTML page. A graphical link triggers the javascript. Thanks for your help.
11:09 pm on May 28, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



these 2 lines

if(x!=1){\
document.CopiersInfo.action.value=""

should be this

if(x!=1){
document.CopiersInfo.action = "";

the book says no value for action.

<added>I assume the actual code has all the semicolons present

1:53 am on May 29, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



right, action is a property of the form object. It has no value.
2:47 pm on May 29, 2002 (gmt 0)

10+ Year Member



I have previously written the script with "document.CopiersInfo.action=" (with no .value) and have tried it again, but it still gives me the same results. (Thanks for clearing that up, though; that was a source of a little confusion.) It still will not send the form info to the cgi script when the form is filled out correct.

I have written scripts with and without semi-colons (to my knowledge, it does not make any difference), but, just to cover all my bases, I went in and added semi-colons to this one. No difference.

Thanks for your help. I'm reposting the text file at [gobozeman.com...] . Any other suggestions?

3:57 pm on May 29, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



kit_kalen

can you sticky me the url for the actual form, I can figure it out then, look at types etc.

4:20 pm on May 29, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I removed the url's from the script, you will just have to pop them back in

I got it working and this is the page I used.

<HTML>
<HEAD>
<TITLE></TITLE>

<SCRIPT LANG="javascript">
<!--
function formCheck() {
if (document.CopiersInfo.namefield.value == "") {
alert("You forgot to enter your name.");
} else if (document.CopiersInfo.contact[0].status && document.CopiersInfo.phone.value=="") {
alert("We cannot contact you by phone without a phone number.");
} else if (document.CopiersInfo.contact[1].status && document.CopiersInfo.email.value=="") {
alert("We cannot contact you by e-mail without an e-mail address.");
} else if (document.CopiersInfo.contact[2].status && document.CopiersInfo.address.value=="") {
alert("We cannot contact you by mail without your address.");
} else {
document.CopiersInfo.action="";
document.CopiersInfo.redirect.value="";
document.CopiersInfo.recipient.value="";
document.CopiersInfo.subject.value="Request Copier Info";
document.CopiersInfo.submit();
}
}
//-->
</SCRIPT>
</HEAD>

<BODY>

<FORM METHOD=POST NAME="CopiersInfo" ACTION="#">
<INPUT TYPE=HIDDEN NAME="redirect" VALUE="#">
<INPUT TYPE=HIDDEN NAME="recipient" VALUE="#">
<INPUT TYPE=HIDDEN NAME="subject" VALUE="#">

<P><INPUT TYPE=TEXT NAME="namefield" SIZE=20>

<P>Phone<INPUT TYPE=RADIO NAME="contact" CHECKED>   <INPUT TYPE=TEXT NAME="phone" SIZE=20>

<P>Email<INPUT TYPE=RADIO NAME="contact">   <INPUT TYPE=TEXT NAME="email" SIZE=20>

<P>Address<INPUT TYPE=RADIO NAME="contact">   <INPUT TYPE=TEXT NAME="address" SIZE=20>

<P><INPUT TYPE=BUTTON VALUE="Click Me" onClick="javascript:formCheck();">

</FORM>
</BODY>
</HTML>

6:10 pm on May 29, 2002 (gmt 0)

10+ Year Member



Jatar_k - Thanks for the code. I plugged it in to the page and I got a step closer. It tried accessing the cgi script, then gave me a bad referrer error (which is technically bogus, since other forms not sent through javascript do not have the error). I have had the javascript in an external js file, but, on a lark, I decided to put the code in the page. And, holy toledo, batman, it worked! I'm trying to figure out why that would make a difference, but I'm happy it's working. Thanks for your help. Any idea why I'd have a problem with an external javascript file?
6:55 pm on May 29, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I don't think it was the external js, I found a bunch of little things in there that were stopping it from working. There was the single forward slash in the final if statement that was causing that statement to bomb out and the method that I used causes it to only submit if successful instead of posting every time. I found that once it posted to itself it worked even less and was causing problems. It was dropping through some of the if statements as well but that could have been me because I wasn't sure what types you were using. As far as the cgi script goes you will have to look at the script itself I think.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month