SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form submission and return false

    I have a small search appliance on my site that uses the following HTML:

    Code:
    <form action="/search/search.aspx" method="post" onsubmit="siteSearch(this);">
    <input name="q" id="searchbox" type="text" value="Search...">
    <input type="hidden" id="search-url" value="/search/search.aspx"> 
    <input type="submit" class="search" name="submit" id="search-submit" value="GO">
    </form>
    And I use this javascript (with jquery) on the submission:

    Code:
    function siteSearch(form) {
    	var q = $("#searchbox").attr("value");
    	var url = $("#search-url").attr("value");
    	if (q == "Search...") {
    	    alert("Please enter search criteria.");
    	    return false;
    	}
        form.action = url + "?q=" + q;
        return true;
    }
    I am attempting to create a stop in the form action if someone leaves the input blank or with the predefined "Search..." however, once the alert pops up and a user presses "OK" it still attempt to submit the form.

    Any ideas on how to stop the action in the form?

  2. #2
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bradical1379 View Post
    I have a small search appliance on my site that uses the following HTML:

    Code:
    <form action="/search/search.aspx" method="post" onsubmit="return siteSearch(this);">
    <input name="q" id="searchbox" type="text" value="Search...">
    <input type="hidden" id="search-url" value="/search/search.aspx"> 
    <input type="submit" class="search" name="submit" id="search-submit" value="GO">
    </form>
    ...
    Try that. I've added return to the onsubmit.

    Your code looks alright to me. When I'm doing Javascript form validation I always have the return there so see if that works for you.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,676
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Yes, you need the return so that the value returned from the function is itself returned to the form.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •