SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    slight javascript / form problem

    i have the following line in my html file

    Code:
    <form name="theform" action="<? echo(WS_HTTP_SERVER . '/search') ?>" method="post" onsubmit="return checkKeywords(document.theform.keywords.value)" style="display: inline; margin: 0px;">
    everything works ok if i actually click submit button however if i press the enter key on the keyboard, i get an error that document.theform.keywords.value is null even if it is not. can anyone tell me how i can get the form to work with both the submit button and the enter key?
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure a SUBMIT control needs to exist for the enter key to work...do you have a submit button?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, it is there. here is the entire form code

    PHP Code:
     <form name="theform" action="<? echo(WS_HTTP_SERVER '/search'?>" method="post" onsubmit="return checkKeywords(document.theform.keywords.value)" style="display: inline; margin: 0px;">
                            <input type="text" name="keywords" size="15">
                              <input type="submit" name="Submit" value="Search" onclick="return checkKeywords(document.theform.keywords.value)"  style="border:1px outset #C2C1BE; color: #000000; font-family: Arial; font-size: 10pt; background-color: #D2D2D2">
                               <select size="1" name="cType[]" style="font-family: Arial; font-size: 10pt">
                                <option value="console">Console</option>
                                <option value="pc">PC</option>
                                <option value="handheld">Handheld</option>
                                <option value="handheld">Arcade</option>
                                <option value="soundtrack">Soundtrack</option>
                                <option value="magazine">Magazine</option>
                                <option value="memorabilia">Memorabilia</option>
                              </select></form>
    Last edited by archigamer; Nov 20, 2002 at 17:18.
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the function checkKeywords();

    Also instead of using document.theform ... etc use either this.element or document.form[0].element.property

  5. #5
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is the function

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    function checkKeywords(field) {
    	var whitespace = " \t\n\r";
    	var i;
    	if((field == null) || (field.length == 0)) {
    		alert("Please enter valid keyword(s)");
    		return false;
    	}
    	for(i = 0; i < field.length; i++) {
    		var c = field.charAt(i);
    		if(whitespace.indexOf(c) == -1) {
    		return true;
    		}
    	}
    	
    	alert("Please enter valid keyword(s)");
    	return false;
    }
    -->
    </script>
    tried your suggestion about document.form[0] etc. still not working.
    Last edited by archigamer; Nov 20, 2002 at 20:10.
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had this problem with using document.form, try using only:
    theform.keywords.value, without document.

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, what exactly is checkKeywords supposed to do? Seems to me that it
    1. returns false if keywords.value is null
    2. returns false if keywords.value has a length of 0
    3. if no whitespace characters are found, it returns true, otherwise alerts use of error
    Basically, it just looks like you want to make sure it isn't blank, or filled with whitespace characters, right? Here's what is wrong with your method...
    1. A form input's value will never be null, but rather an empty string: ''
    2. Checking for a length of 0 is the same as checking for equality to an empty string
    3. Your whitespace variable isn't what you think it is. Its a string with a space, 3 backslashes and the letters t, n, and r. NOT a space, a tab, a newline, and a carriage return.
    4. Your whitespace checking loop short circuits (even if the whitespace variable did work)
    Here is a simple function that checks to make sure a field isn't blank (or filled with whitespace, and no leading whitespace)
    Code:
    <script>
    function checkKeywords(elem) {
    	if (!/^\S/.test(elem.value))
    		alert('Please enter valid keyword(s)');
    		elem.focus();
    		return false;
    		}
    	return true;		
    	}
    </script>
    
    <form ... onsubmit="return checkKeywords(this.keywords)" ... >
    Of course, this can be change to allow NO spaces, regardless of where they appear, but I dont think that is what you want...
    Last edited by beetle; Nov 21, 2002 at 11:24.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, but still no go. I think it is my php code now so i am going to go and post about it there.
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But you really didn't tell me what you are trying to do! What is it supposed to do?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    like you said check to make sure there is no whitespace in the beginning characters or enter a blank field.

    I it is not working because when you press enter key it doesnt return the submit button with the value of "Search" which i have checking in my php script to see if it is set, if it is it performs the search. if you press the submit button with your mouse it works correctly because the submit button with the value "Search" is sent back to php. so my problem is now is to get the value of "Search" sent back when a person presses the enter key.
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  11. #11
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should always use a hidden field to check for form submissions in PHP
    Code:
    <form action="whatever.php" method="post">
    <!-- FORM STUFF HERE -->
    <input type="hidden" name="valid" value="true" />
    <input type="submit" value="Submit" />
    </form>
    Then in whatever.php...
    PHP Code:
    <?php
    if (isset($_POST['valid'])) {
       
    # Whatever PHP code here
       
    }
    ?>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  12. #12
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks it works perfectly now.
    Web Finesse Studios
    Professional, business oriented web hosting and development.


Bookmarks

Posting Permissions

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