www.webdeveloper.com
Results 1 to 8 of 8

Thread: Why is this not working!!!

  1. #1
    Join Date
    Oct 2009
    Posts
    116

    Why is this not working!!!

    I have this code it works fine validates fine until i press submit and it fails can anyone tell me what


    just a simple drop down box
    HTML Code:
     <tr>
              <td><label for="subject">Subject</label></td>
              <td><select name="subject" id="subject" value="<?=$_SESSION['post']['subject']?>" >
              
                <option value="0" selected="selected">Choose</option>
                <option value="Question">Question</option>
                <option value="Business proposal">Business proposal</option>
                <option value="Advertisement">Advertising</option>
                <option value="Complaint">Complaint</option>
              </select>         </td>
             <td id="subjectInfo">Subject</td>
            </tr>
    With this javascript

    Code:
    var subject = $("#subject");
    var subjectInfo = $("#subjectInfo");
    
    form.submit(function(){
    		if(validateName() & validateEmail() & validateSubject() & validateName() & validateName()) 
    			return true
    		else
    			return false;
    	});
    
    function validateSubject(){
    		if(this.selectedIndex > 0) {
       			subjectInfo.text("Valid");
    			subjectInfo.removeClass("error");
    			return true;
    		} 	
    		// Not valid
    		else{
    	  		subjectInfo.text("NOT valid");
    	  		subjectInfo.addClass("error");
    	  		return false;
       
      }
    	}
    Again it works fine up until i press submit and it goes from valid to not valid! can anyone tell me what i am not seeing??

  2. #2
    Join Date
    Oct 2009
    Posts
    116
    can anyone suggest what is wrong here?

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by webevelopersFTW View Post
    can anyone suggest what is wrong here?
    https://developer.mozilla.org/en/Cor...ical_Operators
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  4. #4
    Join Date
    Oct 2009
    Posts
    116
    well thanks for the help, although it didn't help but thanks anyways.

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Quote Originally Posted by webevelopersFTW View Post
    well thanks for the help, although it didn't help but thanks anyways.
    I thought he gave you a LOT of help.

    Take a look at your code with the '&' characters
    and compare it to the logical comparison characters of '&&'

  6. #6
    Join Date
    Oct 2009
    Posts
    116
    what i am saying is if i do this... it still does not validate when i press the submit button. it validates when i click on the drop down box but when i press the submit button it goes back to being 'NOT valid' (as show in the second set of code)

    Code:
    form.submit(function(){
    		if(validateSubject()) 
    		return true	
    		else
    		return false;
    	});
    Code:
    function validateSubject(){
    		if(this.selectedIndex > 0) {
       			subjectInfo.text("Valid");
    			subjectInfo.removeClass("error");
    			return true;
    		} 	
    		// Not valid
    		else{
    	  		subjectInfo.text("NOT valid");
    	  		subjectInfo.addClass("error");
    	  		return false;
       
      }
    	}

  7. #7
    Join Date
    Oct 2009
    Posts
    116
    can anyone out there help?

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb Something to try...

    Put your PHP back into this example.

    Your requirements are not very clear to me with only a portion of the code given
    so this is just a guess as to what you are trying to do...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    function validate() {
      if (validateName() && validateSubject() ) {
        alert('Name AND Subject are BOTH valid');
        return true
      } else {
        alert('Name OR Subject are NOT valid');
        return false;
      }
    }
    
    function validateName(){           // Valid
      if(document.getElementById('subject').selectedIndex > 0) { // alert('Valid');
        return true;
      } else {	// alert(('NOT valid');  // Not valid
        return false;
      }
    }
    
    function validateSubject(){           // Valid
      if(document.getElementById('subjectinfo').selectedIndex > 0) { // alert('Valid');
    //    subjectInfo.text("Valid");  subjectInfo.removeClass("error");
        return true;
      } else {	// alert(('NOT valid');  // Not valid
    //    subjectInfo.text("NOT valid");  subjectInfo.addClass("error");
        return false;
      }
    }
    
    </script>
    
    </head>
    <body>
    
    <form name="myForm" action="javascript:alert('Success')" method="post"
     onsubmit="return validate()">
    <table border="1">
     <tr>
      <td><label for="subject">Subject</label></td>
      <td id="subjectInfo">Subject Info</td>
     </tr>
     <tr>
      <td>
       <select name="subject" id="subject" value="">
        <option value="0" selected="selected">Choose</option>
        <option value="Question">Question</option>
        <option value="Business proposal">Business proposal</option>
        <option value="Advertisement">Advertising</option>
        <option value="Complaint">Complaint</option>
       </select>
      </td>
      <td>
       <select name="subjectinfo" id="subjectinfo" value="">
        <option value="0" selected="selected">Select</option>
        <option value="Question">Question</option>
        <option value="Business proposal">Business proposal</option>
        <option value="Advertisement">Advertising</option>
        <option value="Complaint">Complaint</option>
       </select>
      </td>
     </tr>
    </table>
    <input type="submit" value="Submit">
    </form>
    
    </body>
    </html>
    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles