Jump to content

Formating form strings for output


tsteuwer

Recommended Posts

Hello all. I've been dabbling in PHP for a few years but never got that deep. Until now, I've never really built a system that interacts PHP with MySQL. I've been working on a new website and I am having issues generating output that looks exactly like how it was inputed.

 

Here's how the data looks when I input it:

 

"Some random text.

 

More random text, followed by more random text.

 

Also, we'll add more random text."

 

When I insert this data into my MySQL database, it looks exactly like the above. However, when I get the data from MySQL and echo it via PHP it looks like the following:

 

"Some random text. More random text, followed by more random text. Also, we'll add more random text."

 

It basically removes all formatting. I find this really odd because when I log into the MySQL Admin, and actually open up the data, it looks like the first example. What am I doing wrong when outputting?

 

You can see an example of the output.

 

Link to comment
Share on other sites

When a user submits data via a form, I clean it and then insert into the database with:

 

$description = cleanData($_POST['description']);

function cleanData($originalvariable) {
$newvariable = trim(strip_tags(addslashes(htmlspecialchars($description))));
return $newvariable;
}

 

Then, when I'm displaying it via echo, I use:

 

echo decodeData($description);

function decodeData($originalvariable) {
$newvariable = htmlspecialchars_decode(stripslashes($originalvariable));
return $newvariable;
}

Link to comment
Share on other sites

Hey buddy,

 

I tried using mysql_real_escape_string and the formatting comes out the exact same. Any suggestions on how to clean data from a form to make sure I don't get attached by sql and to keep the formatting of the user in tact? And then, how would I echo it to look the same way and keep the formatting as well?

Link to comment
Share on other sites

Obviously, there is something stripping your output of its newlines.  Which is exactly what the nl2br function is for.  It take new lines and turns them into

's. 

 

I know you can't assume the user knows HTML, you've already mentioned this.  Repetition isn't the answer and The answer isn't repetition.  If you can't get the newlines to show, then how do you expect the user to.  I'm not expecting you or telling you how to run your website or even how to program it.  But before you can even begin to program a site FOR someone you need to know WHY it isn't showing the newlines.

 

Is there any other code you're not showing us that could possibly effect the output.  Since obviously, the input seems to be PERFECT.

Link to comment
Share on other sites

Zanus,

 

I'm sorry for my redundant posts. I always feel like I'm not explaining myself correctly. :)

 

Anyway, you were completely right about it nl2br(). I have no clue what I was doing prior, but I added it to the portion of where it was being echoed and it came out correct.

 

Thank you so much for your help. I've got a lot to learn. Check it out now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.