/    Sign up×
Community /Pin to ProfileBookmark

Code question…

Is there a way to force all fields to be filled in with “something” before the submit button will work?

Thanx.

Code follows:
————————-

<?PHP

$filename = “guestbook.txt”; #CHMOD to 666
$text = “”;

if (isset($_POST[“submit”])) {
$name = $_
POST[“name”];
$message = $_POST[“message”];

$date = date (“l, F jS, Y”);
$time = date (“h:i A”);

$value = $message.”<br>n<span style=”font-family: verdana, arial, sans-serif; font-size: 70%; font-weight: bold;”>Posted by $name at $time on $date.</span>n<break>n”;

##Write the file##

$fp = fopen ($filename, “a”);
if ($fp) {
fwrite ($fp, $value);
fclose ($fp);
}
else {
echo (“Your entry could not be added.”);
}

}

?>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
http://www.w3.org/TR/html4/strict.dtd“>
<html>
<head>
<title>Guestbook</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>

<body>
<form action=”<?PHP echo $_SERVER[“PHP_SELF”]; ?>” method=”post”>
<p>Your Message:<br>
<textarea name=”message” rows=”7″ cols=”50″></textarea><br>
Your Name: <input type=”text” name=”name”>
<input type=”submit” name=”submit” value=”submit”></p>
</form>

<?PHP

$contents = @file($filename) or die(“No files in guestbook.”);

foreach ($contents as $line_num => $line) {
$text .= $line;
}

$text = split(“<break>”, $text);

for ($i=0; $i<count($text)-1; $i++) {
echo “<div style=”border: gray 1px solid; padding: 5px; font-family: arial, sans-serif; background-color: #eeeeee;”>”;
echo $text[$i];
echo “</div>”;
}

?>
</body>

</html>
————————-

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@pyroNov 19.2003 — Just check if they are [url=http://us3.php.net/manual/en/function.empty.php]empty()[/url].

Also, you should run $name and $message through [URL=http://us3.php.net/manual/en/function.htmlspecialchars.php]htmlspecialchars()[/URL] to be sure they don't insert HTML/PHP in their entries... ?
Copy linkTweet thisAlerts:
@stmasiauthorNov 19.2003 — You'll have to excuse my ignorance in this matter...could you inform me of where the "empty()" should be inserted into the existing code?

Thank you.
Copy linkTweet thisAlerts:
@pyroNov 19.2003 — Try this (also locked the file, to keep data protected):

[code=php]<?PHP

$filename = "guestbook.txt"; #CHMOD to 666
$text = "";

if (isset($_POST["submit"])) {
$name = htmlspecialchars($_POST["name"]);
$message = htmlspecialchars($_POST["message"]);

if (empty($name)) {
echo "Please fill in your name.";
}
else if (
echo "Please fill out a message.";
}
else {
$date = date ("l, F jS, Y");
$time = date ("h:i A");

$value = $message."<br>n<span style="font-family: verdana, arial, sans-serif; font-size: 70%; font-weight: bold;">Posted by $name at $time on $date.</span>n<break>n";

##Write the file##

$fp = fopen ($filename, "a");
if ($fp) {
flock($fp, LOCK_EX);
fwrite ($fp, $value);
flock($fp, LOCK_UN);
fclose ($fp);
}
else {
echo ("Your entry could not be added.");
}
}
}

?> [/code]
Copy linkTweet thisAlerts:
@stmasiauthorNov 19.2003 — It must be closer now because now I'm getting an error:

Parse error: parse error, unexpected T_ECHO in guestbook.php on line 14


Any ideas?

Thanx yet again.
Copy linkTweet thisAlerts:
@pyroNov 19.2003 — Yeah. That error is called "me not thinking..." :rolleyes:

[code=php]<?PHP

$filename = "guestbook.txt"; #CHMOD to 666
$text = "";

if (isset($_POST["submit"])) {
$name = htmlspecialchars($_POST["name"]);
$message = htmlspecialchars($_POST["message"]);

if (empty($name)) {
echo "Please fill in your name.";
}
else if (empty($message)) {
echo "Please fill out a message.";
}
else {
$date = date ("l, F jS, Y");
$time = date ("h:i A");

$value = $message."<br>n<span style="font-family: verdana, arial, sans-serif; font-size: 70%; font-weight: bold;">Posted by $name at $time on $date.</span>n<break>n";

##Write the file##

$fp = fopen ($filename, "a");
if ($fp) {
flock($fp, LOCK_EX);
fwrite ($fp, $value);
flock($fp, LOCK_UN);
fclose ($fp);
}
else {
echo ("Your entry could not be added.");
}
}
}

?> [/code]
Copy linkTweet thisAlerts:
@stmasiauthorNov 19.2003 — Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.

;-)
Copy linkTweet thisAlerts:
@pyroNov 19.2003 — You bet... ?
Copy linkTweet thisAlerts:
@stmasiauthorNov 19.2003 — One last thing...

Is there a way to force a preset number of characters?

Okay...two last things...

Is there a way to check for a valid email address?

Thanx...thanx...a million times thanx.

8^)>
Copy linkTweet thisAlerts:
@pyroNov 19.2003 — Take a look at this:

&lt;?PHP
$numchars = "abcd";
if (strlen($numchars) &lt; 5) {
echo "String too short&lt;br&gt;";
}
$email = "foo@your";
if (!preg_match('/[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+/', $email)) {
echo "Please enter a valid email address";
}
?&gt;
All credit to Jeff Mott for the regex (email validation).
Copy linkTweet thisAlerts:
@stmasiauthorNov 19.2003 — Thanx Pyro!!!

Thanx Jeff!!!

How about client IP Address? I tried REMOTE_ADDR, but it doesn't seem to work.

Thanx again.
Copy linkTweet thisAlerts:
@pyroNov 19.2003 — $_SERVER['REMOTE_ADDR'] ?
Copy linkTweet thisAlerts:
@Paul_JrNov 20.2003 — [i]Originally posted by pyro [/i]

[B]Take a look at this:



&lt;?PHP
$email = "foo@your";
if (!preg_match('/[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+/', $email)) {
echo "Please enter a valid email address";
}
?&gt;
[/B][/QUOTE]



I was just wondering what part that massive line of gibberish plays in making this script work?
Copy linkTweet thisAlerts:
@pyroNov 20.2003 — That "massive line of gibberish" is called a regular expression, and plays a massive roll in it. Regex are an extremely powerful tool for manipulation text and data. They allow you to match things (as the email address validation is doing), replace text (even if you do not necessarily know the text to be replaced), split text, etc.
×

Success!

Help @stmasi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...