Krzysiek
Krzysiek

Reputation: 1527

best way to parse html using ajax

I am working on a page that lets users use buttons to insert relational algebra and returns a query result. My buttons create a structure using custom tags. Suppose below structure:

<proj>
    <attr>
        attributes
    </attr>
    <table>
        table
    <table>
</proj>

Once the structure is complete, I need some way to get the information about of it. I could use forms, but I do not want to reload the entire page with every query, so I thought about using AJAX, but I am not too familiar with it. I thought I could parse the structure using jQuery and send the variables to a php file using AJAX. Would this is a correct approach to this problem? Or am I overcomplicating this?

Upvotes: 0

Views: 121

Answers (1)

AlliterativeAlice
AlliterativeAlice

Reputation: 12577

Use jQuery.parseXML to parse the user's input into a document object and then you can access the individual attributes with find(), etc. Basically treating it the same way you'd treat an HTML document. For example:

var doc = $.parseXML(userInputStr);
$(doc).find('proj').each(function () {
    var attr = $(this).find('attr').text();
    //Whatever else you want to do
}

You could then post just what you need to post to the backend. Another alternative is to post the user's entire input and then parse it as XML in your backend code.

Upvotes: 1

Related Questions