#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Assistance with understanding how to get a javascript array into database


    Hello all,
    I'm new to PHP and trying to get data to post to a database via an api. I read the documentation, however I'm kind of stuck on 2 things, 1, is this api asking for the data to be formatted via xml format see: "$set_contents = ". 2, is it possible to accomplish the same thing with javascript?

    Code:
    <?php
    	// Confidential: POSLavu API Documentation
    	// EXAMPLE FOR INSERTING AN ORDER USING THE POSLAVU API.
    	
    	function post_to_api($postvars)
    	{	
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, "https://admin.poslavu.com/v2/reqserv/");
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    		curl_setopt($ch, CURLOPT_POST, 1);
    		curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    		$response = curl_exec($ch);
    		curl_close ($ch);
    		
    		return $response;
    	}
    	function get_tag_contents($tag,$str)
    	{
    		$contents = array();
    		$str_parts = explode("<".$tag.">",$str); // look for start tag
    		for($i=0; $i<count($str_parts); $i++)
    		{
    			$current_parts = explode("</".$tag.">",$str_parts[$i]); // look for end tag
    			if(count($current_parts) > 1) // full tag found, get contents
    			{
    				$contents[] = $current_parts[0];
    			}
    		}
    		return $contents;
    	}
    	
    	$api_url = "https://admin.poslavu.com/cp/reqserv/";
    	$api_dataname = "(dataname here)";
    	$api_key = "(key here)";
    	$api_token = "(token here)";
    
    	//----------------------------------------- Add the order
    	$set_contents = "
    	<row>
    		<opened>2011-09-06 16:10:00</opened>
    		<closed>0000-00-00 00:00:00</closed>
    		<subtotal>14.25</subtotal>
    		<taxrate>0.000</taxrate>
    		<tax>0.98</tax>
    		<total>15.23</total>
    		<server>Ancori</server>
    		<server_id>15</server_id>
    		<tablename>Table 5</tablename>
    		<cash_paid>5.50</cash_paid>
    		<change_amount>0</change_amount>
    		<cash_applied>5.50</cash_applied>
    		<card_paid></card_paid>
    		<gift_certificate></gift_certificate>
    		<guests>2</guests>
    	</row>
    	";
    	$postvars = "dataname=$api_dataname&key=$api_key&token=$api_token";
    	$postvars .= "&cmd=insert&table=orders&contents=".$set_contents;
    	$response = post_to_api($postvars);
    
    	// When creating an order the api will return the following info:
    	// <result><id>id of the new entry</id><order_id>the order id for the new order</order_id></result>	
    
    	$order_id_list = get_tag_contents("order_id",$response); // the api will assign the order_id for the new 
    	if(count($order_id_list) > 0)
    	{
    		$order_id = $order_id_list[0];
    		
    		//----------------------------------------- Add the order contents
    		$set_contents = "
    		<row>
    			<sent>0</sent>
    			<order_id>" . $order_id . "</order_id>
    			<item>Nanbot</item>
    			<price>5.5</price>
    			<quantity>1</quantity>
    			<options></options>
    			<special></special>
    			<modify_price></modify_price>
    			<check>1</check>
    			<seat>1</seat>
    			<item_id>814</item_id>
    			<printer>1</printer>
    			<apply_taxrate>0</apply_taxrate>
    			<custom_taxrate>0</custom_taxrate>
    			<course>1</course>
    			<subtotal>5.5</subtotal>
    			<discount_amount></discount_amount>
    			<discount_value></discount_value>
    			<discount_type></discount_type>
    			<after_discount></after_discount>
    			<subtotal_with_mods>5.5</subtotal_with_mods>
    			<tax_amount></tax_amount>
    			<notes></notes>
    			<total_with_tax></total_with_tax>
    			<after_gratuity></after_gratuity>
    			<void>0</void>
    			<discount_id></discount_id>
    			<server_time>2011-10-10 12:49:16</server_time>
    			<device_time>2011-10-10 11:50:12</device_time>
    		</row>
    		<row>
    			<sent>0</sent>
    			<order_id>1-2</order_id>
    			<item>Columbian</item>
    			<price>3.5</price>
    			<quantity>2.5</quantity>
    			<options></options>
    			<special></special>
    			<modify_price></modify_price>
    			<check>1</check>
    			<seat>1</seat>
    			<item_id>824</item_id>
    			<apply_taxrate>0</apply_taxrate>
    			<custom_taxrate>0</custom_taxrate>
    			<course>1</course>
    			<subtotal>8.75</subtotal>
    			<discount_amount></discount_amount>
    			<discount_value></discount_value>
    			<discount_type></discount_type>
    			<after_discount></after_discount>
    			<subtotal_with_mods>8.75</subtotal_with_mods>
    			<tax_amount></tax_amount>
    			<notes></notes>
    			<total_with_tax></total_with_tax>
    			<after_gratuity></after_gratuity>
    			<void>0</void>
    			<discount_id></discount_id>
    			<server_time>2011-10-10 12:49:17</server_time>
    			<device_time>2011-10-10 11:50:23</device_time>
    		</row>
    		";
    		$postvars = "dataname=$api_dataname&key=$api_key&token=$api_token";
    		$postvars .= "&cmd=insert&table=order_contents&contents=".$set_contents;
    		$response = post_to_api($postvars);
    	
    		//-------------------------------------------- Add order payments	
    		$set_contents = "
    		<row>
    			<order_id>" . $order_id . "</order_id>
    			<check>1</check>
    			<amount>5.50</amount>
    			<transaction_id></transaction_id>
    			<datetime>2011-10-10 11:50:38</datetime>
    			<pay_type>Cash</pay_type>
    			<total_collected>5.50</total_collected>
    			<server_name>Ancori</server_name>
    			<action>Sale</action>
    			<server_id>15</server_id>
    			<server_time>2011-10-10 12:49:26</server_time>
    		</row>
    		";
    		$postvars = "dataname=$api_dataname&key=$api_key&token=$api_token";
    		$postvars .= "&cmd=insert&table=order_payments&contents=".$set_contents;
    		$response = post_to_api($postvars);
    	}
    ?>
    script?



    Any help would be greatly appreciated.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I don't understand your question. Are you asking if you can do the POST request with JavaScript instead of PHP? No, the same origin policy forbids that. Or are you somehow trying to replace the XML with JavaScript? That wouldn't even make sense.

    By the way, do not use that code. It's kind of funny, but obviously the API "developers" have no idea how to properly parse/generate XML and when to use URL encoding. So you're certainly better off reading up on SimpleXML (or any other library) and writing the code yourself.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Thanks Jacques,

    I guess what I'm trying to do is I have a web app that posts to the php file using xhr request, and when I get to the php file, one thing I don't understand if I do a xhr.send(params) to the php file how do I retrieve that data? Is it using a '$_POST['params']' or '$_POST' tag?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by JasonPowell8
    [...] if I do a xhr.send(params) to the php file how do I retrieve that data? Is it using a '$_POST['params']' or '$_POST' tag?
    The POST parameters are transmitted as they appear in the "params" string. For example, if you had
    Code:
    var params = 'id=123&foo=bar';
    then the PHP script would receive the parameters "id" and "foo" and place them in the $_POST array -- it works just like any form submission.

    To inspect the POST parameters, just do a var_dump($_POST); in your PHP script.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Ah much thanks! So I'd just put the post parameters into an array if for what I'm trying to do! Thanks again!



    Originally Posted by Jacques1
    The POST parameters are transmitted as they appear in the "params" string. For example, if you had
    Code:
    var params = 'id=123&foo=bar';
    then the PHP script would receive the parameters "id" and "foo" and place them in the $_POST array -- it works just like any form submission.

    To inspect the POST parameters, just do a var_dump($_POST); in your PHP script.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    The parameters are in an array, namely $_POST.

IMN logo majestic logo threadwatch logo seochat tools logo