I am creating an application where a users draws shapes on a canvas and then saves and retrieves them from a database. The saving part works fine, now though, im trying to load this XML content, Thats where the troubles are starting.
Firstly a user has a list of documents they have created, when clicked it loads that document into the applicaiton to do this, i use the following code, firstly a javascript function which takes the ID of the document, then sends it to a PHP script which retrieves that documents data from a database. The PHP script than loads that documents data into a $_SESSION['data'] variable. Once done, it goes back to the javascript function which redirects the user to application page.
function loadDocument(docID){
$.ajax({
url: "load_a_document.php",
type: "POST",
data: {
documentID: docID,
},
success: function(data)
{
alert(data); //THIS DISPLAYS THE XML WITH NO PROBLEMS???
window.location = "application.php";
}
});
};
The PHP queries the database and retreives the name and XML content of the document, it then does this:
$_SESSION['document_Name'] = $doc_NAME;
$_SESSION['document_XML'] = $doc_DATA;
echo($_SESSION['document_XML']); //this is 'data' on the ajax success call
Now when the PHP is finished it echoes the php context, this shows up in the alert box in the success:{} of AJAX with no problems. Now it takes the user to the actual application which begins like so:
<?php
session_start();
$document_Name = $_SESSION['document_Name'];
$document_Data = $_SESSION['document_XML'];
?>
<script>
alert(" <?php echo $document_Name; ?> "); //WORKS FINE
alert(" <?php echo $_SESSION['document_Name']; ?> ") //WORKS FINE
//alert(" <?php echo $document_Data; ?> "); //STOPS THE PAGE LOADING
//alert(" <?php echo $_SESSION['document_XML']; ?> ") //STOPS THE PAGE LOADING
</script>
Fetching the first two items, there are no problems, as soon as XML data is printer then their is a real problem. I dont understand why the loadDiagram() can alert
() the XML but my application page cannot. Has the data been corrupted somehow?
Thanks for any feedback.
You probably have quotes in the string that's causing the problem. Try
alert(<?php echo json_encode($document_Data) ?>);
Related
I have php API with $_POST , where i am getting API key and QR code from the request.
Then my php server generates the html code with <div>, <img> <html> <body> etc.. it means full website - getting data from the mysql server and rendering to html page.
At the end of the php code i have javascript code like this:
?>
<script>
window.scrollTo(0, 0);
html2canvas(document.getElementById("SelectorToPrint")).then(function (canvas) {
var dd= canvas.toDataURL("image/jpeg",1);
$.ajax({
type: "POST",
url: "https://myserver/API/sendimg.php",
data: {
key: '<?php echo $key;?>',
barkod: '<?php echo $qrcode;?>',
img: dd
}
}).done(function(o) {
});
});
</script>
When i am displaying the page, the picture is being saved (using html2canvas js) , everything is OK.
The problem is, when i am calling this API request from another php code, the page is not displaying, and for this reason the picture is not rendered.
Is there some way to render html page and save a picture without displaying the generated html code?
I changed the code for a while - to not have $_POST , but $_GET and testing with this code:
<?php
$key='my_api_key';
$qrcode='my_code';
$test = file_get_contents('https://myserver/API/apipage.php?key='.$key.'&qrcode='.$qrcode);
//var_dump($test) ;
?>
when the var_dump is removed from the code, the page is not rendered and the picture is not saved. When i enable the var_dump (or simply print_r or echo ) the picture is generated correctly. The problem is, that i dont want to display the html code (i think it's normal when i am talking about API calls).
Thanks for any hint
OK so my solution was the following:
installed xvfb on my linux server
installed ssh2 for php
installed CutyCapt on my linux server
instead of calling and rendering picture directly - i am calling with specially created user (with privileges only to do this job) the following script directly from php API
"/usr/bin/xvfb-run --server-args='-screen 0, 1920x1080x24' /usr/bin/CutyCapt --url='{$url}' --zoom-factor=1.4 --out=/home/myuser/public_html/rendered_images/{$filename}.{$extension}"
so as i learnt, there is no easy way to do it, but it's doable
hope it helps for someone
I am currently using . . .
<form action="phpcall.php" method="POST">
.
.
.
</form>
to initiate my php file with a few values from an html form, and submission by a button click. This call is intended to store the entered values into MySQL DB, and generate a graph to be displayed on the html page.
Is there no way to use the data sent by the html form, to generate some data and display it within the html page without navigating away from the html page? As in the php file inserts pertinent data onto the html page (in this case an image, some simple database info and other various data).
I realize this is a fairly novice question, and this would almost be behaving like Javascript, but if I need this php file to log information to MySQL, the way to do so is not apparent to me. Perhaps setting up the html page as a php document would suffice -- therein reinstantiating all of the same html structures after each call -- but this doesn't seem elegant, and I from what I understand separate php and html files are the norm.
/e
My current "solution" is to have the .php have a mirrored format, and sort the variables from there.
You could use an Ajax call, similar to this:
<script>
jQuery(document).ready(function() {
$('form').submit(function() {
var data = $('form').serialize();
$.ajax({
type: 'POST',
url: 'phpcall.php',
data: data,
success: function(data) {
//do something here like display a success message
}
}
});
return false;
});
});
</script>
This would process the PHP file without reloading or navigating away form the page
I'm new to PHP and trying to figure this out and still not understanding. I'm trying to take the value of a html text box in jQuery and turn it into a variable I can then store as a (variable or string?) and pull back again after a page refresh.
I want the user to hit a button that then executes the following:
$('.save').click(function () {
// pulls the value from the textarea
var file = $('#subtext').val();
// send to php to hold text
$.ajax({
url: '/mininote/system/data.php',
type: 'POST',
data: { textbox: file },
success: function(data) {
alert('Saved data to php!');
$(save).text('All changes saved.').show().delay(3000).fadeOut(800);
}
});
});
Then receives the post data and stores in the php until the user reloads the page where it pulls data (or checks if there is any) from the php like so and replaces the value of the textbox with the value from the php:
$.ajax({
url: '/mininote/system/data.php',
type: 'GET',
data: { textbox: file },
success: function(data) {
// add text back to text box
$("#subtext").val(data);
}
});
Basically what I'm looking for is below:-
a way to perform an ajax POST to insert the data grabbed from the textbox,
add to PHP
on a page reload use a GET request and replace textbox text with text from the PHP file.
What would I need to put into the PHP code? Or would it be easier to go in another direction? I've gotten this method to work in local storage. I also want browser compatibility for this work.
I don't need to set it up for a bunch of users. Any response that will increase my knowledge on this will help greatly.
EDIT: I'm really looking for something more server-side so it's usable across multiple platforms and devices.
actually jquery-ajax works like below:-
it takes request and
it gives response.
For Your requirement You also need to follow this steps. so for this , You
send request to PHP page then
send response from php page
so replace Your above jquery-ajax code part with below:-
$('.save').click(function () {
// pulls the value from the textarea
var file = $('#subtext').val();
// send to php to hold text
$.ajax({
url: '/mininote/system/data.php',
type: 'POST',
data: { textbox: file },
success: function(data) {
alert('Saved data to php!');
$('#subtext').val(data);
$(save).text('All changes saved.').show().delay(3000).fadeOut(800);
}
});
});
make sure in Your data.php page textbox value has made echo after inserting data to DB. This process would be something like below:-
data.php
<?php
$textbox = $_POST["textbox"];
/*
perform Your DB inser part here
*/
if(data inserted to db)
{
echo $textbox;
}
?>
Hope this will help You.
You could use the following simple PHP script to accomplish your goal:
<?php
session_start();
if (isset($_POST['textbox'])) {
$_SESSION['textbox'] = $_POST['textbox'];
} else {
echo $_SESSION['textbox'];
}
?>
Another option would be to use HTTP Cookies. Just set the cookie with JavaScript using a plugin or something simple such as, document.cookie = "variable=value"; and access it in PHP with, $_COOKIE["variable"].
Hope this helps.
In the PHP code you could use a PHP session variable like so:
$_SESSION['saveMe'] = $saveMe;
Then get it later, even after a refresh, by using the session variable in php as you would normally use any variable.
For more info see http://www.w3schools.com/php/php_sessions.asp
Use a session, like a temporary cookie, like the following.
session_start();
$_SESSION['sessionName'] = $sessionVar;
Then you can destroy the session with, session_destroy();
See more here.
Why you send it to PHP ?
Just saving in client-side with cookie is better and access in PHP with $_COOKIE
Download jQuery.cookie from :
carhartl/jquery-cookie
and do :
$('.save').click(function () {
// save data from textarea to cookie
var data = $.trim($('#subtext').val());
$.cookie('TEXTAREA_VALUE', data);
});
and go to read by PHP :
<?php
echo $_COOKIE['TEXTAREA_VALUE'];
?>
and to remove :
$.removeCookie('TEXTAREA_VALUE', { path: '/' });
Okay friend !
Ajax is mainly used for sending data without reloading the webpage - from client(js) to serverside(php) or from serverside(php) to client(js). Make sure that name-attribute is given in the textarea and that method is set to post in the form. If I understand your issue correctly you could do something like this:
<?php
session_start();
if (isset($_POST['subtext'])) {
$_SESSION['subtext_value'] = $_POST['subtext'];
}
if (isset($_SESSION['subtext_value'])) {
$subtextValue = $_SESSION['subtext_value'];
}
else {
$subtextValue = '';
}
?>
<html>
<body>
<form action="/mininote/system/data.php" method="post">
<textarea id="subtext" name="subtext"><?php echo $subtextValue;?></textarea>
<input type="submit" value="ok" />
</form>
</body>
</html>
I am looking to use jquery to add to the DOM for an <ul>, I want to add more <li>'s into the list, but just to last for a session with php.
e.g.
$lists = $_SESSION['names']
//which contains an array of all the 'names', and then I would run a loop:
echo "<ul>";
foreach($lists as $list) {
echo "<li>$list</li>";
}
echo "</ul>";
So the user can add these variables into the list, during this session.
How would this be done, and how is the jquery able to interact with php like this? And what method would be easiest?
It seems like you misunderstand how PHP works.
PHP is a server side script. This means that it runs on a server and the user can't see it running or have the code appearing on his side.
jQuery is a javascript library. It is a client side helper for user interactions.
So basically there's no way to directly run PHP from an HTML document
But indirectly there is AJAX.
AJAX allows you to visit a page (and basically running a PHP script) without reloading your page.
http://api.jquery.com/jQuery.ajax/
Example
$.ajax({
type: "POST",
url: "some.php",
data: { list: jQueryVariableList
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
Now on the PHP side you can pick up the list using $_POST['jQueryVariableList']
Have a look at this:
In javascript:
jQuery('#div_session_write').load('session_write.php?session_name=new_value');
In session_write.php file:
<?
session_start();
if (isset($_GET['session_name'])) {$_SESSION['session_name'] = $_GET['session_name'];}
?>
In html:
<div id='div_session_write'> </div>
I am creating a PHP site and have ran into the following problem, i would like to be able to click a hyperlink on a page, send a small data item to a php script, have the script perform its function and then return the results.
I am using AJAX with this site to have each page load into a div section of the site, all pages, data and responses load into this central div, like so:
<html>
<body>
<div id="topMenuBar">
<div id="contents">
//ALL DATA IS LOADED HERE...//
</div>
</div>
</body>
</html>
So, when a page is selected from the top menu i simply have the following code run:
$('#topMenuBar a').click(function(e)
{
e.preventDefault();
$('#contents').load($(this).attr('href'), function()
{
});
});
Now that pages load into the content section, I am loading a page called "results.php" which connects to a DB, queries it, and the creates a HTML table with the results. Each table row has a small hyperlink which when clicked is intended to send a value to another PHP script and then clear the contents div and then repopulate the div with the response from this script (getInfo.php). For example, a row will have the following PHP code generate a link:
<label class="moreInfo"><a name="test01" onClick="getInfoFromPHP(<?php echo $data[$id]; ?> )">Get Info</a></label>
So when the table is generated by PHP it, the link when clicked passes a JS function the value.
What i now need to do is to send the value to a PHP script which will again query the DB, and have the results inserted into the "contents" div. I have been trying the following function.
function getInfoFromPHP(myVar){
var netID = myVar;
$.ajax({
url: "getInfo.php",
type: "POST",
data: {
networkID: netID
},
success: function(html) {
$('#contents').empty();
$('#contents').load(html);
}
});
};
When i call the function it does seem to send the data to the script but i get the following error from firebug:
POST http://127.0.0.1/private/networks/includes/leave_a_network.php - 200 OK -15ms
GET http://127.0.0.1/%3Ch2%3EHello 403 Forbidden 21ms
"NetworkError: 403 Forbidden - http://127.0.0.1/%3Ch2%3EHello" Hello
The PHP script is only doing the following:
<?php
session_start();
$networkID = $_POST['networkID'];
echo "<h2>Hello World</h2>";
?>
What is the best way to send data to a PHP script and have it load into a div?
Thanks for any feedback.
In the success function, put $('#contents').html(html); instead of load.
success: function(html) {
Is returning html as a string that does not need to be 'load'ed again like you try here -
$('#contents').load(html);
Change that line to this
$('#contents').html(html);
A better way is using $.ajax to send the request. On the success callback, you can analyse the result, and do what you want with :
http://api.jquery.com/jQuery.ajax/