Jump to content

How to add a text box check


GunnDawg

Recommended Posts

Alright so I have this lil test project of a message board that I am using to practice writing, and calling data from a mysql db. It all seems to work fine except for one minor lil hitch. If I refresh the page via "browser refresh button" then it adds a blank entry into my table. How would I go about adding a check to the query to say something like

 

"if $_POST[message] >= 0 { echo "no data entered"} return false;

 

I know thats not a proper code, its just me thinking out loud in half english and half php about what I think it would sort of look like :) But how would I actually do it?

 

Here is my code....

 

<html>
<head>
<title>Making a forum for mario</title>
</head>

<body bgcolor="#666666">

<center>

<font size="12" face="comic sans ms" color="black">
Welcome to my Message Board!
</font>

<br \>
<br \>

<form name="forum" action="test2.php" method="post">
<textarea name="message" cols="75" rows="10">
Enter your message here...
</textarea><br \><br \>
<input type="submit" value="Post Message!">
</form

</center>

<?php

$message = $_POST['message'];

$user = 'admin';
$pass = 'xxxxx';
$host = 'localhost';
$link = mysql_connect ($host, $user, $pass);

if(!$link) {

echo "Failed to connect to database";

}

else

{

echo "Connection Established".'<br />';

}

mysql_select_db('cdev', $link);

$query = "INSERT INTO forum (message) VALUES ('".$message."')";
mysql_query($query) or die ('Error writing message to db!');

echo "Your message was added!".'<br />'.'<br />';



$sql = "SELECT message FROM forum";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){
  echo $row['message'].'<br />';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/137784-how-to-add-a-text-box-check/
Share on other sites

Alright so I have this lil test project of a message board that I am using to practice writing, and calling data from a mysql db. It all seems to work fine except for one minor lil hitch. If I refresh the page via "browser refresh button" then it adds a blank entry into my table. How would I go about adding a check to the query to say something like

 

"if $_POST[message] >= 0 { echo "no data entered"} return false;

 

I know thats not a proper code, its just me thinking out loud in half english and half php about what I think it would sort of look like :) But how would I actually do it?

 

Here is my code....

 

<html>
<head>
<title>Making a forum for mario</title>
</head>

<body bgcolor="#666666">

<center>

<font size="12" face="comic sans ms" color="black">
Welcome to my Message Board!
</font>

<br \>
<br \>

<form name="forum" action="test2.php" method="post">
<textarea name="message" cols="75" rows="10">
Enter your message here...
</textarea><br \><br \>
<input type="submit" value="Post Message!">
</form

</center>

<?php

$message = $_POST['message'];

$user = 'admin';
$pass = 'xxxxx';
$host = 'localhost';
$link = mysql_connect ($host, $user, $pass);

if(!$link) {

echo "Failed to connect to database";

}

else

{

echo "Connection Established".'<br />';

}

mysql_select_db('cdev', $link);

$query = "INSERT INTO forum (message) VALUES ('".$message."')";
mysql_query($query) or die ('Error writing message to db!');

echo "Your message was added!".'<br />'.'<br />';



$sql = "SELECT message FROM forum";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){
  echo $row['message'].'<br />';
}

?>

 

Anyone know where in my code I would add this if statement, and what the rest of the statement would be..?

 

if(!$_POST['message'])

  • 2 weeks later...

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.