SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to submit form [POST] with AJAX?

    I need to submit a form [select input] with AJAX, how can this be done?

    Thanks
    - Tomer

  2. #2
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. When opening a request, use POST instead of GET.
      ajx.open('POST', url, true);

    2. Send the request header after opened a connection.
      ajx.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    3. Send the post data as URL-Encoded string instead of null.
      objz.send('name1=value1&name2=value2');

  3. #3
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thanks, but I want to send the select form value on step #3, not plain text, so how would I do that?

    objz.send('name1=' + $_POST[value]);

    See what I meant, I need to pass the form value, I've tried doing this:

    objz.send('name1=' + document.myform.selectName.value);

    But that didn't work, any ideas? [myform = the form's name; selectName = the name of the select that I want his value]

    Thanks so much! SP rocks!
    - Tomer

  4. #4
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    objz.send('name1=' + document.myform.selectName.options[document.myform.selectName.selectedIndex].value);

  5. #5
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I have:

    function submitPost() {
    http.open('POST', '/admin/recent.php', true);
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = handleResponse;
    http.send('length=' + document.myform.length.options[document.myform.length.selectedIndex].value);
    }

    That correct? What am I supposed to write instead of "selectedIndex"? The whole point for this is to see what option in the select was choosen.

    - Tomer

  6. #6
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and then, how can i submit my form with multipart/form-data?
    i'm trying to submit $_FILES value from my file in POST Data
    <input id="image" type="file">

  7. #7
    winter is around the corner Tomer's Avatar
    Join Date
    Jul 2005
    Location
    Israel
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't use AJAX to upload files. I remember reading about that somewhere.

    Anyone know the answer to my problem?

    - Tomer


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
  •