Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/311541-access-external-php-array-in-html-file/
Share on other sites

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

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.

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 by benanamen
 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 by ajoo
  • Like 1
  • Thanks 1
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.