Data inside text forms

Displaying user inputting data inside a text form.

Page 1 of 1

8 Replies - 1214 Views - Last Post: 31 May 2008 - 02:22 PM Rate Topic: ***** 1 Votes

#1 livelonger87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Data inside text forms

Posted 28 May 2008 - 05:26 PM

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....
<?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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Data inside text forms

#2 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Data inside text forms

Posted 28 May 2008 - 06:25 PM

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

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


you need to add in this:
<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 - 06:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 livelonger87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Data inside text forms

Posted 28 May 2008 - 06:44 PM

Hey Thanks for the info JBrace1990. It's working, but... there's a small problem. Anyways, the update:
<?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?
Was This Post Helpful? 0
  • +
  • -

#4 livelonger87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Data inside text forms

Posted 28 May 2008 - 06:51 PM

Also, another issue that seems to occur. Whenever I try to select the "message" box, it automatically sets me to the "username" box.
Was This Post Helpful? 0
  • +
  • -

#5 livelonger87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Data inside text forms

Posted 29 May 2008 - 11:20 AM

Oh come on GUYS!! HELP!! It's been a day since I had a reply :\
Was This Post Helpful? 0
  • +
  • -

#6 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Data inside text forms

Posted 29 May 2008 - 01:32 PM

you have the textarea in an else statement, so you either get one of the ifs or elseifs instead of the text area...
Was This Post Helpful? 0
  • +
  • -

#7 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data inside text forms

Posted 31 May 2008 - 02:08 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data inside text forms

Posted 31 May 2008 - 02:13 PM

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

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:

$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 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Data inside text forms

Posted 31 May 2008 - 02:22 PM

View Postlivelonger87, on 29 May, 2008 - 02:20 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1