Jump to content

Are My Issets And &s Correct ?


phpsane

Recommended Posts

Php Mates,

I have built an html form where atleast one of the input fields must be filled. If not one filled atleast then show error.

I tried these ways but none of them work:

	<?php 
	//Required PHP Files. 
include 'config.php'; //Required on all webpages of the site. 
	//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE) 
{ 
    //Redirect User to Log-in Page immediately. 
    //header("refresh:0; url=home.php"); 
    header("location:login.php"); 
    exit(); 
} 
	if ($_SERVER['REQUEST_METHOD'] == "POST") 
{ 
    //Step 2: Check User submitted details. 
    
    //Check if User made any inputs or not. 
    if (!isset($_POST["one"]) & 
       !isset($_POST["two"]) & 
       !isset($_POST["three"]) & 
       !isset($_POST["four"]) &
       !isset($_POST["five"])) 
    { 
        echo "not ISSET";  
        echo "You must atleast make an input in one field"; 
    } 
    else 
    { 
        echo "ISSET"; 
        echo ""; //How to show the ISSET one here ? (The field that has been filled in).
    }
	}
?> 
	<form method="post" action=""> 
    <p align="left"><h2>QuickLink Submission Form</h2></p> 
    <div class="form-group"> 
        <p align="left"><label>One:</label> 
        <input type="url" name="one" autocorrect=off value="<?php if(isset($_POST['one'])) { echo htmlentities($_POST['one']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Two:</label> 
        <input type="url" name="two" autocorrect=off value="<?php if(isset($_POST['two'])) { echo htmlentities($_POST['two']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Three:</label> 
        <input type="url" name="three" autocorrect=off value="<?php if(isset($_POST['three'])) { echo htmlentities($_POST['three']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Four:</label> 
        <input type="url" name="four" autocorrect=off value="<?php if(isset($_POST['four'])) { echo htmlentities($_POST['four']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Five:</label> 
        <input type="url" name="five" autocorrect=off value="<?php if(isset($_POST['five'])) { echo htmlentities($_POST['five']); }?>"></p> 
    </div> 
    <p align="left"><button type="submit" class="btn btn-default" name=submit">Submit!</button></p> 
</form> 
</div> 
</body> 
</html>
	

 

I get error:

Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\test\settings.php on line 27

Q1. What is wrong ?

Line 27 is this: 

	echo "You must atleast make an input in one field";
	

 

Ok. Making another attempt ....

	<?php 
	//Required PHP Files. 
include 'config.php'; //Required on all webpages of the site. 
	//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE) 
{ 
    //Redirect User to Log-in Page immediately. 
    //header("refresh:0; url=home.php"); 
    header("location:login.php"); 
    exit(); 
} 
	if ($_SERVER['REQUEST_METHOD'] == "POST") 
{ 
    //Step 2: Check User submitted details. 
    
    //Check if User made any inputs or not. 
    if (!isset($_POST["one"]) & 
       !isset($_POST["two"]) & 
       !isset($_POST["three"]) & 
       !isset($_POST["four"]) &
       !isset($_POST["five"])) 
    { 
        echo "not ISSET";  
    } 
    else 
    { 
        echo "ISSET"; 
        echo ""; //How to show the ISSET one here ? (The field that has been filled in).
    }
}    
?> 
	

 

Same  error.

Q2. Have I got my issets and my &s correct to achieve what I want ? If not, then care to show another way with a code sample.

Do bare in mind that, I want php to give error if atleast one input field is not filled out of the five.

 

Thanks!

 

 

Link to comment
Share on other sites

3 hours ago, phpsane said:

//How to show the ISSET one here ? (The field that has been filled in).

By checking each field individually to see if it is set. Basically the exact opposite of what you had done in the if condition.

3 hours ago, phpsane said:

Q1. What is wrong ?

I don't see anything wrong with the line. Delete it, delete the one before it too, and type them out again.

3 hours ago, phpsane said:

Q2. Have I got my issets and my &s correct to achieve what I want ?

Actually no. You have the general form correct, except isset is not the right function to use. You're also using &s which will work but && is more appropriate: the first one is used to manipulate numbers, the second is an "and" operator.

When a field is empty in a form, it will still be set in $_POST. The value will be an empty string, but it will be set.

You need another way to check if the field is "set". Like by checking that the value is not an empty string.

Link to comment
Share on other sites

1 hour ago, requinix said:

By checking each field individually to see if it is set. Basically the exact opposite of what you had done in the if condition.

I don't see anything wrong with the line. Delete it, delete the one before it too, and type them out again.

Actually no. You have the general form correct, except isset is not the right function to use. You're also using &s which will work but && is more appropriate: the first one is used to manipulate numbers, the second is an "and" operator.

When a field is empty in a form, it will still be set in $_POST. The value will be an empty string, but it will be set.

You need another way to check if the field is "set". Like by checking that the value is not an empty string.

Thank You!

Now, if an input field is empty and it is still set in $_POST then what is the use of checking if the input field is set or not ?

	if (isset($_POST["one"]) {
	do this;
	else {
	do that
	       }
	

 

I know you are hinting to use something like this as I saw about 2 samples like this after googling few hrs ago some-wheres. Gonna try this now.

	if (empty($_POST["one"])
	

Link to comment
Share on other sites

isset() tells you if the field was sent with the form, which normally means the form was submitted. It can also tell you which button was used to submit the form.

You don't care about the button and you already checked the form was submitted by using REQUEST_METHOD so isset() isn't the right one to use. You can safely assume the field was submitted with the form.

And yes, empty() would do the job, as long as you don't mind considering "0" as empty. Remember that: if you try it on a number field and the user enters 0, empty() will tell you the field is empty. If you can remember to consider that every time you use empty() then go ahead and use it, otherwise stick to the safer =="" method.

Link to comment
Share on other sites

21 minutes ago, requinix said:

isset() tells you if the field was sent with the form, which normally means the form was submitted. It can also tell you which button was used to submit the form.

You don't care about the button and you already checked the form was submitted by using REQUEST_METHOD so isset() isn't the right one to use. You can safely assume the field was submitted with the form.

And yes, empty() would do the job, as long as you don't mind considering "0" as empty. Remember that: if you try it on a number field and the user enters 0, empty() will tell you the field is empty. If you can remember to consider that every time you use empty() then go ahead and use it, otherwise stick to the safer =="" method.

Thanks!

I learnt 2 things tonight. But, does this really feel right ?

	if ($_SERVER['REQUEST_METHOD'] == "POST") 
{ 
    //Step 2: Check User submitted details. 
    
    //Check if User made any inputs or not. 
    if (($_POST["one"]) == "" && 
       ($_POST["two"]) == "" & 
       ($_POST["three"]) == "" && 
       ($_POST["four"]) == "" &&
       ($_POST["five"])) { 
        
        echo "not ISSET";  
    } else { 
        echo "SET";
    }
} 
?> 
	

I still get the same error regarding line 29 which still looks like this:

	    } else { 
	

Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\test\settings.php on line 29

Full code update:

	<?php 
	//Required PHP Files. 
include 'config.php'; //Required on all webpages of the site. 
	//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE) 
{ 
    //Redirect User to Log-in Page immediately. 
    //header("refresh:0; url=home.php"); 
    header("location:login.php"); 
    exit(); 
} 
	$user = $_SESSION["user"]; 
	if ($_SERVER['REQUEST_METHOD'] == "POST") 
{ 
    //Step 2: Check User submitted details. 
    
    //Check if User made any inputs or not. 
    if (($_POST["one"]) == "" && 
       ($_POST["two"]) == "" & 
       ($_POST["three"]) == "" && 
       ($_POST["four"]) == "" &&
       ($_POST["five"])) { 
        
        echo "not ISSET";  
    } else { 
        echo "SET";
    }
} 
?> 
	<!DOCTYPE html> 
<html> 
    <head> 
        <title><?php echo "$social_network_name";?> Settings Page</title> 
    </head> 
<body> 
<div class ="container"> 
	<?php 
//Error Messages. 
if (isset($_SESSION['error']) && !empty($_SESSION['error'])) { 
    echo '<p style="color:red;">'.$_SESSION['error'].'</p>'; 
} 
?> 
	<?php 
//Session Messages. 
if (isset($_SESSION['message']) && !empty($_SESSION['message'])) { 
    echo '<p style="color:red;">'.$_SESSION['error'].'</p>'; 
} 
?> 
	<?php 
//Clear Registration Session. 
function clear_settings_session() 
    { 
        //Clear the User Form inputs, Session Messages and Session Errors so they can no longer be used. 
        unset($_SESSION['message']); 
        unset($_SESSION['error']); 
        unset($_POST); 
        exit(); 
    } 
?> 
	<form method="post" action=""> 
    <p align="left"><h2>QuickLink Submission Form</h2></p> 
    <div class="form-group"> 
        <p align="left"><label>One:</label> 
        <input type="url" name="one" autocorrect=off value="<?php if(isset($_POST['one'])) { echo htmlentities($_POST['one']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Two:</label> 
        <input type="url" name="two" autocorrect=off value="<?php if(isset($_POST['two'])) { echo htmlentities($_POST['two']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Three:</label> 
        <input type="url" name="three" autocorrect=off value="<?php if(isset($_POST['three'])) { echo htmlentities($_POST['three']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Four:</label> 
        <input type="url" name="four" autocorrect=off value="<?php if(isset($_POST['four'])) { echo htmlentities($_POST['four']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Five:</label> 
        <input type="url" name="five" autocorrect=off value="<?php if(isset($_POST['five'])) { echo htmlentities($_POST['five']); }?>"></p> 
    </div> 
    <p align="left"><input type="submit" class="btn btn-default" name="submit" value = "submit">Submit!</button></p> 
</form> 
</div> 
</body> 
</html>
	

Link to comment
Share on other sites

7 minutes ago, requinix said:

Delete all those lines around that and type them again.

I deleted line 29 and  retyped it and error is gone! How come ? Everything working as expected!

Anyway, is my submit button ok or not ?

	<?php 
	//Required PHP Files. 
include 'config.php'; //Required on all webpages of the site. 
	//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE) 
{ 
    //Redirect User to Log-in Page immediately. 
    //header("refresh:0; url=home.php"); 
    header("location:login.php"); 
    exit(); 
} 
	$user = $_SESSION["user"]; 
	if ($_SERVER['REQUEST_METHOD'] == "POST") 
{ 
    //Step 2: Check User submitted details. 
    
    //Check if User made any inputs or not. 
    if (($_POST["one"]) == "" && 
       ($_POST["two"]) == "" & 
       ($_POST["three"]) == "" && 
       ($_POST["four"]) == "" &&
       ($_POST["five"])) { 
        
        echo "All fields are empty!"; 
       } else { 
        echo "Atleast one field has been filled"; 
       } 
} 
?> 
	<!DOCTYPE html> 
<html> 
    <head> 
        <title><?php echo "$social_network_name";?> Settings Page</title> 
    </head> 
<body> 
<div class ="container"> 
	<?php 
//Error Messages. 
if (isset($_SESSION['error']) && !empty($_SESSION['error'])) { 
    echo '<p style="color:red;">'.$_SESSION['error'].'</p>'; 
} 
?> 
	<?php 
//Session Messages. 
if (isset($_SESSION['message']) && !empty($_SESSION['message'])) { 
    echo '<p style="color:red;">'.$_SESSION['error'].'</p>'; 
} 
?> 
	<?php 
//Clear Registration Session. 
function clear_settings_session() 
    { 
        //Clear the User Form inputs, Session Messages and Session Errors so they can no longer be used. 
        unset($_SESSION['message']); 
        unset($_SESSION['error']); 
        unset($_POST); 
        exit(); 
    } 
?> 
	<form method="post" action=""> 
    <p align="left"><h2>QuickLink Submission Form</h2></p> 
    <div class="form-group"> 
        <p align="left"><label>One:</label> 
        <input type="url" name="one" autocorrect=off value="<?php if(isset($_POST['one'])) { echo htmlentities($_POST['one']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Two:</label> 
        <input type="url" name="two" autocorrect=off value="<?php if(isset($_POST['two'])) { echo htmlentities($_POST['two']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Three:</label> 
        <input type="url" name="three" autocorrect=off value="<?php if(isset($_POST['three'])) { echo htmlentities($_POST['three']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Four:</label> 
        <input type="url" name="four" autocorrect=off value="<?php if(isset($_POST['four'])) { echo htmlentities($_POST['four']); }?>"></p> 
    </div> 
    <div class="form-group"> 
        <p align="left"><label>Five:</label> 
        <input type="url" name="five" autocorrect=off value="<?php if(isset($_POST['five'])) { echo htmlentities($_POST['five']); }?>"></p> 
    </div> 
    <p align="left"><input type="submit" class="btn btn-default" name="submit" value ="submit"></p> 
</form> 
</div> 
</body> 
</html>
	

 

What do you think about these lines ?

	<html> 
    <head> 
        <title><?php echo "$social_network_name";?> Settings Page</title> 
    </head> 
<body> 
<div class ="container"> 
	<?php 
//Error Messages. 
if (isset($_SESSION['error']) && !empty($_SESSION['error'])) { 
    echo '<p style="color:red;">'.$_SESSION['error'].'</p>'; 
} 
?> 
	<?php 
//Session Messages. 
if (isset($_SESSION['message']) && !empty($_SESSION['message'])) { 
    echo '<p style="color:red;">'.$_SESSION['error'].'</p>'; 
} 
?> 
	<?php 
//Clear Registration Session. 
function clear_settings_session() 
    { 
        //Clear the User Form inputs, Session Messages and Session Errors so they can no longer be used. 
        unset($_SESSION['message']); 
        unset($_SESSION['error']); 
        unset($_POST); 
        exit(); 
    } 
?> 
	

Link to comment
Share on other sites

Requinix,

Do you see my previous post' code ?

Especially, note my session messages and session errors and the error echoes. I reckon I got my wires crossed here like missing a few lines and so care to fix my code for our learning purpose and demonstrate here how these session messages and session errors should be ?

Link to comment
Share on other sites

16 minutes ago, phpsane said:

Everything working as expected!

Then you testing procedures are definitely suspect.

This code is incorrect:

if (($_POST["one"]) == "" && 
       ($_POST["two"]) == "" & 
       ($_POST["three"]) == "" && 
       ($_POST["four"]) == "" &&
       ($_POST["five"])) { 
        
        echo "All fields are empty!"; 
       } else { 
        echo "Atleast one field has been filled"; 
       } 
Link to comment
Share on other sites

4 minutes ago, Barand said:

Then you testing procedures are definitely suspect.

This code is incorrect:


if (($_POST["one"]) == "" && 
       ($_POST["two"]) == "" & 
       ($_POST["three"]) == "" && 
       ($_POST["four"]) == "" &&
       ($_POST["five"])) { 
        
        echo "All fields are empty!"; 
       } else { 
        echo "Atleast one field has been filled"; 
       } 

Ok. It should be without the typo:

	if (($_POST["one"]) == "" && 
       ($_POST["two"]) == "" && 
       ($_POST["three"]) == "" && 
       ($_POST["four"]) == "" &&
       ($_POST["five"])) { 
	

 

But, should I rid the spaces ?

	if (($_POST["one"])=="" && 
       ($_POST["two"])=="" && 
       ($_POST["three"])=="" && 
       ($_POST["four"])=="" &&
       ($_POST["five"])) { 
	

 

Link to comment
Share on other sites

33 minutes ago, phpsane said:

I deleted line 29 and  retyped it and error is gone! How come ?

You likely inserted a non-breaking space somewhere in there. Probably with shift+space. PHP doesn't consider those like plain spaces so finding one will confuse it.

Link to comment
Share on other sites

On 10/14/2018 at 3:28 AM, Barand said:

How about checking if $_POST['five'] == "" also?

And what happens if the user enters a space into each of the fields?

So, you suggest I do this ? :

	if (($_POST["one"]) == "" || == " " && 
       ($_POST["two"]) == "" || == " " && 
       ($_POST["three"]) == "" || == " " && 
       ($_POST["four"]) == "" || == " " && 
       ($_POST["five"] == "" || == " " && )) 
	

Yes or no ? 

Shall I remove the spaces ? 

if (($_POST["one"])==""||==" "&& 
       ($_POST["two"])==""||==" "&& 
       ($_POST["three"])==""||==" "&& 
       ($_POST["four"])==""||==" "&& 
       ($_POST["five"]==""||==" "&&))
Link to comment
Share on other sites

Ok. Working:

	//Check if User made any inputs or not. 
    if ((($_POST["one"]) == "" || ($_POST["one"]) == " ") && 
       (($_POST["two"]) == "" || ($_POST["two"]) == " ") && 
       (($_POST["three"]) == "" || ($_POST["three"]) == " ") && 
       (($_POST["four"]) == "" || ($_POST["four"])== " ") && 
       (($_POST["five"]) == "" || ($_POST["five"]) == " ")) 
    { 
	

 

But, trying to use trim instead. Need help:

	if (trim($_POST["one"]) == "" && 
       trim($_POST["two"]) == "" && 
       trim($_POST["three"]) == "" && 
       trim($_POST["four"]) == "" && 
       trim($_POST["five"]) == "") 
    { 
	

Get error:

Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\test\settings_v1.php on line 32

Is ok:

	if (trim($_POST["one"]) == "" && 
       trim($_POST["two"]) == "" && 
       trim($_POST["three"]) == "" && 
       trim($_POST["four"]) == "" && 
       trim($_POST["five"]) == "") 
    { 
	

Seems to be working. :) 

Link to comment
Share on other sites

Unless you created a new syntax error with your editing, we're back to the original problem of you typing a non-breaking space again. Delete lines 31-33 and write them out again, then get yourself an editor that doesn't think shift+space should be a special character (or disable any setting that might be adding them).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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