phpsane Posted October 13, 2018 Share Posted October 13, 2018 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! Quote Link to comment Share on other sites More sharing options...
requinix Posted October 13, 2018 Share Posted October 13, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2018 Share Posted October 13, 2018 3 hours ago, phpsane said: I have built an html form where atleast one of the input fields must be filled 3 hours ago, phpsane said: I want php to give error if atleast one input field is not filled out of the five Which of those is it? Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 13, 2018 Author Share Posted October 13, 2018 1 hour ago, Barand said: Which of those is it? Anyone of the fields. It is like I am asking you 5 questions and I expect you to answer anyone atleast. Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 13, 2018 Author Share Posted October 13, 2018 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"]) Quote Link to comment Share on other sites More sharing options...
requinix Posted October 13, 2018 Share Posted October 13, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 13, 2018 Author Share Posted October 13, 2018 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> Quote Link to comment Share on other sites More sharing options...
requinix Posted October 13, 2018 Share Posted October 13, 2018 1 minute ago, phpsane said: I still get the same error regarding line 29 which still looks like this: Delete all those lines around that and type them again. Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 13, 2018 Author Share Posted October 13, 2018 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(); } ?> Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 13, 2018 Author Share Posted October 13, 2018 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 ? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2018 Share Posted October 13, 2018 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"; } Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 13, 2018 Author Share Posted October 13, 2018 (edited) 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"])) { Edited October 13, 2018 by phpsane Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2018 Share Posted October 13, 2018 How about checking if $_POST['five'] == "" also? And what happens if the user enters a space into each of the fields? 1 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 13, 2018 Share Posted October 13, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 15, 2018 Author Share Posted October 15, 2018 (edited) 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"]==""||==" "&&)) Edited October 15, 2018 by phpsane Quote Link to comment Share on other sites More sharing options...
Barand Posted October 15, 2018 Share Posted October 15, 2018 24 minutes ago, phpsane said: Yes or no ? No. Your code needs to be syntactically correct. Try using trim() 1 Quote Link to comment Share on other sites More sharing options...
phpsane Posted October 15, 2018 Author Share Posted October 15, 2018 (edited) 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. Edited October 15, 2018 by phpsane Quote Link to comment Share on other sites More sharing options...
requinix Posted October 15, 2018 Share Posted October 15, 2018 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). 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.