PDA

View Full Version : Check if field is empty using php



rjkdonaldson
10-30-2009, 02:12 AM
How do i perform a simple check using php on the backend to check whether or not field is empty? Here is my sample code




<?php // Script 12.5 - add_entry.php
// This script adds an entry to the database.

// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

if (isset ($_POST['submit'])) { // Handle the form.

// Connect and select.
if ($dbc = @mysql_connect ('localhost', '', '')) {

if (!@mysql_select_db ('msgboard')) {
die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
}




} else {
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}




// Define the query.
$query = "INSERT INTO entry (name, message, date) VALUES ('{$_POST['from']}', '{$_POST['message']}', NOW())";

// Execute the query.



if (@mysql_query ($query)) {
print '<p>The message has been added.</p>';



} else {
print "<p>Could not add the message because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}





} else { // If no session value is present, redirect the user.

ob_end_clean(); // Delete the buffer.

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/message_board.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}
mysql_close();
ob_end_flush();
}

// Display the form.
?>

oesxyl
10-30-2009, 02:15 AM
http://www.php.net/manual/en/function.empty.php

what field? in the database or a php variable?

best regards

rjkdonaldson
10-30-2009, 02:22 AM
Where do I place the code? I have the php code already in my login.php code but it wouldn't work.
Its on a form just want it to check to make sure field is not left empty.

Here is the link to website I'm working on www.postcardsfromheaven.co.nz/form.php


<form action="form.php" method="post">
<p>From: <input type="text" name="from" size="40" maxsize="100" /></p>
<p>Message: <textarea name="message" cols="40" rows="5"><?php
$message = $_GET['msg'];
if(isset($message))
print "Enter your message here:&nbsp;";
?></textarea></p>
<input type="submit" name="submit" value="Submit" /></form></center>

oesxyl
10-30-2009, 02:33 AM
<?php // Script 12.5 - add_entry.php
// This script adds an entry to the database.

// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

if (isset ($_POST['submit']) && !empty($_POST['message'])) { // Handle the form.<===

// Connect and select.
if ($dbc = @mysql_connect ('localhost', '', '')) {

if (!@mysql_select_db ('msgboard')) {
die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
}




} else {
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}




// Define the query.
$query = "INSERT INTO entry (name, message, date) VALUES ('{$_POST['from']}', '{$_POST['message']}', NOW())";

// Execute the query.



if (@mysql_query ($query)) {
print '<p>The message has been added.</p>';



} else {
print "<p>Could not add the message because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}





} else { // If no session value is present, redirect the user.

ob_end_clean(); // Delete the buffer.

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/message_board.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}
mysql_close();
ob_end_flush();
}

// Display the form.
?>



best regards

rjkdonaldson
10-30-2009, 02:49 AM
Thanks alot for your help fumigator! it works hehehe...if I wanted to add another check for the first field do I just add another && to the code?

oesxyl
10-30-2009, 03:00 AM
Thanks alot for your help fumigator! it works hehehe
fumigator? well, yes I'm smoker, :)
you are welcome, :)


...if I wanted to add another check for the first field do I just add another && to the code?
yes, && and a condition, :)

best regards

rjkdonaldson
10-30-2009, 03:17 AM
Now thats all going how can I display a simple message "field is required".

oesxyl
10-30-2009, 03:21 AM
Now thats all going how can I display a simple message "field is required".


Where do I place the code? I have the php code already in my login.php code but it wouldn't work.
Its on a form just want it to check to make sure field is not left empty.

Here is the link to website I'm working on www.postcardsfromheaven.co.nz/form.php


<form action="form.php" method="post">
<p>From: <input type="text" name="from" size="40" maxsize="100" /></p>
<p>Message: <textarea name="message" cols="40" rows="5"><?php
$message = $_GET['msg'];
if(isset($message))
print "Enter your message here:&nbsp;";
if(empty($message)){
print "field is required";
}
?></textarea></p>
<input type="submit" name="submit" value="Submit" /></form></center>

best regards

rjkdonaldson
10-30-2009, 03:24 AM
That simple, thanks bro

Gave that a go and didn't get the result I wanted. Would look better if the message was'nt sent along with the form after submitting it.

I actually edited the code that gets that part of the message and deleted the get part because it was sending everything to the messageboard.

oesxyl
10-30-2009, 03:28 AM
That simple, thanks bro
wait, I put the message inside the textarea, move it outside anywhere you want but after the line with GET.

best regards