Forum Moderators: open

Message Too Old, No Replies

How to implement Ajax with search?

         

toplisek

9:30 am on Jan 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Search results will be insert into the element of my choice or in the dynamic layer displayed over the whole page.

How to do this with Ajax if I have search results solved with Ajax output?
Which is the best application in Ajax to deal with as I like to set Id of the element where the Ajax will insert the results.

daveVk

10:32 pm on Jan 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Any of the frameworks would be suitable.

JQuery example

$("#feeds").load("feeds.html");

from [api.jquery.com...]

JAB Creations

10:51 am on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, jQuery is not the answer, ever.

Use the onkeydown event handler to send the value of the form field via AJAX to the server.

- John

toplisek

11:10 am on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you mean:
onkeydown event handler:
<body>
<form action="" method="POST" id="myForm">
<input type="text" name="myText" onKeyDown="changeVal()">

<script type="text/javascript" language="JavaScript">
s1 = new String(myForm.myText.value)

function changeVal() {
s1 = "You pressed a key"
myForm.myText.value = s1.toUpperCase()
}

</script>
</form>
</body>

The onKeyDown event handler uses the following Event object properties.

type - this property indicates the type of event.
target - this property indicates the object to which the event was originally sent.
layerX - the cursor location when the KeyDown event occurs.
layerY - the cursor location when the KeyDown event occurs.
pageX - the cursor location when the KeyDown event occurs.
pageY - the cursor location when the KeyDown event occurs.
screenX - the cursor location when the KeyDown event occurs.
screenY - the cursor location when the KeyDown event occurs.
which - this represents the key pressed as its ASCII value.
modifiers - lists the modifier keys (shift, alt, ctrl, etc.) held down when the KeyDown event occurs.

Fotiman

2:06 pm on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Use the onkeydown event handler to send the value of the form field via AJAX to the server.

It's worth noting that using onkeydown could lead to inaccurate results. If you press and hold a key down, the events will be:

onkeydown, onkeypress, onkeypress, onkeypress, ..., onkeyup

In other words, onkeypress will be repeated. For that reason, onkeyup is a better listener to attach to.


<script type="text/javascript" language="JavaScript">

Don't use the language attribute. It's invalid.

s1 = new String(myForm.myText.value)

Don't use "new" for String values. Always declare variables using the var keyword. And don't forget the semicolon:

var s1 = myForm.myText.value;

Here's a summary of the logic I would start with if I was doing it:

1. Attach onkeyup event handler/listener to the input field
2. Use cancelTimeout and setTimeout to schedule the AJAX request for some short time in the future. This is so that if someone is typing quickly, you only perform the search when they have stopped long enough for it to make sense to do the search. Start with a 500ms delay, and adjust from there.
3. When the function in the setTimeout is called, send an AJAX request to the server with the value in the input field. The callback method will get the result and populate the value on the page.

Note, you should also make sure that your callback method verifies somehow that the results back from the server are from the most recent AJAX request. Because AJAX is asynchronous, you might send 2 requests and get the responses back in the wrong order.

JAB Creations

5:59 pm on Jan 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, there are multiple events that could be used, I was mainly interested in making sure the thread went in the correct direction. ;)

- John