Alexa Posted September 29, 2020 Share Posted September 29, 2020 Hi! I am trying to use the View Model Design Pattern in my application The problem is that it is the first time I use it and I am a beginner. So, I have the logic in the Model and the "front end" in View. I am talking about a sign up page. In the current Html file I am trying to access an error array from the model file and display it to the user above the input field. Here is the a snippet from the Model (Signup.php) $account = new Account($con); //the account which takes as param the db connection $error = null; //the array // anything the user writes gets inside this array if (isset($_POST["submitButton"])) { $firstName = UnifyFormInput::unifyUserFLName($_POST["firstName"]); $lastName = UnifyFormInput::unifyUserFLName($_POST["lastName"]); $username = UnifyFormInput::unifyFormUserName($_POST["username"]); $email = UnifyFormInput::unifyFormEmail($_POST["email"]); $password = UnifyFormInput::unifyFormPassword($_POST["password"]); //contain true or false based on the query being successful or not $success = $account->register($firstName, $lastName, $username, $email, $password); if ($success) { $_SESSION["userLoggedIn"] = $username; header("Location:index.php"); }else{ $error = $account->getError(Constants::$registerFailed); } } //compact — creates array containing variables and their values //call render function from View file to show the register page content View::render('register', compact('error')); And here is the view: <?php if (!empty($error)) { echo $account->getError(Constants::$loginFailed); } ?> <input type="text" class="form-control" name="firstName" placeholder= "First Name" value="<?php getInputValue("firstName"); ?>" required> Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/ Share on other sites More sharing options...
requinix Posted September 29, 2020 Share Posted September 29, 2020 Seems reasonable enough. What's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581646 Share on other sites More sharing options...
Alexa Posted September 30, 2020 Author Share Posted September 30, 2020 (edited) The error is not displayed. Actually, nothing is displayed. Only a refresh to the page is done. I used the same piece of code for the login page, and there it works wonders. Edited September 30, 2020 by Alexa Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581653 Share on other sites More sharing options...
requinix Posted September 30, 2020 Share Posted September 30, 2020 Well... Given just that code it's going to be really hard for anyone besides you to understand what the problem could be. You say the page is refreshing but it sounds like that's just the regular page load from submitting the form. Write out a list of steps that the code is supposed to follow, then verify each one in turn. For example, one of those steps is going to be that the code checks if the "submitButton" was pressed, so you need to verify that (1) there is a submit button, (2) it is named "submitButton", (3) it has a value so that it can be sent along with the form, and (4) you are using it to submit the form. Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581655 Share on other sites More sharing options...
Alexa Posted September 30, 2020 Author Share Posted September 30, 2020 (edited) Ok. I will try it. Can I also attach the whole files for you to be easier to check ?:) Edited September 30, 2020 by Alexa Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581656 Share on other sites More sharing options...
benanamen Posted September 30, 2020 Share Posted September 30, 2020 Quote one of those steps is going to be that the code checks if the "submitButton" was pressed, so you need to verify that (1) there is a submit button, (2) it is named "submitButton", (3) it has a value so that it can be sent along with the form, and (4) you are using it to submit the form. C'mon @requinix, you know that's not how to do it. In fact, that's exactly how not to do it. Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581658 Share on other sites More sharing options...
Alexa Posted September 30, 2020 Author Share Posted September 30, 2020 And benanamen, then, how should I do it?😀 Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581659 Share on other sites More sharing options...
benanamen Posted September 30, 2020 Share Posted September 30, 2020 (edited) What you need to do is check the REQUEST METHOD for a post request and then handle the form validation, processing, etc. The code should not care about the name of a submit button and can actually completely fail in certain cases by doing so. The submit button doesn't even need the name attribute at all and it's value should not even matter to your code. As to #4, if you want/need to insist data is only submitted from your form then you need to implement CSRF protection and maybe a nonce. if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Do processing } * This is in response to a previous post, not specifically to your OP. Edited September 30, 2020 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581661 Share on other sites More sharing options...
benanamen Posted September 30, 2020 Share Posted September 30, 2020 @Alexa, If you can put your entire app on GitHub I will take a look at it. You can make it a private repo if you don't want the world to see it. It will be easier to properly help you if I can see everything. Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581662 Share on other sites More sharing options...
ajoo Posted October 1, 2020 Share Posted October 1, 2020 (edited) if ($success) { $_SESSION["userLoggedIn"] = $username; header("Location:index.php"); }else{ $error = $account->getError(Constants::$registerFailed); } It's a good practice to use an exit after the header ("Location ... Edited October 1, 2020 by ajoo 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/#findComment-1581664 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.