PHP print content as it is - php

I have a textarea
<textarea id=view-body ></textarea>
that gets a content when an onclick from
<a href='#' onclick='popup_view(this)' data-body ='". $rows['body'] ."' >view</a>
<script language="JavaScript">
function popup_view(e)
{
var body = e.getAttribute('data-body');
el = document.getElementById("view-body");
el.innerHTML = body;
}
</script>
I wanted the content of my textarea to be printed the way it is.
For example the onclick sends
<i>Wired</i>
my textarea printout
<i>Wired</i>
but not, and supposed to be like this
Wired

Javascript innerHTML function will convert text to html form.
You can use textContent function as shown here
http://jsfiddle.net/yWgn7/
In jquery you can use .text() function, but i am not sure about textContent compatibility.

Instead of a textarea try using a div. If I'm understanding this correctly, the textarea is showing the <i></i> and you don't want it to. Textarea doesn't handle html formatting naturally.

HTML's built in <textarea> elements doesn't allow you to style it's contents. What you need is some kind of a wysiwyg editor to do that.

You can replace your text-area with any Rich text editor
http://ckeditor.com/

You must use the htmlentities function to replace special characters with html entities.
data-body = "<? php echo htmlentities($rows[' body ']);?>"

Related

trouble in getting Rich text formatted document from tinymce plugin which i have used in my form

I am using a TinyMCE plugin inside my form.so that user can write a small
formatted blog about their job.but there is a trouble in
storing those data in a var using javascript.
processing and
transferring that file(.rtf) through a mail using PHP mail().
when the user submits the form.
so I need some suggestions. how I can do this thing for form.
so far I have tried retrieving the text using jquery but it's not working.
just have a look at my simplest code for retrieving those text.
<form>
<textarea id="tinymce"></textarea>
</form>
<button onclick="myfunctionforTinyMce()">hit me!</button>
<script>
function myfunctionforTinyMce(){
alert($("#tinymce").val());
}
</script>`
this is the output on clicking on that button
Thank you.
If you look at this question, you can see that the TinyMCE editor is not a normal TextArea. You need to call the TinyMCE object API methods for manipulating its content.
var content = tinymce.getContent('tinymce');
alert(content);
This should do the trick for your example. It will return the content in HTML format.
EDIT
To get the content as plain text, try the following:
var content = tinymce.get('tinymce').getContent({format:'text'})
EDIT #2
I added a CodePen below demonstrating a simple implementation with a button alerting the content to you. Just replace the alert with an AJAX-call ($.post(...) for example), or do a normal form submission where you post the content to your backend PHP server. Then let the server use the data to send the mail.
https://codepen.io/canis1980/pen/qYVKPW

not getting inside value of id through (java script)

i want only values inside my ISBN through my HTML Code
<div ISBN="9381015889">
Java Script Code
($("#ISBN").val());
i am not getting value of ISBN=9381015889
you can get this by using jquery data
HTML
<div data-ISBN="9381015889" id="ISBN">
Jquery
$("#ISBN").data('ISBN');
Put id attribute in the html div. ISBN cannot be attribute. So make id as attribute and name it as ISBN like below.
<div id="ISBN">9381015889</div>
Use this to access
$("#ISBN").html();
as .val() is used if you want to take from form fields like text field or drop down.
try this:
alert($(this).attr('ISBN'));
Try this:
HTML
<div ISBN="9381015889" id="ISBN">
Jquery
$("#ISBN").attr('ISBN');
You forgot to specify an ID to the div.:
<div id="ISBN" data-isbn="9381015889">
You can access the ISBN number via: $("#ISBN").data("isbn")
Reff: Jquery
Without modifying the html code, the css selector can be adapted: div[ISBN] instead of #ISBN.
Try this snippet:
document.write( $('div[ISBN]').attr('ISBN') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div ISBN="9381015889">
You can user data-ISBN instead of plain ISBN and include an id attribute for div tag
Then you can retrieve it using javacript like this
<div id='isbndata' data-ISBN="9381015889">
var selector= document.querySelector('#isbndata');
alert('Info is :' + selector.dataset.ISBN);
You can also use jquery's data attribute:
$('#isbndata').data('ISBN');
Links which can be helpful to u
http://api.jquery.com/data/
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes
Hope this helps

Html tag automatically added textarea value if form submitted through Jquery

Html tag automatically added textarea value if form submitted through Jquery.
My jquery code.
$('#calling').click(function() {
$('#myform').submit();
});
In my form one text area is there.
<textarea id="area" name="rte1" cols="50" rows="10">Value of textarea.</textarea>
Javascript code as below.
document.myform.submit();
In case of jquery form submit i am getting textarea value in PHP as
<p>Value of textarea.</p>
But in case of java script form submit i am getting the proper value in php.
Please suggest what is the problem with my jquery code.
Well a simple way in php to get the value without any HTML Tag
$textarea = strip_tags($_REQUEST['rte1']);
This will return you Value of textarea. without <p> tags.
See Manual if you need any more help.
There is no problem at all with your jquery code. Tested with POST and GET methods and the server only gets the "Value of textarea." string. You must have something wrong in your PHP.
http://jsfiddle.net/sftHD/
document.myform.submit();
$('#myform').submit();
Those produce the same result.

How i add rel="nofollow" on a tag in the ckeditor content

I have a site what user can submit content.So i want to avoid spam link by add on a tag rel="nofollow".How can i do that with jquery or php
This is link
Use this
$("a[href='http://mnking.net']").attr("rel", "nofollow");
You can do:
$(document).ready(function(){
$("a").attr("rel", "nofollow");
});
Put post into a separate div, let's say class="post"
and then just simple jquery code:
$('.post a').attr('rel', 'nofollow');
if you get the link using $_POST, you could use the following code (written in PHP):
$url = $_POST['url'];
$tag = 'This is link';
In PHP , the sign . (a simple dot) is used to concat strings.
Good luck.

Passing HTML to javascript function

I have a variable in which a complete html is saved.
$body= $myhtmlpage;
<a onclick="openWin(' <?php echo htmlspecialchars(json_encode($body)) ?>');" href="javascript:void(0);"> Click </a>
and i have this javascript function which display the text in new window.
<script type="text/javascript">
function openWin( str )
{
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(str+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
When there is simple text in my body, it works fine. but if there is some html then it does not display, I am new to javascript. please tell me how can i prepare my HTML that it should be passed to Javascript html. i tried htmlspecialchars(json_encode($body))
functions but still having problem.
Uncaught SyntaxError: Unexpected identifier
You will have a long battle trying to get a lot of HTML to work as a string variable in Javascript. It would be far better for you to put that markup in a hidden block (like a DIV) in your markup and then just get the contents of that markup and show it in your window.
This has the added advantage of allowing your hidden markup to be validated. It is very hard to debug a lot of html markup stuffed into a string variable, but when included in the DOM as real markup it makes your life much easier.
UPDATE: Adding some sample code:
<div id="my_hidden_content" style="display:none;">
<?php echo $body; ?>
</div>
<a onclick="openWin('my_hidden_content');" href="javascript:void(0);"> Click </a>
Now the javascript:
function openWin( contentId )
{
var contentContainer = document.getElementById(contentId);
var content = contentContainer.innerHTML;
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(content+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
Firstly, you do not need to use json_encode(), that is just confusing the situation.
Secondly, the problem will be that your HTML contains quotes. This will result in a syntax error in the HTML you output, since htmlspecialchars() does not escape quotes.
Use htmlentities() with the ENT_QUOTES flag instead. So change the line to this:
<a onclick="openWin('<?php echo htmlentities($body, ENT_QUOTES) ?>');" href="javascript:void(0);">Click</a>
Thirdly (although it should probably be firstly since it is the most important point) your approach to this is all wrong. If you're opening a new window, you should have it load a page from the server and generate the HTML when the window is opened.
You can try html code inside php code, instead php code inside html.
for e.g.
<?php
echo "<a onclick='openWin(\" ".htmlspecialchars(json_encode($body))." \")'>Click</a>";
?>
That's because, HTML isn't JSON. For that simply use: htmlspecialchars($body)

Categories