submit form then check text fields if statements

I need to make a script that sends information to a mysql data-base. Sending this info to the data-base and storing it is not an issue but new to php im stuck on a stupid place and cant find an answer off people on other places!

what i got is 2 text fields where the user inputs their name and a comment… yes basicaly like a guest book but not.

thats done and the data to mysql gets sent perfectly.

Now what i need to do is stop if one of the fields or if both of the fields are empty.

So what i thought would work dosent…

It stops but it also stops if there is text in the fields so i need some sample code of how i would go about doing these if statements because everywhere i look i cant seem to find anything i understand lol

so this is what i thought would work but dosent.

[php]if (!strlen(trim($_REQUEST[‘usersname’]))){
die(‘Please go back and enter out the required name!’);
}
elseif (!strlen(trim($_POST[‘comment1’]))){
die(‘Please go back and enter out the required comment!’);
}else{

///process all my mysql data past this line
} [/php]

could someone please help me thanks!

That’s an interesting way to do it, clear spaces and count the string length. At least your using trim()… my boss always checks if strlen == 0 (with or without spaces). If there’s a blank space, the code continues. Yes, it drives me nuts and I always have to go through his code and replace it with something like:

[php]
if (!empty($_POST[‘something’]) || (!empty($_POST[‘something_else’]))) {
// $myvar has a value that isn’t whitespace or null
} else {
// empty variable
}[/php]

Or if all form fields need to be filled out you could loop through the $_POST array:

[php]

$_POST = array(‘1’ => ‘one’, ‘2’ => ‘’); //Example

foreach ($_POST as $key => $value) {
if (empty($value)) {
echo $key . ’ is empty
‘;
} else {
echo $key . ’ not empty
’;
}
}
/*
Outputs:
1 not empty
2 is empty
*/
[/php]

Yes i understand that but what i dont understand is i tried that top 1 you just give me and if i filled it out it still was not adding to the data base and telling me info was not filled out even though the 2 text fields had the name and comment in so there is clearly something iv done wrong here is what i have

[php]<?php
$name = $_REQUEST[‘usersname’]; // required
$comment = $_REQUEST[‘comment1’]; // required
$datetime=date(“d-m-y h:i:s”); //date time … this is not submitted by the user!

if (!empty($_POST[$name]) || (!empty($_POST[$comment]))) {
// $myvar has a value that isn’t whitespace or null

$host=“host”; // Host name
$username=“username”; // Mysql username
$password=“dbpassword”; // Mysql password
$db_name=“dbname”; // Database name
$tbl_name=“tbname”; // Table name

// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect server “);
mysql_select_db(”$db_name”)or die(“cannot select DB”);

$sql=“INSERT INTO $tbl_name(name, comment, datetime)VALUES(’$name’, ‘$comment’, ‘$datetime’)”;
$result=mysql_query($sql);

//check if query successful
if($result){
echo “Added”;
echo “
”;
echo “”; // link to view guestbook page
}

else {
echo “ERROR”;
}

mysql_close();
header(‘Refresh: 0; URL=…/’);
} else {
die(“Please go back and fill out all the information!”);
// empty variable
}
?>[/php]

It wants to shoot down to die(“Please go back and fill out all the information!”); no matter what even if the form is filled out.

Thanks

[php] $name = $_REQUEST[‘usersname’]; // required
$comment = $_REQUEST[‘comment1’]; // required
$datetime=date(“d-m-y h:i:s”); //date time … this is not submitted by the user!

if (!empty($_POST[$name]) || (!empty($_POST[$comment]))) {[/php]

The above code will read like:

[php]
if (!empty($_POST[$_REQUEST[‘usersname’]]) || (!empty($_POST[$_REQUEST[‘comment1’]]))) {[/php]

Since they’re not valid form fields, it will always be empty. Try using just $_POST[‘usersname’] and $_POST[‘comment1’]. Also, unless you really need to, I wouldn’t recommend throwing the $_REQUEST array around in your code as it contains sessions, cookies, gets, and posts - if all you need are posts.

i really cant figure this out although i wasent sure if you was telling me to change the above code to that or not never the less i did and still cant get it to work so there must be something else wrong here because it still sends it straight down to the error message. i think i got my wire crossed here somewhere :frowning: lol

[php]<?php
$name = $_POST[‘usersname’]; // required
$comment = $_POST[‘comment1’]; // required
$datetime=date(“d-m-y h:i:s”); //date time … this is not submitted by the user!

if (!empty($_POST[$_REQUEST[‘usersname’]]) || (!empty($_POST[$_REQUEST[‘comment1’]]))) {
// $myvar has a value that isn’t whitespace or null

$host=“host”; // Host name
$username=“username”; // Mysql username
$password=“dbpassword”; // Mysql password
$db_name=“dbname”; // Database name
$tbl_name=“tbname”; // Table name

// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect server “);
mysql_select_db(”$db_name”)or die(“cannot select DB”);

$sql=“INSERT INTO $tbl_name(name, comment, datetime)VALUES(’$name’, ‘$comment’, ‘$datetime’)”;
$result=mysql_query($sql);

//check if query successful
if($result){
echo “Added”;
echo “
”;
echo “”; // link to view guestbook page
}

else {
echo “ERROR”;
}

mysql_close();
header(‘Refresh: 0; URL=…/’);
} else {
die(“Please go back and fill out all the information!”);
// empty variable
}
?>[/php]

This is wrong:
($_POST[$_REQUEST[‘usersname’]])

this is right:
($_POST[‘usersname’])

Sorry for the confusion ;D

Aha great i got it to work it apears i had some kind of error between my submission form and my script itself so i fixed that and now it works spot on and most of all i understood what you said!

Thanks

Sorry to tail the thread like this, but when I see something like
[php]if (!strlen(trim($_REQUEST[‘usersname’]))){
die(‘Please go back and enter out the required name!’);
} [/php]
…I grimace. I understand PHP is weakly typed, so, I suppose 0 is the same as false, you’re checking to see if something is not 0…that is not false…that is true.
Bizarre for someone coming from a strongly typed background… but I suppose it makes some sense. But what if length was 7? So PHP has to determine the truth value of !7 ?? Or would strlen return true so it’s then !true = false. But how do you know if PHP will return a number or a boolean from strlen? Does it return both and the functions/evaluators just know how to translate it? This is more of a general question, since I see stuff like this in PHP all the time.

Sponsor our Newsletter | Privacy Policy | Terms of Service