Jump to content

Help: How to display comment field ??


worth2talk

Recommended Posts

Hi PHP Freaks,

I have been facing a problem with PHP forms. I have created a form to take inputs from users i.e. user comments and have to display it. But the comments user provide will always display in a signle line. For example, if user comment has couple of new line characters then all will come and display in a single line.

 

main.php code:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>PHP Test</title>

<link href="test.css" type="text/css" media="screen"  rel="stylesheet">

</head>

 

<body>

 

<form action="welcome.php" method="post">

        <label for="user" >Name</label>

        <input type="text" name="user" value="" /><br />

 

        <label for="emailaddress">Email Address:</label>

        <input type="text" name="emailaddress" value="" /> <font color="red">(will not be published)<br />

 

        <label for=\"webaddress\">Web Address</label>

        <input type="text" name="url" value="" /><br />

 

        <label for="comments">Comments:</label>

        <textarea name="comments"></textarea><br />

 

        <label for="terms">Agree to Terms?</label>

        <input type="checkbox" name="terms" class="boxes" /><br />

 

        <input type="hidden" name="pid" value='$pid' /><br />

<input type="submit" name="submitbutton" id="submitbutton" value="Submit" />

        </form>

 

</body>

</html>

 

 

comhandler.php

 

<html>

<body>

 

<?php

$userName = $_POST['user'];

$userMail = $_POST['emailaddress'];

$userUrl = $_POST['url'];

$userComment = $_POST['comments'];

 

 

echo "$userName";

echo "<br />";

echo "$userMail";

echo "<br />";

echo "$userUrl";

echo "<br />";

echo "$userComment";

echo "<br />";

echo "$magicString";

 

?>

</body>

</html>

 

Please guide me...as i m a newbie to PHP. Your quick response will be appreciated...!!

 

Thanks in advance ..!!

Link to comment
https://forums.phpfreaks.com/topic/47994-help-how-to-display-comment-field/
Share on other sites

First you might want to modify your code by placing it inside [ code ] tags
because no one wants to read that mess!

 

<?php
echo "$userName <br />"; 
echo "$userMail <br />";
echo "$userUrl <br />";
echo "$userComment <br />";
echo "$magicString";

// Or this
echo "$userName \n"; 
echo "$userMail \n";
echo "$userUrl  \n";
echo "$userComment \n";
echo "$magicString";

?>

First of all sorry for messing up the code... i was not aware of that.

Well, i dont want to add any newline character for displaying comment. I want to display as user entered.. For example, consider user provides comment as below:

----------------------------

Awesome....

 

wonderful

 

 

 

Thanks

------------------------------

There are one newline after 1st sentence, 3 newlines after 2nd sentence...

With my original code, the above comment will display everything in a single line as:

 

Awesome.... wonderful Thanks

but i want to output in the same input format...

 

hope u will get this.

 

I can use nl2br() function and it will work. But another problem is to filtered the user code provided as a part of comment. For example, someone tries to provide a spam input as

<script type='text/javascript'>
window.location = 'http://www.spamsite.com/'
</script>'";

to avoid this i have to used below code before displaying comment

 

$userFilteredComment = htmlentities($userComment);

 

so how to display newline now ??

i found a way to display this...

 

	  $userFilteredComment = htmlentities($userComment);
  $userFilteredComment = nl2br($userFilteredComment);

 

but now the issue is, MYSQL query got failed when i am trying to write this '$userFilteredComment' in table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.