By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

Null isn't empty

P: 3
Hi All
I'm new to php my problem is on sending the form if a specific field is empty how would I stop it being sent. Or would I need to translate empty to NULL or is this a mysql prob with a query. If so could a mod move this to the relevant place pls.
Any help much appreciated.
<?php
$db_host = "localhost";
$db_user = "user";
$db_pwd = "password";
$db_name = "users";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="password" name="pass"><br>
Re-enter: <input type="password" name="pass"><br>
Email: <input type="text" name="email"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST['name'];
$pass = $_POST['pass'];
$enc = md5($pass);
$enc1 = md5($name);
$encf = md5($enc1, $enc);
$email = $_POST['email'];
mysql_query("INSERT INTO `info` (name, pass, email) VALUES ('$name', '$encf', '$email')");
echo "Success! You've been added!";
}
?>
</body>
</html></body>
</html>
Could I use something like ($_POST = "") | also whats the correct syntax to close if is it ]
also to compare a string would this be correct ($pass = $pass1) |
Thanks before hand.
Stef
Jul 12 '06 #1
Share this Question
Share on Google+
5 Replies


sashi
Expert 100+
P: 1,754
Hi there,

coming up with a simple client side validation using javascript would be a good idea.. you can invoke the validation funtion on the OnSubmit event.. look at the below example..

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function validate_required(field,alerttxt)
  5. {
  6. with (field)
  7. {
  8. if (value==null||value=="")
  9.   {alert(alerttxt);return false}
  10. else {return true}
  11. }
  12. }function validate_form(thisform)
  13. {
  14. with (thisform)
  15. {
  16. if (validate_required(email,"Email must be filled out!")==false)
  17.   {email.focus();return false}
  18. }
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
  24. Email: <input type="text" name="email" size="30">
  25. <input type="submit" value="Submit"> 
  26. </form>
  27. </body>
  28. </html>
  29.  
Jul 12 '06 #2

P: 3
Thanks Sashi
Took a couple of secs to get the page it went to right. Tried to incorparate my php into the java page but all works fine now, thanks :)
Stef
Jul 13 '06 #3

P: 3
Just a quick addition, could you tell me do I compare two strings in php like this (!isset($_POST['pass'])=($_POST['pass1'])) |
Stef

Thanks
Jul 13 '06 #4

sashi
Expert 100+
P: 1,754
Hi there,

am not a php kind of person.. yet i found some tips & tricks for you.. see below.. hope it helps you to get started.. good luck my fren.. :)

isset -- Determine whether a variable is set
Description
bool isset ( mixed var [, mixed var [, ...]] )


Returns TRUE if var exists; FALSE otherwise.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

strcmp -- Binary safe string comparison
Description
int strcmp ( string str1, string str2 )

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Note that this comparison is case sensitive.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $pw1 = "yeah";
  3. $pw2 = "yeah";
  4.  
  5. if (strcmp($pw1, $pw2)) {  // This returns false.
  6.    // $pw1 and $pw2 are NOT the same.
  7. } else {
  8.    // $pw1 and $pw2 are the same.
  9. }
  10.  
  11. Where the use of the == operator would give us.:
  12. if ($pw1==$pw2) {    // This returns true.
  13.    // $pw1 and $pw2 are the same.
  14. } else {
  15.    // $pw1 and $pw2 are NOT the same.
  16. }
  17. ?>
  18.  
http://my2.php.net/manual/en/
Jul 13 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Just a quick addition, could you tell me do I compare two strings in php like this (!isset($_POST['pass'])=($_POST['pass1'])) |
Stef

Thanks
What sashi says about using strcmp is good, you can get lots of help from

www.php.net

in the document section, in fact you can download a help file (which I have) which contains everything so you don't keep having to go back to your web-browser.

comparison in php is one of

== equals
=== precisely the same as

as an example

Expand|Select|Wrap|Line Numbers
  1. $a = false;
  2. $b = 0;
  3.  
  4. if ( $a == $b)
  5. {
  6.     echo("A == B");
  7. }
  8.  
  9.  
  10. if ( $a === $b)
  11. {
  12.     echo("A === B");
  13. }
  14.  
this will only output the first echo statement. This is because false is a boolean value and 0 is a integer value. false when convert to a integer takes the value 0 so $a is equal to $b, however $a and $b are not precisely the same because they are of different types.

This is quite an import distinction when you call some library functions which return a value or false if they fail.
Jul 13 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.