PHP form validation on same page - is this logic correct? -
i trying build simple registration form passes through checks , saves data database. initially, there registration form , processing form , un-validated form data getting saved in data base. trouble started when tried validation process.
<edit> there 3 goals here:
- to prevent logged in users trying register.
- as long there no active user account running in browser, first ensure there no empty fields and
- send form data processing.
for #1, posting php self seemed ideal avoided lot of coding me , has advantage users of not having type in stuff again.
for #2, validation limited checking empty fields right , nothing else. registration form , user data being collected - no authentication of user @ all.
for #3, have devised way of sending data php , not issue more.
the issue logic. why parts working separately failing when put together? there fault in logic applying process?
one of comments said elseif , have tried also. not getting error in code - no parse errors, no syntax errors, no fatal errors - nothing happening either - register form getting refreshed. individual parts of system work separately on test pages when put full form, doesn't work.
</edit> here on se found way post form php self , tried out sample code in post. worked expected, seemed fine , added form page. doing nothing when on actual form page except reloading it.
again, here on se found post showed how errors caught in array , displayed. seemed work on sample file , added code appropriate changes of variable names registration page. if there no user logged in , user clicks on submit, empty field error caught should displayed. didn't. display errors when fields left blank. collapsed after that.
all happening registration form reloads - error or no error!
i have tried many things no longer sure whether trying since last evening code doing now. starting on logic , related questions.
here logic , questions each stage....
<php session_start() ;?> <?php //check if user logged in if (isset($_session['validuser'])) { //catch first - before user spends time filling out 12 fields! //send message page saying "you cannot register - member , logged in" //working - confirms form posting same page correctly! //time other checks.... } else (!isset($_session['validuser']) && isset($_post['submit'])) { //if there no logged in user , form has been submitted, start checking fields see if fields empty //collect errors in array , display? //direct form appropriate messages, form fields retained //exit here? or no? //focus has pass form again - need code this? or happen automatically? } if isset($_post['submit'])) { //should part within else block? //if out side, rendered on load? //if there no errors proceed process form //there further checks need talking database //once checks , approvals on data gets saved database } ?> <html> <body> <form action="<?=$_server['php_self']?>" method = "post"> <!-- 12 fields filled out user --> <input type = "submit" name = "submit" value = "submit" /> </form> </body> </html> what missing? please remember workflow trying clean up. original code souped don't want expose here - chances lot of advice sanitation of input , escaping of output! have deliberately left out bits in development environment have access forms , database. sanitizing , escaping adds clutter @ point.
that said, once have workflow right add bits :-)
i'd restructure follows:
if (isset($_session['validuser'])) { //user authenticated } elseif (!isset($_session['validuser']) && isset($_post['submit'])) { //user not authenticated, proceed if (isset($_post['submit'])) //checking if form submitted { //process 12 input fields } else { //form wasn't submitted, display error } }
Comments
Post a Comment