SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    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);" />
    What is the bst way to first validate the data submitted and then pass the values to my function which retrieves the data from the database?

    Many thanks indeed

  2. #2
    SitePoint Enthusiast Mr Moo's Avatar
    Join Date
    Oct 2007
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Zealot
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •