SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Thread: slight javascript / form problem
-
Nov 19, 2002, 15:21 #1
- 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;">
-
Nov 20, 2002, 16:29 #2
-
Nov 20, 2002, 16:50 #3
- 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.
-
Nov 20, 2002, 18:52 #4
- 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
-
Nov 20, 2002, 19:52 #5
- 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>
Last edited by archigamer; Nov 20, 2002 at 20:10.
-
Nov 21, 2002, 10:44 #6
- 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.
-
Nov 21, 2002, 11:18 #7
- 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
- returns false if keywords.value is null
- returns false if keywords.value has a length of 0
- if no whitespace characters are found, it returns true, otherwise alerts use of error
- A form input's value will never be null, but rather an empty string: ''
- Checking for a length of 0 is the same as checking for equality to an empty string
- 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.
- Your whitespace checking loop short circuits (even if the whitespace variable did work)
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)" ... >
-
Nov 21, 2002, 14:10 #8
- 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.
-
Nov 21, 2002, 14:11 #9
-
Nov 21, 2002, 14:25 #10
- 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.
-
Nov 21, 2002, 14:50 #11
- 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>
PHP Code:<?php
if (isset($_POST['valid'])) {
# Whatever PHP code here
}
?>
-
Nov 21, 2002, 16:29 #12
- Join Date
- Jan 2002
- Location
- Strongsville OH
- Posts
- 1,534
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks it works perfectly now.
Bookmarks