Get the Ace editor content using PHP - php

Hey guys I was wondering if it's possible to get the content within the tags I have using PHP. I'm using the Ace editor and I have a save button. I'm trying to save the content to a file using PHP, can someone tell me how I can get the content using PHP please?

May you can save it via AJAX,
send the editor content and desired filename (and path also) using POST method
example
function saveCode () {
$.ajax({
url: 'handler.php',
type: 'POST',
dataType: 'html',
data: {code: editor.getValue(), filename: './test.php'},
})
.done(function(result) {
console.log("success");
});
}
then on the handler.php
<?php
if(isset($_POST['code']))
{
file_put_contents($_POST['filename'], $_POST['code']);
echo 'success';
}
?>

Related

How to link to a PHP file inside a Wordpress Plugin using jquery

I'm currently trying to write a wordpress plugin. Basically it's a Form that is send to a PHP file via jQuery. (I've used the Code shown in this Tutorial:) Unfortunately I don't know how to link to that PHP file inside jQuery. The Problem is, that I have enabled SEO friendly URL's in Wordpress, so when I'm using the following Code:
$.ajax({
type: "POST",
url: "file.php",
data: dataString,
success: function() {
$('.done').fadeIn('slow');
}
});
The Server assumes the PHP file to be located at http://seofriendlylinktomypost/file.php .
Hopefully someone can help me and thanks in advance =).
I'm sorry for my awful English, I hope you understood everything^^
Create a php folder under your theme and then link in this way:
$.ajax({
type : "POST",
url : "<?php bloginfo('template_url'); ?>/php/file.php",
data : dataString,
success : function() {
$('.done').fadeIn('slow');
}
});
There is also other way. Place an empty A tag with an id attribute (like mytemplatebase) and href=bloginfo('template_url') in the template, then you can use:
var urlBase = $('a#mytemplatebase').attr('href');
$.ajax({
type: "POST",
url: urlBase+"/file.php",
data: dataString,
success: function() {
$('.done').fadeIn('slow');
}
});

Using JQuery.Ajax to change content in other file

I am using an iFrame in my application that lets user to upload a CSV file which has information about the new countries to be added into database. What I want is as soon as the CSV file is processed into the database the existing select box that shows current countries from the database should get updated with newly added entries of CSV file.
I am the making ajax call like
$(document).ready(function(){
jQuery.ajax({
type: "POST",
url: "get_all_countries.php",
data: '',
cache: false,
success: function(response)
{
$("#all_countries_select_box").html("<select name='all_countries' id='all_countries' MULTIPLE size='8' style='min-width:250px;'>"+response+"</select>");
}
});
});
My only problem is the id 'all_countries_select_box' is in another PHP file called 'manage_countries.php'. Is there a way to change content of that file from the file that is in iFrame (upload.php). If not, what could be the best possible solution
If you want to access the parent window from inside an iframe you should use window.parent
success: function(response)
{
var parentJQuery = window.parent.jQuery;
parentJQuery("#all_countries_select_box").html("<select name='all_countries' id='all_countries' MULTIPLE size='8' style='min-width:250px;'>"+response+"</select>");
}
if the file you wantr to modify instead is inside an iframe you should do
success: function(response)
{
$('#idoftheiframe').contains().find("#all_countries_select_box").html("<select name='all_countries' id='all_countries' MULTIPLE size='8' style='min-width:250px;'>"+response+"</select>");
}

Dynamic Javascript Ajax + Php

I'm not sure this question has the best of titles but I'm not sure what else to call it so sorry for that.
I'm using ajax to pull in content for a div on my website (after an option is selected). The content is a form generated by a PHP script. When the form is submitted a JavaScript function should be called but I'm just getting an error that says the function can't be found.
The JavaScript is pulled in via ajax with the form and I can't really change that as it needs to change demanding on the option selected.
My question is should this work? if not I'll just have to re think the way I'm doing it, just wanted to check if it wasn't working because it never will or if I'm doing something wrong.
I would show the code but it's very long.
Thanks in advance!
Edit: thanks for all the comments ect, apologies for not including the code before here it is.
function select(id){
$.ajax({
url: 'select/'+id,
type: 'GET',
dataType: 'html',
success: function(msg) {
$('.product_details').html(msg);
return false;
}
});
}
Are you using a javascript library?
With jQuery specify a data type of html and make sure the script tags are before the HTML in the response
$.ajax({
url: "something.php",
dataType: "html",
success: function(data, text, request) {
...
}
});
in mootools...
var myRequest = new Request({
url: "something.php",
evalScripts: true,
onSuccess: function(responseText, responseXML){
....
}
});
myRequest.send();
Now your passed tags will be evaluated and available to the DOM

Send "iframe.contents()" to PHP Script through Ajax any ideas ?

In my code I have an iFrame which loads dynamic content it's like a webpage(B.html) inside a page(A.php). in "A.php" user can edit inline the "B.html" once the process of editing has completed. In my submission I am sending iframes information to another page (script.php). I tried everything but content is not comming up in "script.php".
In nutshell, I want to tranfer my big html text with all stuff to a PHP via AJAX. I have no idea how to do it... my code would be something like below :-
Code for "A.php" inscript :
"myframe" is the iframe which contains the big chunk of HTML.
sendString = $("#myframe").contents();//Tried everything here[JSON as well]
$.ajax({
url: "script.php",
type: "POST",
data: sendingString,
cache: false,
success: function (html) {
return html;
}
});
Any help would be appreciated.
Regards,
Amjad
$("#myframe").contents() will get you it's nodes as a jQuery object. Try $("#myframe").html() instead to get the contents as a string.
EDIT: Oh, and it also helps if you fix your variable names. Change data: sendingString to data: sendString.

Download pdf file from a string using Jquery

All,
I have a PHP5 application written with Zend Framework and MVC. On my home page, I want to incorporate the functionality to download a dynamically generated pdf file. The way this is done is:
User clicks "download file" link.
On Click, an AJAX call occurs to a PHP controller, which takes the form data, generates the pdf and returns it as a string.
My javascript function now has the pdf string.
How can I display the user an "Open/Save" dialog to download the pdf file from javascript?
I have the following code:
<script type="text/Javascript">
$('#dlcontent').click(function(e) {
$.ajax({
type: 'POST',
url: "/downloads/dlpolicies",
data: $("#frmMyContent").serialize(),
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input); // This has the pdf file in a string.
//ToDo: Open/Save pdf file dialog using this string..
}
});
});
</script>
Thanks
I would try to keep this simple. Just sumbit the form with a target="_blank" and then have PHP force the file download.
HTML CODE
<script type="text/Javascript">
$('#dlcontent').click(function(e) {
e.preventDefault();
$("#frmMyContent").submit();
});
</script>
<form id="formMyContent" action="/downloads/dlpolicies" target="_blank" ... >
...
</form>
Then on your server side, you need to tell PHP to send the response as a "download".
PHP Code
$this->getResponse()
->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
->setHeader('Content-type', 'application/pdf');
Got this from here:
Zend Framework how to set headers
The simplest way of doing it is open a new window with URL to pdf string.
Add an element to your page where you want the link to go. It can be a span or a div or a cell in a table or whatever you want. Give it a unique ID. Then use jQuery to set the html of that element. I refer to it as somepageelement here.
$('#dlcontent').click(function(e) {
$.ajax({
type: 'POST',
url: "/downloads/dlpolicies",
data: $("#frmMyContent").serialize(),
cache: false,
dataType: "html",
success: function(html_input){
$('#somepageelement').html('Click here to download the pdf');
}
});
});

Categories