Create XML with php variables in javascript - javascript

I am wanting to create and save, to the server, an xml file. I understand I should use javascript but am not sure exactly how to code it.
Scenario:
I have a php page with variables, $firstname and $lastname for example.
So in the php:
$firstname = "John";
$lastname ="Doe";
I want to have a javascript that when fired will create and save an xml file.
I found the below post and it got me headed in the right direction.
How to save XML using PHP
Can anyone help me. I greatly appreciate some direction.
Additionally is it possible to have it outputjust like :
<?xml version="1.0" encoding="UTF-8"?>
<form1>
<fname>John</fname>
<lname>Doe</lname>
</form1>

Javascript is a client-side technology, PHP is server side. The question you linked is a good place to start, you'll want to use DomDocument and/or simplexml. If there is client side interaction, you'll still have to post the data to the server and store the file there.

Related

PHP to generate JSON for D3

I have a D3 graph which reads a JSON file. Where I am struggling is, what is the mechanism to get the dynamically generated PHP JSON data into the D3 graph.
I think the obvious solution from someone like myself with limited knowledge would be to have PHP write a JSON file to a directory on load, and then have the D3 graph look for it.
However, I feel like there is a better way to do it without writing a json file and reading a json file.
Could anyone please suggest the industry standard method for something like this, so I can continue my reading.
Just request the PHP script from D3 like it is a JSON file. Then in your PHP script...
$data = array('your', 'data', 'here');
header('Content-Type: application/json');
echo json_encode($data);
exit();

"Unexpected token <" when using <?php in a Javascript file

Being new to this, I'm trying to pass a variable from PHP to Javascript.
In my php page, I use a test variable:
$testval = "x";
In my .js file:
var exarr = <?php echo json_encode($testval); ?>;
I've tried several things, but it seems I always get Unexpected token < when I include "
What am I doing wrong ?
Thanks !
In order to use PHP code in any file, the web server has to run that file through the PHP processor. This is configured to happen by default with .php files, but not with .js files.
You could configure your server to process .js files as PHP, but that's generally unwise. At the very least, it creates a lot of unnecessary overhead for those files since most of them won't (or shouldn't) have PHP code.
Without knowing more about the structure of what you're trying to accomplish, it's difficult to advise a "best" approach. Options include (but may not be limited to):
Defining that one var on a PHP page which references the JS file, thereby making it available to the JavaScript code.
Putting the value in a page element somewhere that it can be accessed by JavaScript code, either as a form value or perhaps a data value.
Making an AJAX request to the server to get that value (and other values) after the page has been loaded.
If you have that in a js file (like somefile.js) then PHP isn't going to parse that file by default. In the PHP file that links to that JS you can output a script tag and the var you want like:
echo "<script>var exarr = " . json_encode($testval) . "; </script>";
And make sure your script is linked in after that code;
.js files are not compiled by PHP. The easiest workaround is to put the Javascript in a <script> block within a .php, but you're making one of the most basic of serverside/clientside mistakes and should rethink your entire approach.

Escape <?php ... ?> on javascript file

I'm busy doing server side and client side validation for magento. this validation works fine on server side (php)
on the client side i am using javasrcript.
When i started on this. i had my javascript embedded on a phtml file and everything was working as expected.
because i am using magento so I decided to inject the javascript file via page.xml
When I added the javascript code instead of getting the message pulled I get the php as is.
Here is my javascript:
function DefaultAddressErrorChangeNotAllowedMessage() {
alert("<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>");
return;
}
I run this when a user hit the onclick it will point to this function DefaultAddressErrorChangeNotAllowedMessage()
and the
<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>
will be populated as is.
but when I embed this directly to a phtml file it pull the correct message.
I there a way for javasrcipt that I can use to escape the php and get the correct message which is pulled from config.xml
PHP is rendered server side only. If you need to "inject" PHP specific values to your javascript, then you either need to render the actual value as part of the output of the php script, or you need to take a new roundtrip to the server, using Ajax.
Javascript is clientside, PHP is server side, so all php has been evaluated when javascript is loaded. This means, you can alert php echos, but you can't run PHP operations or any PHP logic in Javascript. You need ajax for this.
sorry for my clumsy answer, but maybe you lost the simple things.
I see that your javascript contains php tag, so i think you should insert your javascript code into .php extension because .js extension can't recognise the php tag.

jQuery + Ajax solution for "browsing" very large files

I've got some potentially very large (multiple GB) text files that I'd like to be able to view within a div.
In concept it's not hard... make an AJAX request for a more sensibly sized chunk of file, the AJAX script (PHP would be nice) seeks to an intelligent position within the file, and sends me what I need, which is then rendered vis JS in to an element.
This can't be a new problem, and I'm hoping someone else has already solved it.
Is there such a bit of Javascript (jQuery preferably) with related server scripts already out there somewhere?
I don't know of any library which does this. But it shouldn't be too hard doing yourself. The server side is almost trivial:
<?php
$chunkSize = $_GET['chunksize'];
$offset = $_GET['offset'];
$handle = fopen("text_file.txt", "r");
fseek($handle, $offset);
$chunk = fread($handle, $chunkSize);
fclose($handle);
header('Content-Type: text/plain');
echo $chunk;
?>
This is without any error handling, but that shouldn't be too much hassle as well.
Now, for the client side, there are of course some subtleties, as you will need to have two chunks in the text field at once in order to provide a "smooth" experience, but it should be doable.

How Can I scrape the Google Keyword Tool?

I want to extract data from the Google keyword tool - https://adwords.google.com/select/KeywordToolExternal. Their site is in Javascript, the script I've been working on is in PHP. Anyway to do this?
Alternatively, if there is no good solution, I was thinking of downloading the csv file and extracting data from it. Unfortunately, I've never done something where I download a csv file then extracted data from it. Can anyone point me in the right direction?
Scraping the Keyword tool is not easy (and it breaks the terms and conditions).
If you download the CSV file you can read it with php (it is a plain text file). You will have to create the logic to use the data in the file.
this will get you started
$file_handle = fopen("myfile", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
}
fclose($file_handle);

Categories

Resources