Passing data between PHP states

So I am using Limonade PHP, which has a RESTful project that emulates PUT, POST, DELETE routes for create, update, delete.

I'm trying to work out some form validation that is going well. The main problem I am facing is how to return the filtered data (which did not pass validation) in order to re-fill the create or edit form.

How to do it? I am currently creating a page:

/admin/page/new -> GET function

/admin/page -> POST function
+ validate
    + pass, update db
    + fail, add errors to flash, redirect to /admin/page/mew

      

Everything crashes as I don't know how to fill / admin / page / new with invalid but filtered data.

+2


a source to share


3 answers


Are you using session to migrate data to / admin / page / new? http://www.php.net/manual/en/session.examples.basic.php



edit: I just found this article: http://www.recessframework.org/page/towards-restful-php-5-basic-tips which recommends using a cookie over $ _SESSION. He doesn't elaborate on why, but either one will achieve the desired result.

+1


a source


To get the data back to the redirected page you need to either use a session or garbage request with getting vars for each item (not nice to look at, not nice for bookmarks, not suggesting).

I would suggest that "add errors to flash" uses a var session (not familiar with Limonade).

Another alternative would be to submit the form using an AJAX call , then the form data would not even change on error.



Of course, you still need a non-AJAX method for backward compatibility.

Update: Lemonade source confirms flash()

uses $_SESSION

. So, you are already using session vars.

+1


a source


You can display the form without redirecting. Place the form in a script that contains no other HTML elements. Set the value to any input of the shape specified in $_POST

(after calling htmlspecialchars

with the appropriate quote type). Include the script form in other scripts where needed.

In your utility functions:

function passthruFormInput($name) {
    if (isset($_POST[$name])) {
      echo htmlspecialchars($_POST[$name], ENT_QUOTES); 
    }
}

      

newForm.php (or whatever you want to call it):

<form action="..." method="POST" onsubmit="...(client side validation function)...">
    ...
    <input name="foo" value="<?php passthruFormInput('foo'); ?>"/>
    ...
</form>

      

If you are creating the form dynamically, adjust the above value to fit. Several things are said about this, and I cannot say that it is not.

Leaving a script form publicly might not be a security issue, but it should either be outside the document root hierarchy or in a branch protected ORDER Allow,Deny

or mod_rewrite . It should probably be related to views.

0


a source







All Articles