?
Solved

php/mysql character issues

Posted on 2005-03-03
15
Medium Priority
?
229 Views
Last Modified: 2006-11-17
Hello,
     I am developing a php/mysql/javascript content management system and i've decided to go the database route.
Also my server is linux.  in the content management system i have a WYSIWYG editor so people at work here can edit the site just as in frontpage.  it is an IFRAME and uses some javascript to assign the data in the iframe to a hidden form value using onSubmit="return addme()".  the addme() function is just some javascript that adds the iframe data to the hidden form field called 'newsPost':

function addme()
{
var htmlCode = iView.document.body.innerHTML;  //assigns data in iframe to variable htmlCode
//document.myform.mytext.value = htmlCode;
document.myform.newsPost.value = htmlCode;  //assigns hidden form field the htmlCode data
}

so now that the data is in the form field it gets posted and the script adds the data to the database.  Ok, sounds good no problem ehh?  I found i needed to use the addslashes() function before the data went into the database else it wouldn't store anything with ' (apostrophes)...so i fixed that issue.  The problem now is not putting the data into the database, it's getting the data BACK into the IFRAME.  here is the code i use to get the data back into the iframe from the database:

<?php
include("_global.php");
$n = $_GET['m']; //m = the story id in the dbase
$qry = mysql_query("SELECT * FROM paths where id = '$n'");
while ($query=mysql_fetch_assoc($qry)){
extract($query);
$ourstory = $query['webstory'];
}
<script language="JavaScript">

  var viewMode = 1; // WYSIWYG
  var webstory2='<?=$ourstory?>';  //this is where the php variable gets assigned to the javascript

function Init()
  {
   
    iView.document.designMode = 'On';
   
    iView.document.write(webstory2);  //this successfully writes the data into the iframe for editing.
   
  }
</script>


<body onLoad="Init()">  //calls the init() function on page load

----------------------
Ok, the reason this is necessary is for editing functionality.  in other words, if an employee needs to edit the story they can have the html written back into the iframe and edit the story easily.  the PROBLEM i am having seems to be with carriage returns in the database and apostrophes.  The Iframe won't accept apostrophes or the carriage returns being written to it.  i edited a very short string in the database and added a carriage return.  the javascript wouldn't write the data to the iframe due to the return.  also it won't write the data to the iframe if the string has an apostrophe.  this may be more of a javascript question but this really bugs me.  can ANYONE suggest anything or have a clue as to how to get around this?  this is my only hangup right now. Everything else is completely figured out.  Any questions, just ask me.  i am at computer until 5 pm central.
thanks in advance.



0
Comment
Question by:linuxrox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 9

Expert Comment

by:gruntar
ID: 13451114
Hi,

You must use htmlspecialchars() before outputing content to iframe.

$content = htmlspecialchars($content_from_database);

cheers
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13451166
let me show on your example
--part of your code...


  var viewMode = 1; // WYSIWYG
  var webstory2="<?php echo htmlspecialchars(stripslashes($ourstory)); ?>";  //this is where the php variable gets assigned to the javascript


cheers
0
 

Author Comment

by:linuxrox
ID: 13451168
gruntar...

that did not work. still apostrophes do not get placed into the iframe even when i use:

$ourstory = htmlspecialchars($query['webstory']);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:linuxrox
ID: 13451228
Ok, i tried that and it works for apostrophes but not a 'return'.  if there is a return in the database then it fails.
also, with your code above, the text displayed in the iframe is the html itself and not formatted like you would see in a browser.
i don't want them to have to see the html.  that will just confuse them.  it's not going to be myself who edits things else i wouldn't care but if they see html when editing a story they will freak :)
0
 

Author Comment

by:linuxrox
ID: 13451418
ok, i figured out how to automatically display the html in the iframe as formatted like you would see in a browser.  my only problem now is carriage returns in the database.  that seems to be the only issue.  if i could get rid of carriage returns in the database i think i would have it set.  or some way of getting RID of the carriage returns before they get placed in the iframe.
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13451579
hi,

then try this   <?php echo eregi_replace("[\r\n]",'', $your_var_herew); ?>

cheers
0
 

Author Comment

by:linuxrox
ID: 13451640
i tried that and str_replace("\r\n", "<br>", $stringtext);
i then went into the database and hit the enter key after the text string.
still didn't show in the iframe.  but as soon as i backspace and remove the carriage return it shows up just fine.
wonder if i should do the replace BEFORE i enter the data in the dbase, or is the replace missing something between windows/linux carriage returns??
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13451689
Why do you care so much for carriage eturns? Don't you use HTML editor? then you already have all BRs that you need. Is not your problem solved with javascript errors?

explain a bit more what you wan't...

cheers
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13451776
You can use function nl2br...

<?php echo nl2br($your_var_here); ?>

hope that helps..
0
 

Author Comment

by:linuxrox
ID: 13452238
ok, the problem is that people will be cutting/pasting/typing a story in and carriage returns will be placed intot the database.  the iframe does NOT display any of the text string (the news story) if there is a carriage return or apostrophe in the string.  so i'm trying to elimnate the carriage returns right now so the iframe will display the data it is sent from the database.  you see?
i'll try that nl2br function.
0
 
LVL 9

Accepted Solution

by:
gruntar earned 2000 total points
ID: 13452364
I can see that you used str_replace which is no good here because carriage return and new lines are special chars.

In order to overcome your problem you must use all functions posted in my previous posts... Let me explain...


<?php

// here we remove backslashes
$cleancode = stripslashes($query['webstory']);
// then we change all tags single quotes... so that javascript does not choak :D
$cleancode = htmlspecialchars($cleancode);
// here we change new lines into <br>
$cleancode = nl2br($cleancode);
// last step is to remove rest of unwanted chars - carriage return..
$cleancode = eregi_replace("[\r\n]",'', $cleancode);


// now you have clean code you can use in your iframe
echo $cleancode;

?>

 var webstory2='<?=$cleancode?>';  //this is where the php variable gets assigned to the javascript


cheers
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13452388
ou, maybe you will have use double quotes when printing out code like this

var webstory2="<?=$cleancode?>";
0
 

Author Comment

by:linuxrox
ID: 13452406
i'll award you your points now.  this is what fixed the problem:


//this is before the data gets placed into the database..removes the returns.
$story = eregi_replace("[\r\n]",'', addslashes($_POST['newsPost']));

//this is in the page that gets the data from the dbase and adds to javascript variable.
var webstory2="<?php echo htmlspecialchars(stripslashes($ourstory)); ?>";

works like a charm. i'd give you the url to test and see what i'm doing if i could email you or pm you safely.
thanks again for the help
0
 
LVL 9

Expert Comment

by:gruntar
ID: 13452432
you can email me at     gruntarATgmail.com

cheers
0
 

Author Comment

by:linuxrox
ID: 13452460
10-4!  just sent you mail.
thx.
0

Featured Post

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question