mcmuney Posted January 29, 2008 Share Posted January 29, 2008 I'm utilizing a search that only works with a click. When you hit ENTER, it just blanks out the input field and does nothing else, giving the impression that it's not working. I'd like an alert that will ask to click the button on enter. This is the site if you want to test it: http://www.gasnearu.com Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 try this: <script language="javascript"> function checkZIP() { var zippy = document.getElementById('zipcode').value.length; if (zippy < "5") { alert("Please Enter A Valid Zip Code"); } else { document.zipform.submit(); } } </script> <form name="zipform" action="gasfinder.php" method="post"> Zip Code: <input type="text" name="zip" id="zipcode" maxlength="5"> <input type="button" value="Get Gas Now!" onclick="checkZIP()"> </form> Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 29, 2008 Author Share Posted January 29, 2008 Thanks for that, I needed to add that also. But that's not what I was looking for. I want to show an alert when users press the ENTER button. I want an alert telling them to click the button instead. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 do this then: <script language="javascript"> function checkZIP() { var zippy = document.getElementById('zipcode').value.length; if (zippy < "5") { alert("Please Enter A Valid Zip Code"); } else { document.zipform.submit(); } } </script> <form name="zipform" action="gasfinder.php" method="post" onsubmit="checkZIP(); return false"> Zip Code: <input type="text" name="zip" id="zipcode" maxlength="5"> <input type="button" value="Get Gas Now!" onclick="checkZIP()"> </form> Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 29, 2008 Author Share Posted January 29, 2008 I don't think you're understanding the issue. The alerts are working fine when the button is clicked. The alerts are not working when the ENTER key is pressed. On ENTER, the page just refreshes, it only works when the button is clicked. So, I want an alert to display when ENTER key is pressed, to say, "Please click the search button". Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 I don't think you're understanding the issue. The alerts are working fine when the button is clicked. The alerts are not working when the ENTER key is pressed. On ENTER, the page just refreshes, it only works when the button is clicked. So, I want an alert to display when ENTER key is pressed, to say, "Please click the search button". and the code I give you above does just that. Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 29, 2008 Author Share Posted January 29, 2008 Hmmm, then maybe I'm not doing it right. Here's my original code if that helps: <!-- Begin function dosearch(form) { var zipcode = form.zipcode.value; var valid = "0123456789" if (! zipcode) { alert("Please enter a Zipcode!"); form.focus(); form.select(); } if (zipcode.length > 5) { alert("Invalid Zipcode! Must be 5 characters!"); form.focus(); form.select(); } if (zipcode.length < 5) { alert("Invalid Zipcode! Must be 5 characters!"); form.focus(); form.select(); } var ok = 1; var temp; for (var i=0; i < zipcode.length; i++) { temp = "" + zipcode.substring(i, i + 1); if (valid.indexOf(temp) == "-1") { ok = 0; } } if (ok == 0) { alert("Invalid Zipcode! Only number allowed!"); form.focus(); form.select(); } else { window.location = 'zipcode_' + zipcode; } } function openMap(url, name) { var temp = ""; url = '' + url; splitstring = url.split(" "); for(i = 0; i < splitstring.length; i++) { if (i > 0) { temp = temp + '_' + splitstring[i]; } else { temp = splitstring[i]; } } url = temp; window.open(url, name, 'scrollbars=1,resizable=no,width=550,height=450,status=0,menubar=0'); } // End --> And the form: <form action="index.php" method="get" name="search"> <table border="0" cellpadding="3" cellspacing="2"> <tr> <td align="right">Zipcode: <input name="zipcode" type="text" class="textbox" value="<?=$zipcode;?>" size="8" maxlength="5"/></td> <td><input type="button" value="Click to Get Gas Prices" onclick="dosearch(this.form);"/></td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 29, 2008 Share Posted January 29, 2008 your not adding the onsubmit() event to the form tag. onsubmit="checkZIP(); return false" Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 29, 2008 Share Posted January 29, 2008 your not adding the onsubmit() event to the form tag. onsubmit="checkZIP(); return false" checkZIP()? If you want to catch the "enter" event, just do what he said, but the form will look like this: <form action="index.php" method="get" name="search" onsubmit="dosearch(this);return false;"> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 checkZIP()? yes; that was the original function that I made. Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 30, 2008 Author Share Posted January 30, 2008 I added your full script, in addition to mine, then added the onsubmit code along with the onclick. I did this because without onclick, the search function doesn't work. Anyhow, none of my attempts have been successful. On ENTER the page always refreshes. Here's what the last one looked like: I added onsubmit="checkZIP(); return false" and the id="zipcode" to my existing code <form action="index.php" method="get" name="search"> <table border="0" cellpadding="3" cellspacing="2"> <tr> <td align="right">Zipcode: <input name="zipcode" id="zipcode" type="text" class="textbox" value="<?=$zipcode;?>" size="8" maxlength="5"/></td> <td><input type="button" value="Click to Get Gas Prices" onclick="dosearch(this.form);" onsubmit="checkZIP(); return false"/></td> </tr> </table> </form> And I'm utilizing this in addition to the existing script: <script language="javascript"> function checkZIP() { var zippy = document.getElementById('zipcode').value.length; if (zippy < "5") { alert("Please Enter A Valid Zip Code"); } else { document.search.submit(); } } </script> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 you still didn't do it like I was telling you to; do it like this: <script language="javascript"> function checkZIP() { var zippy = document.getElementById('zipcode').value.length; if (zippy < "5") { alert("Please Enter A Valid Zip Code"); } else { document.search.submit(); } } </script> <form action="index.php" method="get" name="search" onsubmit="checkZIP(); return false"> <table border="0" cellpadding="3" cellspacing="2"> <tr> <td align="right">Zipcode: <input name="zipcode" id="zipcode" type="text" class="textbox" size="8" maxlength="5"/></td> <td><input type="button" value="Click to Get Gas Prices" onclick="checkZIP();dosearch(this.form)"/></td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 30, 2008 Author Share Posted January 30, 2008 Ok, I used the exact code. RESULT: It now alerts on ENTER. This is what I was looking for but this also helps. Problem: On valid zipcode entry, ENTER just refreshes the page (see what I'm talking about by trying it here: http://www.gasdays.com) Try pressing ENTER after placing a valid zipcode. This is where I want to utilize the alert to say "Click the button". Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 30, 2008 Share Posted January 30, 2008 Your form action is for "index.php"; of course it is going to submit back to itself (the domain follows the index page); unless you have another index file, with a different file extension (ie "html" or "htm"). If your "index.php" is supposed to redirect; then you might want to take a look at your php code for this issue. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.