I have xml page contents in a javascript string. I need to save this into my.xml in the server side using php. But i can't seem to pass the xml-content-string value via POST method. I tried file_get_contents and curl but figured out that's not what I want since I have my xml content in string format and not in a file.
Can anyone help me to save this string in javascript as proper xml content.
Thanks in advance
Pre
I am attaching my code below
Page1.php
<form name = "frm_first" method = "POST">
<input type = "hidden" name = "xml_string">
...............
<script type = "text/javascript" language = "javascript">
function getDataXML(){
xmlString = chart.getDataXML();// This is where I get my xml data into the string
document.frm_first.xml_string.value = xmlString;
alert(xml_string); //Good
document.frm_first.submit();
}
</script>
Page2.php //This is the page to which post is submitted.
<?php
echo print_r($_POST);//xml_string vaiable is empty. Other variables are getting displayed.
$domtree = new DOMDocument('1.0','UTF-8');
$domtree->loadXML($_POST['xml_string']);// Error :empty string supplied as input
$domtree->save("my.xml"); //Yes , I have access to the file.
?>
$_POST['xml_string'] is empty is my issue. Other variables are getting passed via POST.
Adjusting the header with PHP did the trick when i tried to save a XML file:
header("Content-Disposition: attachment; filename=file.xml");
But sending the XML should be possible as string. Do you get any error messages?
Sorry to be answering my own question, but thought that it might be of use to someone else too.
I did a if array_key_exists('xml_string', $_POST)
<?php
if array-_key_exists('xml_string', $_POST)
{
echo print_r($_POST);
$domtree = new DOMDocument('1.0','UTF-8');
$domtree->loadXML($_POST['xml_string']);
$domtree->save("my.xml");
?>
and now $_POST variables are coming in properly.
Related
I'm trying to create a js that send data to a php.
My first problem is that I get get back a html code if I insert this to the php.
This is only for understand the idea. So the js should send the "param" to the php and the php should return the result6 variable in this case, but I get a html code...
$('#f_field').change (function()
{
var param = 28;
$.post('check_mutet.php', param, function(result6) {
alert(result6);
});
});
while check_mutet.php contains this
<?php
$result6=666;
echo $result6;
Thank you for your help, as you can see I'm rather noob :)
param is a plain string (it starts out as a number, but will be converted to a string by the time it gets through to HTTP).
This will be available as STDIN in PHP, which you can read as described in answers to this question.
However, you should encode the data into a structured format that is easier to use.
The traditional format for this is application/x-www-form-urlencoded, which is the default format sent by HTML forms.
If you pass an object to jQuery post, it will encode the data in that format for you:
var param = 28;
var data = { example: param };
$.post('check_mutet.php', data, function(result6) {
Then you can read it through the POST superglobal in PHP:
<?php
$result = $_POST['example'];
I am out of hope for this question to which the answer will finally complete my project.
I am using a signature plugin/API ( https://github.com/szimek/signature_pad ) as one of the input fields in a form in the first file form.php.
When the form is submitted I am sending the data to a database and retrieve useful data. this useful data is seen in the second file success.php.
I have successfully gotten the all data except the signature.
The signature is not an <input> but a <canvas> because of the plugins
when the form is submitted a javascript file sends the signature toDataURL a function from the documentation of signaturepad.js. Currently I have send it to a window.open new window. The url shows a data64 text which when converted is a image.
The problem is accessing this dataURL from success.php. When I can to this I can also send it to a database and send it within the automatic mail.
how do I get my signature to the second file as an image?
this is my code:
form.php
<form id="form">
<canvas id="signature"></canvas>
form.js
var signature = $("#signature");
var signaturePad = new SignaturePad(signature);
$("#form").submit(function(e) {
signaturePad.toDataURL(); )};
success.php (my idea how it should work)
<?php $image = $_POST['signaturePad']; ?>
<?php echo "<img src='"$image"'>";
success.php within SQL statements
$sql = "INSERT INTO $table ($field) VALUES ($image)";
I have also tried other options and API/plugins but I think this would be the best solution.
Hello and good day everyone! I am here to ask on how to pass a file type using javascript, so that I can use it to another php page. The following is an input for type file:
<input type="file" name="imgCompany" id="imgCompany">
I am getting the file like this in php:
$file = $_FILES['imgCompany']['tmp_name'];
The first question I have to ask is how can I get the $file variable to be place in a javascript function? (same php page). So far this was my attempt to do so:
function addImage() {
var companyImage = $('#imgCompany').val();
}
The second question I have to ask how do I pass that file value once it is declared in the addImage() function using a method like this:
$.post('addImage.php', {postimage:file},
function(data)
{
//returns the echoed value from the php file
$('#result').html(data);
});
Or is there another way?
I am going to use what I pass from the post to another php page, so I can upload that image into a database. I am uploading this image into a database like this:
...
$image = addslashes (file_get_contents($_FILES['image']['tmp_name']));
//$image_name = addslashes ($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size == FALSE)
echo "<h3 class='warningStyle' align='center' style='position:absolute; top:235px; left:660px;'>No Image was Selected</h3>";
else {
if (!$insert = $db->query ("INSERT INTO company VALUES('','$companyName','$image')"))
echo "<h3 class='warningStyle' align='center' style='position:absolute; top:235px; left:650px;'>Problem uploading the image</h3>";
else {
echo "<h3 class='successStyle' align='center' style='position:absolute; top:235px; left:620px;'>Company Account successfully created</h3>";
}
}
I cannot read JQuery (nor do I want to), but here are a few pointers:
The file name in file-upload: javascript-how-to-extract-filename-from-a-file-input-control
The second question I have to ask how do I pass that file value once it is declared in the addImage() function using a method like this:
You don't have to, the file will be send in the form you are sending.
I do not understand what you are trying to accomplish by adding javascript to that. The original filename will also be send to the server. (Inspect the $_FILES on arrival to see it for yourself)
About the insertion in the database:
addslashes? Really? Escape it in another way, preferable with the quotation/escape functionality delivered with your database.
And another thing: If you go to "another page" your $_FILES will be empty. Handle it on arrival, not after invoking a new page. I am not sure what or how you are doing it, but keep that in mind when you see an empty $_FILES while you expected some information in it. :-)
Here is the Javascript AJAX code:
var anything=document.getElementById("textarea").val;
I have Javascript AJAX implemented:
httpRequest.open('POST',anyURL,true);
http.send(anything);
Now, my question is since this is the post data, and this is not actually associated with any html tag or id, how do I receive this in php?
<?php
$parameter=$_POST[' '];
?>
What should I put in the white space there?
Add a new variable and use JSON.stringify on your anything variable that was turned into a key value pair.
var anything={myTextArea: document.getElementById("textarea").value};
var JSONstr = JSON.stringify(anything);
PHP:
<?php
$parameter=$_POST['JSONstr'];
?>
I have been reading how to pass variables from a php webpage to a separate javascript file, but am not having any luck.
Please don't mark this as duplicate, as I know there are a ton of things out there telling the ways to do this. I recognize those posts and am more just checking my syntax or seeing if there is anything unique about my specific situation that is causing those methods not to work.
So I have a PHP webpage where I POSTed some variables to:
DOCTYPE HTML
...
<?php
$id = $_POST["id"];
$name = $_POST["name"];
?>
...
HTML code with some usage of PHP variables
javascriptFunction()
end page
Then in a separate javascript file I have:
var markerlocation = '<?php echo $point; ?>';
function javascriptFunction () {
alert("markerlocation");
});
This seems really straight forward, but for whatever reason I can't get it. I also have tried with json encode.
I can delete this when done.
Sincere thanks for any help.
My way:
Declare a Array to store variable for passing variable to JavaScript
Encode the Array to JSON in php
Decode the JSON String from php and store as a JavaScript variable
PHP
<?php
//You may declare array for javascript
$jsVal = array(
'user_name' => 'Peter',
'email' => 'peter#gmail.com',
'marker_location' => '102,300'
);
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>var phpConfig = jQuery.parseJSON(<?=json_encode($jsVal)?>);</script>
Separated javascript file
alert(phpConfig.marker_location);
You can try it
You can point the script tag source to a .php file instead of a .js file, I think that's what you want. Works with image tags too ;)
Edit: some browsers may require the text/javascript mime header in order for it to work properly, but it's not hard with PHP I'll assume you already know how to do that.
On a side note: This option should probably only be used if you're planning on allowing the client to cache the javascript output. If you don't want the client to cache it, you need to set additional headers.