Welcome! Log In Create A New Profile

Advanced

How To? In Textbox Empty Do Nothing

Posted by Malcolm 
How To? In Textbox Empty Do Nothing
August 06, 2010 03:23AM
How do I make it so that if the textbox is empty and the button is clicked, nothing happens?



Edited 1 time(s). Last edit at 08/06/2010 03:27AM by Malcolm.
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 08:00AM
This is quite easy actually, I'll show you the code I use for my search box in combination with a simple javascript:
<script type="text/javascript">
<!--
function mySubmit() {
    if (document.search.query.value == '') {
        alert('Introduce a search term before hitting the Search Button');
        return false;
    }
}
//-->
</script>

<div>
<form id="search" action="search.php" method="get">
<fieldset>
<input type="text" name="query" maxlength="255" />
<input type="hidden" name="search" value="1" />
<a href="javascript:document.forms['busqueda'].submit()" onclick="return mySubmit()"><input type="submit" value="Search" /></a>
</fieldset>
</form>
</div>
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 09:32AM
Thanks, but that will make a popup. I want it to do nothing, like google.
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 09:53AM
Simply remove the alert from the javascript code:
<script type="text/javascript">
<!--
function mySubmit() {
    if (document.search.query.value == '') {
        return false;
    }
}
//-->
</script>
See how this goes. smiling smiley
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 04:00PM
It's not working. This is how i have it:

<script type="text/javascript">
<!--
function mySubmit() {
    if (document.search.query.value == '') {
        return false;
    }
}
//-->

</script>

<br /><br /><br /><center>
<table cellpadding="5" cellspacing="1" class="searchBox">
<tr>
	<td align="center">

	<form action="" method="get" id="search">

<table><tr><td>
	<div align="left"> 
<input type="text" name="query" id="query" size="40" value="<?php   print quote_replace($query);?>" action="includes/js_suggest/suggest.php" columns="2" autocomplete="off" delay="1500">

</div> 
<td>
<a href="javascript:document.forms['search'].submit()" onclick="return mySubmit()"><input type="image" src="images/artic.gif"></a>
</td></tr>
<tr>
<td>
<center><a href ='?adv=1'>Advanced Search</a></center>
</td>
</tr>
</table>

I'm really not good with javascript.
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 05:05PM
It's not the javascript I gave you, I tested it and it works as you want.

One thing I notice is that the closing </form> tag is missing in your code.
And maybe the suggest.php is interfering, try your code closing the form and removing this part:
 action="includes/js_suggest/suggest.php" columns="2" autocomplete="off" delay="1500"

Or simply copy the code I gave you for the searchbox, just for testing.

That's all I can say...
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 06:37PM
I tried it without the suggest, it still didn't work. The form is closed last on in the script. I just didn't post that.

It always says 'The search "" did not match any documents '
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 07:10PM
Try the code as I posted before, for the javascript and the searchbox.
t-p
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 07:56PM
hi Willy,

Thanks for the code.

How can I use it if I am using "image" instead of "submit"?

I tied this but did not work.

<script type="text/javascript">
<!--
function mySubmit() {
if (document.search.query.value == '') {
alert('Introduce a search term before hitting the Search Button');
return false;
}
}
//-->
</script>

<div>
<form id="search" action="search.php" method="get">
<input type="text" name="query" maxlength="255" />
<input type="hidden" name="search" value="1" />
<a href="javascript:document.forms['busqueda'].submit()" onclick="return mySubmit()">
<input type="image" src="/images/searchbutton.gif" class="searchbox_submit" title="Search: Enter search term before clicking this button" value="" /></a>
</form></div>



Edited 1 time(s). Last edit at 08/06/2010 08:29PM by t-p.
Re: How To? In Textbox Empty Do Nothing
August 06, 2010 08:36PM
Hello to the both of you,

I believe I made a mistake,

The code should be
<script type="text/javascript">
<!--
function mySubmit() {
    if (document.search.query.value == '') {
        alert('Introduce a search term before hitting the Search Button');
        return false;
    }
}
//-->
</script>

<div>
<form id="search" action="search.php" method="get">
<fieldset>
<input type="text" name="query" maxlength="255" />
<input type="hidden" name="search" value="1" />
<a href="javascript:document.forms['search'].submit()" onclick="return mySubmit()"><input type="submit" value="Search" /></a>
</fieldset>
</form>
</div>
I believe this should work for image button too, if it doesn´t I don't know how to make it work for you, the only thing I can think of is that you should try to change
<a href="javascript:document.forms['search'].submit()"
into
<a href="javascript:document.forms['search'].image()"
but I don't think that'll work.
t-p
Re: How To? In Textbox Empty Do Nothing
August 07, 2010 03:43AM
thanks Willy for your response.

I tried your suggestions but did not work for me. Thanks again for your help.
Sorry, only registered users may post in this forum.

Click here to login