views:

34

answers:

5

How to make up a form that is going to be a search box and work only via Ajax? That is:

1) What to put as the form's action value? (action="what")

2) How to submit it so that nothing else happenes except for calling the JavaScript function?

A: 

Use a framework like jQuery. It has facilities to hook forms and turn them into AJAX. Check out the jQuery Forms plugin.

Marcelo Cantos
No, thank you very much.
Alex Polo
(IMHO, all offers of jQuery should be comments not answers. Oh the ad world, how I would love to put the end on you!)
Alex Polo
@dfjhdfjhdf, this is a case where the usage of jQuery is justified. Using vanilla Javascript, you are looking at some 40+ rows of code, including building your own ajax handling functions. Using jQuery or similar would probably do that in under 5 rows - but you are correct, this answer would have served better as an comment. :)
Tatu Ulmanen
http://meta.stackoverflow.com/questions/45176/when-is-use-jquery-not-a-valid-answer-to-a-javascript-question
Alex Polo
Hehe, Tatu Ulmanen, funny indeed.
Alex Polo
@dfjhdfjhdf, given your attitude, you are very unlikely to find proper answers here.
Tatu Ulmanen
What attitude? I don't love jQuery. I am so cool that I make the ends meet on my own (without JS libs). BTW, I just answered my own question. Hope it will help someone one day.
Alex Polo
A: 

I think you can use HTml box also.

Penny stocks
A: 
  1. form is irrelevant for this. form is just a container for the other form elements. You can have whatever action you want for it (GET / POST, whatever), won't matter.
  2. attach an event handler to the search button click or keydown event for the textbox and check for Enter key and route the value of the textbox to an AJAX call using xmlhttp object. I am sure you can find the detail on how to do Ajax using xmlHttpRequest object somewhere on the web :)... or... well, you can use jQuery, lol.
Jimmy Chandra
Yeah, my question consisted of two minor issues not the Ajax part.
Alex Polo
"form is irrelevant for this." - Do you suggest to not use the form?
Alex Polo
Technically, you can have INPUT tag without any FORM tag surrounding it and AJAX should still work just fine, but if you feel like you want to stay compliant, well, doesn't hurt to have it there.
Jimmy Chandra
A: 

I think it works well this way:

<form method="post" onsubmit="theFunction(this); return false;">
    <input type="text" value="" class="address_bar" />
</form>
Alex Polo
A: 

You can do something like:

<input id="search" /> <input type="button" onclick="doSearch()" />
<div id="results"></div>

dosearch will be something like

function doSearch()
    {
    var s = document.getElementById("search");
    // Substitute this line with whatever code you use to do an AJAX call
    // Essentially call a serverside page by passing the search keyword and
    // call onReturn afterwards 
    doAJAXCall("searchpage.php?q="+s.value, onReturn);
    }

searchpage.php will search the DB as needed and then return, for instance, a JSON string (e.g. by storing the results in an array and using json_encode) or XML or even straight HTML

Finally, the onReturn function will go through the results and print them for instance in a list, like this:

 function onReturn(results)
     {
     // decode your results variable here in the appropriate way
     // e.g. into a JSON object 
     var d = document.getElementById("results");

     var res = "<ul>";

     for (var i=0; i<results.lenght; i++)
         res = res + "<li>"+i.text+"</li>";

     res = res + "</li>";
     d.innerHTML = res; 
     }
nico
Thanks for your reply. Though there was no need to go so deep. But thanks anyway!
Alex Polo
@djhdfjhdf: no prob, did just take a couple of mins to write :)
nico