Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 9 of 9

Thread: Sending a DOM object to PHP over Ajax

  1. #1

    Sending a DOM object to PHP over Ajax

    Hey guys,

    I've got a DOM object containing an entire form, and I need to send the entire object to PHP through ajax. It's important to maintain the values of each input field.

    I don't know what fields are going to be in the form, and that is why I must send the entire form. If there is an alternative, I'm open to it, but I'd prefer to receive xml into the PHP file.

    So far, I have something like this:

    [highlight=javascript]xmlHttp.open("POST",'filename',true);
    xmlHttp.send(document.getElementById('element_id') );[/highlight]

    Is there any method to convert a dom object to a string or xml?

    Thanks

    Note: This is a commercial project, and I am unable to use any open source software, so please don't post any ajax libraries.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  2. #2
    176
    posts
    Registered User
    well what about

    var xmlDOM=document.getElementById('element_id').inner HTML;
    xmlHttp.send(xmlDOM)


    cuz as long as the form is well formed XHTML you should have no problem converting innerHTML into XML, no?
    PHP Code:
    <zip> echo this; </zip

  3. #3
    innerHTML doesn't work because it does not return the data values in each element. For example, when I run innerHTML, it'll get an input tag, but the value inside is not preserved, therefore making it, well, useless.

    Any other ideas?


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  4. #4
    .value, replacing .innerHTML, would give you the input data.

    As for sending multiple input data entries.. that could take some work. You can probably use the DOM as an xml object, and look for the next node in terms of the input values. The javascript for that could be a bit complicated, as it would have to parse HTML on the fly.
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  5. #5
    4,029
    posts
    home cooking is killing the restaurant industry
    Can't you use getElementsByTagName to find all the input, select and textarea elements? Give them all a unique ID and send that.
    There are only 10 kinds of people in this world:
    Those that might know ternary, those that do, and those that don't
    Say NO to DRM.

  6. #6
    You could… but it'd be a bit messy. I suppose I'll probably have to do that in the end.

    Thanks, though.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  7. #7
    176
    posts
    Registered User
    Quote Originally Posted by nokrev
    innerHTML doesn't work because it does not return the data values in each element. For example, when I run innerHTML, it'll get an input tag, but the value inside is not preserved, therefore making it, well, useless.

    Any other ideas?
    Yes ... use onchange to set the value attribute ...

    HTML Code:
    <script language="javascript">
    function makeValue(obj){
      var v = document.createAttribute("realValue")
      v.value = obj.value;
      obj.setAttributeNode(v);
    }
    </script>
    ...
    <input type="text" name="Name" onchange="makeValue(this)" />
    This should add an attribute to your input named realValue with the value of the input field. And when you do the innerHTML it will have the value

    Don't know if it will actually work ... just an idea
    PHP Code:
    <zip> echo this; </zip

  8. #8
    176
    posts
    Registered User
    Proof of Concept:
    http://www.solidstructures.net/checked.htm

    Enter values into the fields then click and you will see the source of the form.
    (*Note output source is formatted so it comes out pretty ... but actual inner HTML is good)

    also trying out a handy HTML Printer-Outer
    Last edited by antizip; January 23rd, 2006 at 06:59 PM.
    PHP Code:
    <zip> echo this; </zip

  9. #9
    Thanks a bunch. It works like a dream.

    Oh, and great idea!


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012