Get source code of element in PHP - javascript

I have a container which has dynamic content in it. The content can be edited by users with an inline editor called Froala Editor.
<div id="froala-editor">
DYNAMIC HTML CONTENT
</div>
I want to output the source code generated by the froala editor into a php variable.
I have tried using froala editor's function for it, but it does not get any result.
$('div#froala-editor').froalaEditor('html.get');
I also tried to write my own function:
<?php
ob_start();
?>
<script>
document.write($('div#froala-editord').html());
</script>
<?php
$output = ob_get_contents();
ob_end_clean();
$html_output = htmlentities($output);
echo "<pre>";
echo $html_output;
echo "</pre>";
?>
But this snippet gives the result:
<script>
document.write($('div#froala-editor').html());
</script>
What I want is to get the source code of the #froala-editor div and store it in a php variable.
I also tried to use file_get_contents() but the file that has this div also has dynamic content in it.
Also tried the Simple HTML DOM Parser but it gives back an error using file_get_contents()
When the user finished editing the content with the editor, I want to submit a form with a hidden element that has the value of the edited content's source code.

Related

How can I add advertisement script inside yii framework loop?

I am trying to add an horizontal ad in between the content. An example article of my website is: http://articlefirm.com/biography/penny-taylor-age-net-worth-biography-twitter.html. I would like the ad to come somewhere inside the content.
The website is on YII framework. Here's the content code:
<?php echo $page->detail; ?>
Please let me know how could I add script inside the above given code.
you can change your main.php layout and add the code for show the ads as you like this way
if you need pass some php vars to the main.php layout
a simple way is add property to your controller
class YourController extends CController {
public $myParam;
then assign in your action the values you need for params
eg:
$this->myParam ='my_adv';
and then in your layout you can simple
<?php echo $this->myParam; ?>
otherwise if you don't need pass params form controller/action
you can simply change you views/layouts/main.php
adding the conde you need
<?php
echo 'here i cand add the adv i need before the view content';
?>
<?php echo $content; ?>

PHP link written into page not being retrieved by a JAVASCRIPT function

I'm writing on a page the following code with PHP. This code creates a A HREF link with the ID equal to $intIdType, which is the value of the PRIMARY KEY in a database, and $count gets the amount of records per category on the database. All of this is inside a WHILE that reads each record and writes on the PAGE the results
echo "<a href='#' id='$intIdType'>";//Starts creating the link
echo "$intIdType -";
echo $arrBusinessTypes["business_Description"];
echo "(";
echo " $count1 )"."</a>";
Now, after the results are on the page it will look like this:
$intIdType $arrBusinessTypes $count
--------------------------------------------
1 -Auto Sales ( 1 )
2 -Auto Repair ( 1 )
5 -Web Desig & I.T. Services( 2 )
6 -Computer Shop ( 1 )
The above result displays each rown as a link where I can click on it, but nothing happens. Even just a simple Alert in javascript does not show up. It seems that it never reaches even the Javascript at all.
What I need now is to retrieve that PHP generated code on the page by a Javascript file, that will allow me to use the hiperlink generated by PHP into the .HTML page
It works if I write directly into the page what PHP is suppose to write. I wonder if this happens because Javascript can not read posted data from PHP into the page.
The Javascript File looks like this:
window.onload=post_result
function post_result() {
$("#1").click(function() { //This is the HREF ID that is written by PHP into the page
$('#list_results').load("results.php");//This seeks to run a PHP file in a DIV
$('.title').text('Listing Categories');//This just replaces the Title in the page
})
I'm just a beginner trying. Thanks for any help.
echo "<a id='$intIdType'>";//Starts creating the link echo "$intIdType -";
echo "$intIdType -";
echo $arrBusinessTypes["business_Description"];
echo "("; echo " $count1 )"."</a>";
just remove href attribute and try
Remark #1: quote custom data when outputs it to HTML:
echo $arrBusinessTypes["business_Description"]; must be
echo htmlspecialchars($arrBusinessTypes["business_Description"]);
Remark #2: you don't need window.onload handler here. In this piece of code you select complex way to do simple thing. Why do not write direct onclick handler? Write function loading some data depending of parameter and do something like:
$htmlspecialchars = function($s){return htmlspecialchars($s);};
echo <<<HTML
<a href='#' id='$intIdType' onclick='loadInfo($intIdType)'>
$intIdType -{$htmlspecialchars($arrBusinessTypes["business_Description"])} ( $count1 )
</a>
HTML;
(converted to heredoc to make HTML more clean)

Loading a HTML file into ACE Editor PRE Tag

There is a (on the server) locally stored HTML file which i need to show to user and allow use to make changes to it and save it. (Something like template file editor in wordpress).
For this I am using ACE Editor.
My javascript code:
$(document).ready(function() {
var editor = ace.edit("editor");
editor.getSession().setMode("ace/mode/html");
editor.setTheme("ace/theme/chrome");
editor.setValue("<?php echo addslashes(file_get_contents("abc.html")); ?>");
editor.gotoLine(1);
});
Code in file abc.html
My Problem: Although I have used addslashes, there are some characters that cause problem.
Isn't there a method to directly supply a file to ACE Editor?
Is there any other such editor which can directly be supplied a file name to open?
EDIT: SOLVED!
Instead of passing file text via setValue() function, I directly printed text within PRE tag
<pre id="editor"><?php echo htmlentities(file_get_contents($input_dir."abc.html")); ?></pre>
It worked.
the correct escaping is
htmlspecialchars(addslashes(file_get_contents("abc.html")));
editor.setValue("<?php echo addslashes(file_get_contents("abc.html")); ?>");
is wrong. abc.html is out of php code. syntax error
editor.setValue('<?php echo addslashes(file_get_contents("abc.html")); ?>');
this could work. have not tested

How can I use a theme option in a JS file?

I'm creating a Wordpress theme and have added an option which allowed users to change a font family using (simplified code): update_option('mytheme_font', $_POST['mytheme_font']);
How do I then get the value of that option in a JS file of the theme? I need it because I'm using Cufon to replace some H1's and H2's. Thanks!
You have four options I suppose.
Output your javascript to your page inside script tags. Easiest
<script>
<?php echo 'var x = 3;' ?>
</script>
Output the variable to your page and then read that from your javascript file. Clunky, but does mean you don't have to create some js globals.
<div id="x" style="display: none;">3</div>
var x = document.getElementById('x').innerHTML();
[Added] - Use AJAX to request the data and parse after page load.
Last and I don't recommend it, but setup php to parse .js files to dynamically produce javascript files. This way you could place a <?php ?> call in your .js files.
You could echo some thing like this in the <head> of the header.php
<?php
$defaultThemeFont = "myDefaultValue";
$userThemeFont = get_option("mytheme_font");
if($defaultThemeFont == NULL)
$userThemeFont = $defaultThemeFont
?>
<script>
<?php echo "var mytheme_font = $userThemeFont;"; ?>
</script>
Now you can access this variable from any JS file

How can I send wysiwyg editor code to php by ajax

I have nicEditor wysiwyg editor in my page (textarea tag) .
I want to send the output of the code editor to php page by ajax , this is my request .
You can do a php require to include any type of file:
textarea.html:
<textarea>
//htmlcode
</textare>
page.php:
<?php
//php code
require "textarea.html"
?>
is that what you are talking about?
Or are you refering to using javascript to get html code from the file?
i don't know exactly how nicEdit works, but according to nicedit.com, it just changes all <textarea>s to rich html editors. I'm guessing you can still refer to the textareas the same way, which would by to do what #atlavis said:
<textarea id='text1'></textarea>
<script>
var textarea document.getElementById('text1').value;
textarea = escape(textarea);
//now send textarea to the php with a XHR request
</script

Categories

Resources