SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Oct 30, 2007, 06:19 #1
- Join Date
- Jun 2006
- Posts
- 145
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Submitting form fields to ajax function
Hi there
I'm an experienced php developer but I'm only just delving into javascript a bit more. Basically, I'm making a site using google maps. I have a search form for entering a postcode, date and other various information. I'm using a javascript function and ajax to read in the xml repsonse that a php file generates. I pass the form field values to the php file via the url. Eg, reading the response from map_data.php?postcode="+postcode+"&date="+date etc etc.
Anyway, to cut a long story short, I would like to know the best way to submit the form data to the javascript function without loading the page. I currently have something like this at the bottom of the form:
Code:<input type="button" name="search" value="Search" onclick="getMarkers(postcode.value, date.value, event.value, venue.value);" />
Many thanks indeed
-
Oct 30, 2007, 22:42 #2
There is no reason to pass the values into your function, your function can retrieve them. It would be better to use a submit button, because then the user can just hit enter to submit your form. If you do this then instead of handling the onclick of the button, you handle the onsubmit of the form. Returning false will prevent the form from submitting.
HTML Code:<form onsubmit='return getMarkers();'> <!-- form elements --> <input type='submit' /> </form>
PHP Code:function getMarkers() {
//Get form values
//Validate
//Make Ajax request
return false;
}
Not one shred of evidence supports the notion that life is serious.
eternal.co.za - code, thoughts, rants and raves
f1rivals.net - formula 1 forums, and, hopefully, soon, prediction game
-
Oct 31, 2007, 03:13 #3
- Join Date
- Jun 2006
- Posts
- 145
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Mr Moo,
Yep, thats perfect thank you. I'd been reading up since my post yesterday and realised that is the best way forward to thanks for confirming it.
Cheers
Bookmarks