I am trying to parse a xml file that i have (using javascript DOM). and then i want to insert this data in a mysql table (using php).
I know how to parse xml data. But i am unable to figure out how to use php and javascript together and insert data in mysql table.
Kindly help me in this.
Best
Zeeshan
Why don't you just use PHP DOM to parse the XML ?
e.g:
$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();
After loaded your document $doc you can use the PHP DOM functions.
The JavaScript needs to send the data to the server. There are several ways this can be achieved.
Generate a form and submit it
Use XMLHttpRequest directly
Use a library like YUI or jQuery
Once there, you get the data (from $_POST for example) and insert it into MySQL as normal.
You can use jQuery to do this, for example:
<house>
<roof>as</roof>
<door>asd</door><window number="12">iles</window></house>
$('path_to_xml',xml).each(function(){
roof = $('roof',this).text();
door = $('door',this).text();
num_wind = $('window',this).attr('number');
});
Related
I can convert javascript functions to php to use it on server side through:
in JS:
$("#containerphp").load("save.php", {firstValueContainer:firstValue, secValueContainer:secValue});
in php (save.php):
$firstVal = $_REQUEST['firstValueContainer'];
$secVal = $_REQUEST['secValueContainer'];
It works fine but now I need to send a image variable (var image = canvas.toDataURL("image/png");) from javascript to php. I saw that the following code works to send it but in a form (without JS):
$_FILES['name']['tmp_name'];
Anybody knows how can I convert my image from JS to php in this context?
Thank you all.
You can use the following
var image = canvas.toDataURL("image/png");
and send it to php
$_FILE can not be used here because $_FILE is An associative array of items uploaded to the current script via the HTTP POST method.
convert javascript functions to php to use it on server side through:
$("#containerphp").load("save.php", {firstValueContainer:firstValue, secValueContainer:secValue, imageValue: image});
I'm creating an android app which takes in some json data, is there a way to set up a directory such as;
http://......./jsons/*.json
Alternatively, a way to add into a json file called a.json, and extend its number of containing array data, pretty much add more data into the .json file this increase its size.
It could be by PHP or Javascript.
Look into Parsing JSON, you can use the JSON.parse() function, in addition, I'm not sure about getting all your JSON files from a directory call, maybe someone else will explain that.
var data ='{"name":"Ray Wlison",
"position":"Staff Author",
"courses":[
"JavaScript & Ajax",
"Buildinf Facebook Apps"]}';
var info = JSON.parse(data);
//var infostoring = JSON.stringify(info);
One way to add to a json file is to parse it, add to it, then save it again. This might not be optimal if you have large amounts of data but in that case you'll probably want a proper database anyway (like mongo).
Using PHP:
$json_data = json_decode(file_get_contents('a.json'));
array_push($json_data, 'some value');
file_put_contents('a.json', json_encode($json_data));
I query the db i my model like so
function graphRate($userid, $courseid){
$query = $this->db->get('tblGraph');
return $query->result();
}
My controller gets data back from my model and I json encode it like so
if($query = $this->rate_model->graphRate($userid, $courseid)){
$data['graph_json'] = json_encode($query);
}
$this->load->view('graph', $data);
And thats returns me a json object like so
[
{"id":"1","title":"myTitle","score":"16","date":"2013-08-02"},
{"id":"2","title":"myTitle2","score":"17","date":"2013-09-02"},
{"id":"3","title":"myTitle3","score":"18","date":"2013-10-02"}
]
In my view graph I'm loading an js file
<script type="text/javascript" src="script.js"></script>
Now I want to use $data that is being sent from my controller to my view, to my external script.js to use as labels and data to feed my chart. But How do I get that Json data to my external script.js so I can use it?
1 more thing about the json data, isn't it possible to get the output of the json data as
{
"obj1":{"id":"1","title":"myTitle","score":"16","date":"2013-08-02"},
"obj2":{"id":"2","title":"myTitle2","score":"17","date":"2013-09-02"},
"obj3":{"id":"3","title":"myTitle3","score":"18","date":"2013-10-02"}
}
The problem isn't a Codeigniter problem, it's a javascript scope/file inclusion/where-do-i-get-my-data-from problem.
I run into this all the time and have used these solutions:
naming my php files with .php extensions and loading them as if they're views.
Just putting the script that needs data from a view IN the view file where it's used
Using an ajax request in my included js file to hit a controller and get json data.
I use #2 most frequently (for things like datatables where I WANT the js code right there next to the table it's referencing.
I use #1 occasionally, but try NOT to do that because it means some .js files are in my webroot/js dir and some are in teh application/views directory, making it confusing for me or anyone else who wants to support this project.
#3 is sometimes necessary...but I like to avoid that approach to minimize the number of requests being made and to try to eliminate totally superfluous requests (which that is).
You need to print the result of the output json string to the html generated file.
But you need to parse the string with some script. I would recommend you: http://api.jquery.com/jQuery.parseJSON/
For the second question. It is possible by doing:
$returnValue = json_encode(
array (
"obj1" => array("id"=>"1","title"=>"myTitle","score"=>"16","date"=>"2013-08-02"),
"obj2" => array("id"=>"2","title"=>"myTitle2","score"=>"17","date"=>"2013-09-02"),
"obj3" => array("id"=>"3","title"=>"myTitle3","score"=>"18","date"=>"2013-10-02"),
)
);
Print the output using PHP like:
echo json_encode($query);
Then from the client-side (where JavaScript resides) load that JSON that you printed using PHP. This can be done easily using JQuery.
Like this:
$.get("test.php", function(data) {
alert("Data Loaded: " + data);
});
You can find more information about this here: http://api.jquery.com/jQuery.get/
Now you'll need to parse this data so that JavaScript can understand what you got as text from the server. For that you can use the JSON.parse method on the "data" object in the aforementioned example. Once parsed, you can use the object like any other object in JavaScript. You can find more information about JSON.parse here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
I hope that helps.
Does anyone know how I could get the select options in the following to be populated from a SQL DB?:
var cell2 = row.insertCell(1);
var sel = document.createElement('select');
sel.name = 'selRow' + rowCount;
sel.options[0] = new Option('text zero', 'value0');
sel.options[1] = new Option('text one', 'value1');
cell2.appendChild(sel);
Thanks,
B.
Either take the long and inelegant route of outputting each of the sel.options[x] lines when the page is generated, or if you want to do it based on other information on the form, using AJAX is the way.
Javascript can't directly connect to a database, you're going to need some server-side (PHP, ASP, Coldfusion, etc.) code to do that.
UPDATE:
1. In PHP create a page that reads the database & outputs XML or JSON or even the HTML code itself
2. In Javascript make an AJAX call to that PHP page
3. Use Javascript parse the XML/JSON/HTML response to update the page.
Using a library like JQuery will make this much easier to code. Let's assume your generating the HTML code in PHP & using JQuery, you could make & parse the AJAX call like this:
$.get("YourPHPPage.php", function(data){
$('.DynamicSelect').html(data);
});
That will make an ajax call to the PHP page, and insert the result into your select box.
Here's something I want to learn and do. I have a JSON file that contains my product and details (size, color, description). In the website I can't use PHP and MySQL, I can only use Javascript and HTML. Now what I want to happen is using JQuery I can read and write a JSON file (JSON file will serve as my database). I am not sure if it can be done using only JQuery and JSON.
First thing, How to query a JSON file? (Example: I would search for the name and color of the product.)
How to parse the JSON datas that were searched into an HTML?
How to add details, product to the JSON file?
It will also be great if you can point me to a good tutorial about my questions.
I'm new to both JQuery and JSON.
Thanks!
Since Javascript is client side, you won't be able to write to the JSON file on the server using only Javascript. You would need some server side code in order to do that.
Reading and parsing the JSON file is not a problem though. You would use the jQuery.getJSON function. You would supply both a url and a callback parameter (data isn't needed, because you're reading a file, so no need to send data). The url would be the path to your JSON file, and the callback would be a function that uses the data.
Here's an example of what your code might look like. I don't know exactly what your JSON is, but if you have a set called "products" containing a set of objects with the details "name" and "price", this code would print those out:
$.getJSON("getProductJSON.htm",
function(data) {
$.each(data.products, function(i, item) {
var name = item.name;
var price = item.price;
// now display the name and price on the page here!
});
},
);
Basically, the data variable in $.getJSON makes the entire contents of the JSON available to you, very easily. And the $.each is used to loop over a set of JSON objects.