Jump to content

Recommended Posts

Ok so i have registration page on my webs but i didn't work on empty fields so how could i check all fields?

//if no name
if(!$name){
$error = "Username missing.";
}
//if no pass
if($pass == false){
$error = "$error,Password missing";
}
//if no pass conf
if($pass_conf == false) {
$error = "$error,Pass conf missing.";
}
//if no email
if($email == false){
$error = "$error,Email missing.";
}
//pass conf
if($pass != $pass_conf){ 
$error = "$error,Passwords do not match.";
    }
    echo "<script>alert('$error');</script>;";
//header("Location: register.php");

My script gives same results every time (even if i fill any of fields)...also if i un-comment header command it wont show alert..it just reloads the page :'(

 

So can anyone help me with this?

Thanks..

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/
Share on other sites

I would try this:

 

<?php

$fileds = array(0=>$name,1=>$pass,2=>$pass_conf,3=>$email);

$formGood = true;

foreach($fileds as $x){

if(empty($x)){

$formGood = false;

}

}

if($fileds[1] != $fileds[2]){

$formGood = false;

}

if($formGood == true){

echo "<script>alert('$error');</script>;";

}

else{

header("Location: register.php");

}

?>

 

Let me know what happens :D

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186687
Share on other sites

I think you didn't understand what i wanna achieve...

If just email and username i empty and rest are filled it should alert error "Username and email fields are empty.." (for each false different error and after user clicks on "ok" button reload page,or just stop script...

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186689
Share on other sites

Try this:

 

<?php

$fileds = array('Username'=>$name,'Password'=>$pass,'Password Confirmation'=>$pass_conf,'Email'=>$email);

$formGood = '';

foreach($fileds as $x){

if(empty($x)){

$formGood .= 'Your '.key($x).' is empty!!!!';

}

}

if($fileds[1] != $fileds[2]){

$formGood .= key($fields[1]).' must equal '.key($fields[2]).'.';

}

if($formGood != ''){

echo "<script>alert('$error');</script>;";

}

else{

header("Location: register.php");

}

?>

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186691
Share on other sites

Warning: key() expects parameter 1 to be array, string given in C:\xampp\htdocs\login\check.php on line 13

 

Warning: key() expects parameter 1 to be array, string given in C:\xampp\htdocs\login\check.php on line 13

 

Notice: Undefined offset: 1 in C:\xampp\htdocs\login\check.php on line 16

 

Notice: Undefined offset: 2 in C:\xampp\htdocs\login\check.php on line 16

 

Notice: Undefined variable: error in C:\xampp\htdocs\login\check.php on line 20

;

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186697
Share on other sites

Try this:

 

<?php

$fileds = array('Username'=>$name,'Password'=>$pass,'Password Confirmation'=>$pass_conf,'Email'=>$email);

$formGood = '';

foreach($fileds as $x){

if(empty($x)){

$formGood .= 'Your '.array_search($x).' is empty!!!!';

}

}

if($fileds[1] != $fileds[2]){

$formGood .= array_search($fields[1]).' must equal '.array_search($fields[2]).'.';

}

if($formGood != ''){

echo "<script>alert('$error');</script>;";

}

else{

header("Location: register.php");

}

?>

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186704
Share on other sites

Sorry to have posted so much.  Here is the script again.  I tested and debugged so I know it works:

 

<?php

$name = 'asdf';

$pass = 'dan';

$pss_conf = 'd';

$fields = array('Username'=>$name,'Password'=>$pass,'Password Confirmation'=>$pass_conf,'Email'=>$email);

$formGood = '';

foreach($fields as $key=>$x){

if(empty($x)){

$formGood .= 'Your '.$key.' is empty!!!!';

}

}

if($fields['Password'] != $fields['Password Confirmation']){

$formGood .= 'Your Password must be equal to the Password Confirmation.';

}

if($formGood != ''){

echo "<script>alert('$formGood');</script>";

}

else{

header("Location: register.php");

}

?>

 

I removed the ';' that you had after the '</script>' tag.  empty() is a better method than false as it will check for more values that are considered 'empty'.  See: http://www.php.net/manual/en/function.empty.php

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186714
Share on other sites

You should be able to go to a different location via javascript.  The alert function is syncronous, meaning that it will halt execution of all other javascript untill the ok button is pressed.  If you want to redirect the client after they receive the warning try replacing the bottom part of the above script with the following:

 

continued from above...

_____________________________

if($formGood != ''){

echo "<script>alert('$formGood');window.location=register.php;</script>";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186893
Share on other sites

An empty string should return false, however when assigned the value to an empty $_POST value it returns true (for some reason).

 

You basically needed one function, empty() which returns true if it is indeed empty.

 

So:

if (empty($name))
{echo "The username is empty";
}

 

You can also add ! before empty: !empty($name) which does the opposite (checks if it is NOT empty).

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186916
Share on other sites

I needed to backslash quotes in echo line...it works now just there is an error :S

After alert message shows up and after i press ok it reloads the page as nothing happened,on the first sight it looks great,but i've just checked my database and saw that it actually crates a user with empty fields (even if the page is redirected)...

Can it be made for script to stop at one place? like:

	if($formGood != ''){
		echo "<script>alert('$formGood');window.location=\"register.php\";</script>";
//stop here!
	}

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1186933
Share on other sites

Backslashes can be voided by using single quotes (I should've used them to begin with).

 

Using the die() function will make a final output and then halt the script from further exectuion:

 

-------Code------------

if($formGood != ''){

die("<script>alert('$formGood');</script>");

}

echo 'foobar rama!';

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1187334
Share on other sites

That's not a very good way to do it. The user should be presented with the original form, with the previously entered values pre-filled in their respective fields, and either a list of errors, or the individual errors near each field instead of having to fill out everything again. Not to mention that a user without javascript enabled will get nothing but a blank screen using the method above.

 

Each field should be validated to make sure it contains the expected data. Store the errors in an array, and if the array is empty after the validation routine, there were no errors detected, and the code can proceed. Moreover, using die() when there's an error is like slamming the door in a customer's face. There are much more user friendly ways of handling it. Take the following code as an example, and you should have a good start on the logic needed to do this properly.

 

<?php
// Uncomment next line for debug.
//echo '<pre>'; print_r($_POST); echo '</pre>';

if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted
$errors = array(); // initialize an array to hold validation errors
$_POST = array_map('trim', $_POST); // trim all $_POST array values

if( !empty($_POST['name']) ) { // validate the name field
	if( !ctype_alpha($_POST['name']) ) {
		$errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error
	}
	if( strlen($_POST['name']) < 3 || strlen($_POST['name'] > 20) ) {
		$errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error
	}
} else {
	$errors[] = 'Name is a required field.'; // if name is empty, store error
}

if( !empty($_POST['number']) ) { // same validations as in name, above.
	if( !ctype_digit($_POST['number']) ) {
		$errors[] = 'Number must be numeric.';
	}
	if( strlen($_POST['number']) < 5 || strlen($_POST['number']) > 10 )  {
		$error = 'Number must be from 3 to 20 digits. It is currently ' . strlen($_POST['number']) . ' digit';
		$error .= strlen($_POST['number']) == 1 ? '.' : 's.';
		$errors[] = $error;
	}
} else {
	$errors[] = 'Number is a required field.';
}

if( !empty($errors) ) {  // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form
	echo "<font color=\"red\">The following errors were detected:<br>";
	echo implode("<br>\n", $errors);
	echo '</font>';
}
}
if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) {
?>
<form method="post">
Name (3-20 letters): <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>"><br>
Number (5-10 numbers): <input type="text" name="number" value="<?php echo isset($_POST['number']) ? $_POST['number'] : ''; ?>"><br>
<input type="hidden" name="submitted" value="yes">
<input type="submit" name="submit" value="Submit">
</form>
<?php
} else {
// Form was submitted, and validated with no errors. OK to run db insert, display success message, etc.
echo "Successful submission!";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1187419
Share on other sites

I think that's all i need...just could you guide me through the code...i understand most of it but..what would commented part of the code do?

 

What this first line means? and what should 'triming' mean?

if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) {
$errors = array();
$_POST = array_map('trim', $_POST);

 

And this one :P

if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) {
?>

 

And i should add to

<form method="post">

action="do_reg.php"  ?

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1187472
Share on other sites

Ok,forget about last post,i get it all now...anyhow i made it work on simple html form but i can't add this php to my form on my page:

<?php
// Uncomment next line for debug.
echo '<pre>'; print_r($_POST); echo '</pre>';

if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted
$errors = array(); // initialize an array to hold validation errors
$_POST = array_map('trim', $_POST); // trim all $_POST array values

if( !empty($_POST['username']) ) { // validate the name field
	if( !ctype_alnum($_POST['username']) ) {
		$errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error
	}
	if( strlen($_POST['username']) < 3 || strlen($_POST['username'] > 20) ) {
		$errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error
	}
} else {
	$errors[] = 'Name is a required field.'; // if name is empty, store error
}

if( !empty($_POST['password']) ) { // same validations as in name, above.
	if( !ctype_alnum($_POST['password']) ) {
		$errors[] = 'Password must be alphanumeric charachters only.';
	}
	if( strlen($_POST['password']) < 3 || strlen($_POST['password']) > 20 )  {
		$error = 'Password must be from 3 to 20 charachters. It is currently ' . strlen($_POST['password']) . ' digit';
		$errors[] = $error;
	}
} else {
	$errors[] = 'Password is a required field.';
}

	if( !empty($_POST['password2']) ) { // same validations as in name, above.
	if($_POST['password2'] != $_POST['password'])  {
		$error = 'Passwords do not match.' . 
		$errors[] = $error;
	}
} else {
	$errors[] = 'Confirm your password.';
}

	if(empty($_POST['email']) ) { // same validations as in name, above.
	$errors[] = 'Email is a required field.';
}

if( !empty($errors) ) {  // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form
	echo "<font color=\"red\">The following errors were detected:<br>";
	echo implode("<br>\n", $errors);
	echo '</font>';
}
}
if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) {
?>

<html>
<head>
<title>Register!</title>
</head>

<body>

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<table border="5" align="center" cellpadding="5" cellspacing="5" bordercolor="#333333">
  <tr>
    <td width="400" align="left" valign="middle">
<table border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#333333">
  <tr>
    <td align="left" valign="middle">
<form action="do_reg.php" method="post" id="info">
  <h2 align="center" class="style10">Register:</h2>
    <div id="username-wrap" class="slider">
        <label for="username">Username:</label>
        <input type="text" id="name" name="username" autocomplete="off" style="background-color: #000000; color: #999999;" />

    </div><!--/#username-wrap-->

    <div id="pass-wrap"  class="slider">
        <label for="password">Password:</label>
        <input type="password" id="pass" name="password" autocomplete="off" style="background-color: #000000; color: #999999;" />
    </div><!--/#password-wrap-->

<div id="pass2-wrap"  class="slider">
        <label for="password2">Repeat:</label>
        <input type="password" id="pass2" name="pass_conf" autocomplete="off" style="background-color: #000000; color: #999999;" />
    </div><!--/#password2-wrap-->

    <div id="email-wrap"  class="slider">
        <label for="email">Email:</label>
        <input type="text" id="email" name="email" autocomplete="off" style="background-color: #000000; color: #999999;" />
    </div><!--/#email-wrap-->

    <div align="center">
<input type=hidden name=ip value='<?php echo $ip ?>'/>
      <input type="submit" id="btn" name="submit" value="Submit" autocomplete="off" style="font-size: 17px; font-weight: bold; background-color: #000000; color: #CCCCCC;"/>
    </div>
    </form></td>
  </tr>
</table>

</td>
  </tr>
</table>

</body>
</html>

 

I get error on last line (</html>) how to make this work??? :S

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1188598
Share on other sites

Can it be made for script to stop at one place? like:

	if($formGood != ''){
		echo "<script>alert('$formGood');window.location=\"register.php\";</script>";
//stop here!
	}

 

Yes u can use exit(); function wherever and whenever you want your script to stop and don't continue any further. :)

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1188675
Share on other sites

@Pikachu

 

When i do this:

<?php
//php code
{
<html>
//html block
</html>
}
?>

I get this error:

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\login\register1.php on line 54

54 line = <html>

 

And when i do this:

<?php
//php code
{
?>
<html>
//html block
</html>
<?php
}
?>

It doesn't show any errors but it wont echo errors...i just echoes debug lines...

Array

(

    [username] => dsadsa

    [password] => dasdas

    [pass_conf] => dsadas

    => dasdas

    [submit] => Submit

)

 

@aabid

Thanks thats useful info :)

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1188772
Share on other sites

<?php
// Uncomment next line for debug.
echo '<pre>'; print_r($_POST); echo '</pre>';

if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted
$errors = array(); // initialize an array to hold validation errors
$_POST = array_map('trim', $_POST); // trim all $_POST array values

if( !empty($_POST['username']) ) { // validate the name field
	if( !ctype_alnum($_POST['username']) ) {
		$errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error
	}
	if( strlen($_POST['username']) < 3 || strlen($_POST['username'] > 20) ) {
		$errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error
	}
} else {
	$errors[] = 'Name is a required field.'; // if name is empty, store error
}

if( !empty($_POST['password']) ) { // same validations as in name, above.
	if( !ctype_alnum($_POST['password']) ) {
		$errors[] = 'Password must be alphanumeric charachters only.';
	}
	if( strlen($_POST['password']) < 3 || strlen($_POST['password']) > 20 )  {
		$error = 'Password must be from 3 to 20 charachters. It is currently ' . strlen($_POST['password']) . ' digit';
		$errors[] = $error;
	}
} else {
	$errors[] = 'Password is a required field.';
}

if( !empty($_POST['password2']) ) { // same validations as in name, above.
	if($_POST['password2'] != $_POST['password'])  {
		$error = 'Passwords do not match.' . 
		$errors[] = $error;
	}
} else {
	$errors[] = 'Confirm your password.';
}

	if(empty($_POST['email']) ) { // same validations as in name, above.
	$errors[] = 'Email is a required field.';
}

if( !empty($errors) ) {  // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form
	echo "<font color=\"red\">The following errors were detected:<br>";
	echo implode("<br>\n", $errors);
	echo '</font>';
}
}
if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) {
?>

<html>
<head>
<title>Register!</title>
</head>
<body>
<table border="5" align="center" cellpadding="5" cellspacing="5" bordercolor="#333333">
  <tr>
    <td width="400" align="left" valign="middle">
<table border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#333333">
  <tr>
    <td align="left" valign="middle">
<form method="post" id="info">
  <h2 align="center" class="style10">Register:</h2>
    <div id="username-wrap" class="slider">
        <label for="username">Username:</label>
        <input type="text" id="name" name="username" autocomplete="off" style="background-color: #000000; color: #999999;" />

    </div><!--/#username-wrap-->

    <div id="pass-wrap"  class="slider">
        <label for="password">Password:</label>
        <input type="password" id="pass" name="password" autocomplete="off" style="background-color: #000000; color: #999999;" />
    </div><!--/#password-wrap-->

<div id="pass2-wrap"  class="slider">
        <label for="password2">Repeat:</label>
        <input type="password" id="pass2" name="pass_conf" autocomplete="off" style="background-color: #000000; color: #999999;" />
    </div><!--/#password2-wrap-->

    <div id="email-wrap"  class="slider">
        <label for="email">Email:</label>
        <input type="text" id="email" name="email" autocomplete="off" style="background-color: #000000; color: #999999;" />
    </div><!--/#email-wrap-->

    <div align="center">

      <input type="submit" id="btn" name="submit" value="Submit" autocomplete="off" style="font-size: 17px; font-weight: bold; background-color: #000000; color: #CCCCCC;"/>
    </div>
    </form></td>
  </tr>
</table>
</td>
  </tr>
</table>
</body>
</html>

<?php
}
?>

 

Sorry for bothering so much :S

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1188917
Share on other sites

You removed the hidden field that is checked for to see if the form has been submitted. Add it in to the form again, and that should take care of that.

<input type="hidden" name="submitted" value="yes">

 

And why did you remove the code that allows the values that were submitted to be echoed into the fields if there was a validation error? Now you're right back to square one; if the user makes an error in any of the fields, they have to fill everything out all over again.

 

 

Link to comment
https://forums.phpfreaks.com/topic/230441-empty-field-check/#findComment-1188936
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.