0

I am trying to send an XML object to a database. I have some POST data for a form that I turned into an XML object that I am wanting to send to a database in a way that will allow for the form to have additional inputs created and that I can later call and use the XML object to populate the form in case users want to edit their form. How can I send this data in such a way? Any and all help would be greatly appreciated! Thanks to gfrobenius this is what I have for creating the XML object:

JQuery:

//Grab all the POST info, turn it into a valid XML object and store it
$postData = null;
if($_SERVER['REQUEST_METHOD'] == 'POST' && count($_POST) > 0) $postData = assocArrayToXML('POST_DATA',$_POST);
//The assocArrayToXML returns the XML object with page breaks, we need a stright non-breaking string
//so that the flexigrid can display the results properly.
$postData = str_replace(chr(13), '', $postData);
$postData = str_replace(chr(10), '', $postData);

Function:

 function assocArrayToXML($root_element_name,$ar)
    {
      $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
      $f = create_function('$f,$c,$a','
              foreach($a as $k=>$v) {
                  if(is_array($v)) {
                      $ch=$c->addChild(htmlspecialchars($k));
                      $f($f,$ch,$v);
                  } else {
                      $c->addChild($k,htmlspecialchars($v));
                  }
              }');
      $f($f,$xml,$ar);
      return $xml->asXML();
    }
2
  • Would not be easier to have an extra field on your table, to allow for different states Commented Jan 31, 2014 at 17:51
  • Well the problem is that their could be 35 input fields or there might be 100+ fields. So I was hoping to just put whatever the input is in an XML object and then send it the same way every time. Commented Jan 31, 2014 at 17:54

1 Answer 1

1

I'm not sure what an XML object is, but this is how I'd pass XML to a database.

I'd use ajax (jQuery or pure JS) to pass it from the client to some server code (webmethod in your page or web service) that hooks into your database that will pass the XML string as a parameter to a stored procedure or parameterized sql.

Should be about 20-30 lines of code and there are a million places you can find examples on the web on how to do this.

EDIT:

jQuery example: http://api.jquery.com/jquery.ajax/

JavaScript example: https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Okay, I will try to find something, I appreciate it.
@gv0029 I added some links. Don't forget to accept the answer if it helped you. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.