views:

242

answers:

3

I'm having problems submitting my ajax form. I am used to the old fashioned way with refresh but this new stuff is beyond me for the time being. It's time to start learning new technolohies.

I have an autosuggest box that is getting my results from a database and populating the textbox just fine. When I designed this about 6 months ago, I was searching the database on the value rather than the key value. This is a problem today and needs to be fixed.

WHat the ajax has returned to my script is the key/value pair. Now that I have the id, I need to pass that into my php method so I can process it from there.

Can somone please give me a hand with this? It seems simple enough but again, javascript was never my thing so I am lost.

Here is all of the relevant code. Also, I don't think, at least from the code samples I have seen so far that I even need a form tag. Am I correct on this? Ideally, I want to submit the found ajax value with the enter button and NOT using a button.

So, just to clarify, this is what happens. The user types 2 or 3 letters. The ajax queries the db on a "LIKE" operator and returns the matches. The user chooses the one he wants and then the id goes out to my method and returns the exact record in a different window.

<form method="post" class="hdrForm" id="search" action="../../index.php?cer=2" target="_top">
<input type="text" name="string" class="hdrInput" id="string" value="Quick Search"><div id="acDiv"></div>

</form>


Note.. I need the "id" in this function to be submitted. Right now, I am getting the POST val off the form tag and that's not correct but how?

  AC.chooseFunc = function(id,label)
  {
    document.forms.search.submit();
  }

Thanks for any help that you guys can give me on this.

A: 
document.getElementById("search").onsubmit = function() {
    // Do what you want with the form
    return false; // Stops submit continuing
}

This also degrades gracefully (if your server side program is written right) in that users without javascript get the form submitted normally to the page in the action attribute, without the AJAX.

I'd suggest you use a framework such as jQuery. A basic tutorial (including AJAX) is available

Macha
Thanks for the help. Maybe I should have been more explicit. I don't know how to submit the id I need. Is the returned value from AJAX a POST var? Is there a way for me to somehow echo it? I can get the id into an alert but I just don't know how to pass it in my query string. That's where I need it.
Passing it in my query string is the only way I know how to pass a value.
Most AJAX requests are GET, and if your using it just for search, GET is usually fine. You set GET or POST when using your XHR object. If all this is too confusing, try a library such as jQuery (http://jquery.com)
Macha
Thanks for that Tony. It is somewhat confusing to me. I'm a server side kind of guy and js is just out of my league for now. I am using JQuery on some other stuff in this app and also downloaded a plugin for autocomplete but don't like it nearly as much as the one I have now. This one is at least 5x
faster than the JQ plugin. So, is what I am trying to do going to be difficult to pass into my query string? Is there a example that maybe you can point me to?
Read the basics of AJAX here (http://w3schools.com/ajax/ajax_browsers.asp) if you are doing it at a native level.
Macha
Thanks for the link Tony. I just went there but it doesn't look like what I have here. This script is using Domdoc.
That link is for native javascript. I.e, it will work regardless of what franework you use.
Macha
For what it's worth, calling form.submit() programmatically will NOT fire your onsubmit event handler.
Josh Stodola
+1  A: 

Take a look at jQuery. It is a javascript library. It contains functionality for doing Ajax.

Peter Stuifzand
A: 

You have two problems. One is that you are telling the form to submit:

document.forms.search.submit();

That is what is causing your form to submit in the standard, non-xhr way - causing a refresh. Also, because your form does not contain an input element for the id, that is not being sent to the server even with a regular form submission.

I agree with the posters that it would be a good idea to use jQuery or something to do your ajax based submission. Something like this could be used inside of your "AC.chooseFunc" function instead of the form submit.

And yes, if you go ajax entirely, you don't even need a form tag.

Russell Leggett