GamesBond008 Posted April 5, 2023 Share Posted April 5, 2023 <?php if(isset($_POST["submit"])) { $fullName = $_POST["fullname"]; $errors = array(); if(empty($fullName)){ array_push($errors, "Fullname is empty"); if (count($errors)>0) { foreach($errors as $error) { echo $error;} } } ?> <html> <head> <title>Help</title> </head> <body> <input class="submit-btn" name="submit" value="Register"> </body> </html> if i use this code, it says Fullname is empty before i click the button i wanna check if something is missing while someone hits Register Quote Link to comment https://forums.phpfreaks.com/topic/316087-my-isset-function-runs-before-i-click-the-button/ Share on other sites More sharing options...
ginerjm Posted April 5, 2023 Share Posted April 5, 2023 How is this script executed the first time? From the address bar of your web page? Or from a command that you issue? Obviously it has to be started somehow and that means that you either triggered it from a submit that is from a form that was sent to the user by a different script. If not that is your problem. The form in this script is not being sent until your code runs and that is your query. Also - Your form only contains a text field and no buttons. And I wouldn't name my text field 'submit' since it is NOT a submit button. The default value of an input tag is 'text' I believe. Quote Link to comment https://forums.phpfreaks.com/topic/316087-my-isset-function-runs-before-i-click-the-button/#findComment-1607062 Share on other sites More sharing options...
gizmola Posted April 7, 2023 Share Posted April 7, 2023 You really have to use an editor with indentation and make sure that you are matching braces. The code you provided is missing a matching bracket. There's also an obvious problem which is that you output errors, before you've started the html section of the page (in cases where there is an error). This will at best malform your html document. The other issue is that you don't have a form, nor does that form set the method to make a POST, so there's no way that the code can work as you intended. You also don't have a form element to test against Here's something more likely to work, based on what you provided: <?php $errors = array(); if (!empty($_POST["submit"])) { $fullName = $_POST["fullname"]; if (empty($fullName)) { array_push($errors, "Fullname is empty"); } } ?> <!DOCTYPE html> <html> <head> <title>Help</title> </head> <body> <?php if (count($errors)>0): ?> <ul> <?php foreach($errors as $error): ?> <li><?= $error ?></li> <?php endforeach ?> </ul> <?php endif ?> <form method="POST"> <input name="fullname" type="text"> <input class="submit-btn" name="submit" type="submit" value="Register"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316087-my-isset-function-runs-before-i-click-the-button/#findComment-1607100 Share on other sites More sharing options...
XCalibre1 Posted April 11, 2023 Share Posted April 11, 2023 You don't have a form there. Quote Link to comment https://forums.phpfreaks.com/topic/316087-my-isset-function-runs-before-i-click-the-button/#findComment-1607212 Share on other sites More sharing options...
ginerjm Posted April 11, 2023 Share Posted April 11, 2023 If you sit back and look at what you have written it might just come to you. You execute the php code BEFORE even send the html to the client. OF Course it is going to tell you things are empty because at that time THEY ARE. Quote Link to comment https://forums.phpfreaks.com/topic/316087-my-isset-function-runs-before-i-click-the-button/#findComment-1607217 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.