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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Location
    Guttenberg, NJ
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS function doesn't work :-(

    Hi!

    What am doing wrong? The script works -- identifies blank fields -- but the second function (change_subject) is ignored.

    This is the script:

    Code:
    <HEAD>
    <script>
      function is_filled()  
      {
        if (form_1.realname.value=="")
          {
            alert("Please enter your name")
            form_1.realname.focus()
            return false
          }
        if (form_1.subject.value=="")
          {
            alert("Please enter a subject")
            form_1.message.focus()
            return false
          }
    else
    return true
      };
      function change_subject
      {
      form_1.subject.value="form_1.realname.value + '_' + form_1.subject.value"
      };
    </script>
    </HEAD>
    This is the FORM (used for MSA FormMail):

    Code:
    	  <form  name="form_1"; onSubmit="return is_filled()"; on Submit="change_subject()"; method="post"; action="http://gb2gf.org/cgi-sys/FormMail.cgi">
    	  <input type="hidden" name="recipient"    value="drt@gb2gf.org">
    	  <input type="hidden" name="required"     value="greeting,realname,city_state,email_1,email_2,message">
    	  <input type="hidden" name="sort"         value="order:greeting,realname,city_state,email_1,email_2,message">
    	  <input type="hidden" name="redirect"     value="http://www.gb2gf.org/thanks.htm">
    
    <!-- Input fields here -->
    
               </form>
    Thanks!

    Dr. T.
    Last edited by Dr.T.; 11-23-2010 at 05:14 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    A good many errors I am afraid. Have you used your error console or Firefox?

    For starters, if (form_1.realname.value=="")
    There is no such field.

    form_1.subject.value= " form_1.realname.value + '_' + form_1.subject.value"
    Values in quotes are literals.

    var x = "myname"; //the variable x is assigned the literal value myname
    var x = myname; // the variable x is assigned the cuurent value of the variable myname.

    <form name="form_1"; onSubmit="return is_filled()"; on Submit="change_subject()";
    You cannot have onsubmit twice.
    <form name="form_1"; onSubmit="return is_filled(); change_subject()";


    Form validation of the pattern if (form_1.realname.value=="") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


    "Think about how stupid the average person is, and then realize that half of them are more stupid than that!" - George Carlin
    Last edited by Philip M; 11-23-2010 at 05:27 PM.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Location
    Guttenberg, NJ
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Follow up!

    Philip,

    1) Thank you for your reply.

    2) The variables realname and subject do exist; they're sent to the server and appear on the email that I receive.

    3) I took out the quotes:

    Code:
      function change_subject
      {
      form_1.subject.value=form_1.realname.value + "_" + form_1.subject.value
      };
    4) I change the FORM:

    Code:
    <form  name="form_1"; onSubmit="return is_filled(); change_subject()"; method="post"; action="http://gb2gf.org/cgi-sys/FormMail.cgi">
    I still get the viewer-entered subject in the email.

    5) For validation, I just want the viewer to know which are the “required” fields.

    Dr.T.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    I have tried to test your code using the following. Bear in mind that submitting the form refreshes the page so that the original field values are reinstated. To test I have replaced the submit button by an ordinary button. To prevent the form from submitting after is_filled() the function change_subject() must also be required to return true. In other words, submit only if both functions return true.

    Code:
    <html>
    <head>
    
    <script>
    function change_subject() {
    form_1.subject.value=form_1.realname.value + '_' + form_1.subject.value;
    alert (form_1.subject.value);  // YYY_XXX
    return true;
    }
    
    function is_filled() {
    // whatever code goes in here
    return true;
    }
    
    </script>
    
    </head>
    <body>
    <form  name="form_1"; onsubmit = "return is_filled() && change_subject()"; >
    	  
    <input type = "text" name = "subject" value = "XXX">
    <input type = "text" name = "realname" value = "YYY">
    
    <input type = "button" value = "Submit" onclick = "return is_filled() && change_subject()" >
    
    </form> 
    
    </body>
    </html>
    Last edited by Philip M; 11-23-2010 at 07:11 PM.


  •  

    Posting Permissions

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