Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,685 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,257 people online right now. Registration is fast and FREE... Join Now!




Data inside text forms

 
Reply to this topicStart new topic

Data inside text forms, Displaying user inputting data inside a text form.

Rating  5
livelonger87
post 28 May, 2008 - 04:26 PM
Post #1


New D.I.C Head

*
Joined: 27 May, 2008
Posts: 28


My Contributions


Hey people, I've only just recently started learning PHP. Well, March to be precise but I decided to leave it for a few weeks/a month, as I had a lot of coursework in school to catch up with. Anyway, the main reason I went back, was to prepare for a project I'm doing during the summer holidays. The project is a requirement in the course I'm doing, as it's equivilent to two units. On one side, it's a motivation to learn PHP properly, but on the other side... it's a worry, as I'd rather understand PHP fully.

Anyway, I've been creating small tests with PHP, in which I've came up with this page....
CODE

<?php
//post variables
$username = $_POST['username'];
$message = $_POST['message'];

//checks if user has submitted
if($_POST['submit'])
{
//check if username doesn't == racism, hate or stupidity
if($username == nigger || username == fuck || username == bastard || username == lulz ||     username == gay || username == hay)
  {
     echo "This username is forbidden.  Please, leave immediately.";
}

//filter variables
$search_message = array("fuck", "nigger", "bastard", "lulz", "gay", "hay");
$replace_message = "censored";
$replace = str_replace($username, $search_message, $replace_message);

//check username
if($username == admin)
  {
      echo "<br> You cannot have the same username as the site admin</br>";
}
else if($username == $spam)
  {
      echo "<br>Please don't spam here.</br>";
}
//if username is ok, then submit.
else
  {
      echo "Thank you, $username for submitting a message.";
}
}
?>

<html>
<head>
<title>Messages</title>
<style type="text/css">
<!--
body {
    background-image: url(message.jpg);
    background-attachment: scroll;
    background-repeat: no-repeat;
}
-->
</style>
</head>
<form id="form1" name="form1" method="post" action="anothertestomg.php">
  <label><br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  Username:
  <input type="text" name="username" id="username" />
  <br />
  Message :
  <input type="text" name="message" id="message" />
  </label>
  <p>
    <input type="submit" name="submit" id="submit" value="message" />

</form>
<?php
//user inputted info here
?>
<form id="form2" name="form2" method="get" action="anothertestomg.php">
  <label>
  <textarea name="messages" id="messages" cols="90" rows="5"></textarea>
  </label>
</form>
<p>&nbsp;</p>
</html>

The problem... well, it's the text area (Just above^). I want to be able to display the data a user inputs through the message/username form, into the textarea. I'm unsure as to how I could do this. If you don't understand what I'm meaning, I mean... a page that's similar to an instant messenger conversation, but without the database connection, etc. Just a basic test. Can someone help me out here, please?

Help is much appreciated smile.gif
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 28 May, 2008 - 05:25 PM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


you're talking about either using cookies or sessions....

basically, where you have this code:
CODE
<textarea name="messages" id="messages" cols="90" rows="5"></textarea>


you need to add in this:
CODE
<textarea name="messages" id="messages" cols="90" rows="5"><?php echo $_COOKIE['message']</textarea>


and you set the cookie by using the setcookie() function...

This post has been edited by JBrace1990: 28 May, 2008 - 05:25 PM
User is offlineProfile CardPM

Go to the top of the page

livelonger87
post 28 May, 2008 - 05:44 PM
Post #3


New D.I.C Head

*
Joined: 27 May, 2008
Posts: 28


My Contributions


Hey Thanks for the info JBrace1990. It's working, but... there's a small problem. Anyways, the update:
CODE

<?php
if($_POST['submit'])
{
if($message == "")
{
    echo "<br>Spam is not welcome here.</br>";
}
else if($message == "nigger")
{
     "<br>Racism isn't welcome here.</br>";
}
else if($message == bastard || $message == twat || $message == fuck)
{
     echo "Hatred isn't welcome here.";
}
else
{
?>
<form id="form2" name="form2" method="post" action="anothertestomg.php">
  <label>
  <textarea name="messages" id="messages" cols="90" rows="5">
  <?php echo $message;?></textarea>
  </label>
</form>
<?php
}
}
?>
</html>

The textarea vanished! It's not appearing on the page, but is appearing in Dreamweaver. How could I fix this issue?
User is offlineProfile CardPM

Go to the top of the page

livelonger87
post 28 May, 2008 - 05:51 PM
Post #4


New D.I.C Head

*
Joined: 27 May, 2008
Posts: 28


My Contributions


Also, another issue that seems to occur. Whenever I try to select the "message" box, it automatically sets me to the "username" box.
User is offlineProfile CardPM

Go to the top of the page

livelonger87
post 29 May, 2008 - 10:20 AM
Post #5


New D.I.C Head

*
Joined: 27 May, 2008
Posts: 28


My Contributions


Oh come on GUYS!! HELP!! It's been a day since I had a reply :\

User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 29 May, 2008 - 12:32 PM
Post #6


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


you have the textarea in an else statement, so you either get one of the ifs or elseifs instead of the text area...
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 01:08 PM
Post #7


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


What test message are you sending that won't display? What does your $message variable contain before the textarea?

Are you looking to just display the text in a textarea? You shouldn't need to contain it between label and form tags. Remove it from there and try it out.

As JBrace said, something is causing your if-else statement to trigger instead of going to the form. Are you receiving any error messages, or is it saying the message is spam? If so, the $message isn't being set properly and is left blank.

You've gotta be patient while waiting for help on the forum boards. Trust me, I'd much rather have one or two great replies in 3 or 4 days than 8 or 9 crap replies in a day. Quality over quantity my friend.

Let us know what else you find out.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 31 May, 2008 - 01:13 PM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


A couple other things real quick. Make sure you're putting quotes around the strings you're testing.

CODE

else if($message == "bastard" || $message == "twat" || $message == "fuck")


Also, keep in mind that if somebody sends the message "You are a twat", the message will be allowed to be displayed. This is because "twat" and "You are a twat" are not equal to each other. You'd want to do a string search in a string or a strstr() function. You might also want to copy the message into a test variable like:

CODE

$testMessage = $message;


Then run strtolower($testMessage) to make everything lowercase, and then test that in the strstr() function. More information about strstr() can be found on php.net. Finally, you'll want to remember that if somebody has punctuation like "twat.", it will still show up. You have to make sure you strip out all punctuation from the message.

Those are just a few steps in creating a message filter that you should probably know about. I know you're just learning PHP, but it's better to get in good habits now than try to break bad habits later.

Take it easy.

This post has been edited by akozlik: 31 May, 2008 - 01:14 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 31 May, 2008 - 01:22 PM
Post #9


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(livelonger87 @ 29 May, 2008 - 02:20 PM) *

Oh come on GUYS!! HELP!! It's been a day since I had a reply :\

This sort of an attitude won't encourage people to help you. Please be patient & someone will offer you help.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:12AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month