Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP form validation on same page - is this logic correct?
    primarykey
    data
    text
    <p>I am trying to build a simple registration form that passes through some checks and then saves the data to the database. Initially, there was just the registration form and the processing form and the un-validated form data was getting saved in the data base. The trouble started when I tried to get some validation into the process. </p> <pre><code> &lt;Edit&gt; </code></pre> <p>There are only three goals here:</p> <ol> <li>To prevent logged in users from trying to register.</li> <li>As long as there is no active user account running in the browser, first ensure that there are no empty fields and</li> <li>Send the form data for processing.</li> </ol> <p>For #1, posting to php self seemed ideal as it avoided a lot of extra coding for me and has the advantage to the users of not having to type in all the stuff again.</p> <p>For #2, validation is limited to checking for empty fields right now and nothing else. This is a registration form and user data is being collected - no authentication of the user at all.</p> <p>For #3, I have devised a way of sending the data to another php and that is not an issue any more.</p> <p>The issue is the logic. Why are the parts that are working separately failing when put together? Is there some fault in the logic I am applying to the process? </p> <p>One of the comments said something about elseif and I have tried that also. I am not getting any error in the code - no parse errors, no syntax errors, no fatal errors - but nothing is happening either - just the register form getting refreshed. The individual parts of the system work separately on test pages but when put together with the full form, it doesn't work.</p> <pre><code> &lt;/Edit&gt; </code></pre> <p>Here on SE I found the way to post the form to php self and tried it out with the sample code in the post. It worked as expected, all seemed to be fine and so I added it to my form page. But it is doing nothing when on the actual form page except reloading it. </p> <p>Again, here on SE I found a post that showed how all the errors could be caught in an array and displayed. It seemed to work on a sample file and I added the code with the appropriate changes of variable names to my registration page. If there is no user logged in and the user clicks on submit, the empty field error that is caught should be displayed. It didn't. display any errors even when all the fields were left blank. Everything collapsed after that. </p> <p>All that is happening now is that the registration form reloads - error or no error!</p> <p>I have tried so many things that I am no longer sure whether what I was trying to do since last evening is what the code is doing now. So I am starting over with just the logic and the related questions. </p> <p>Here is the logic and the questions for each stage....</p> <pre><code> &lt;php session_start() ;?&gt; &lt;?php //check if the user is logged in if (isset($_SESSION['validuser'])) { //catch this first - before user spends time filling out the 12 fields! //send to message page saying "you cannot register - you are already a member and logged in" //WORKING PERFECTLY - CONFIRMS FORM POSTING TO SAME PAGE CORRECTLY! //time for other checks.... } else (!isset($_SESSION['validuser']) &amp;&amp; isset($_POST['submit'])) { //if there is no logged in user and form has been submitted, start checking the fields to see if any fields are empty //collect all errors in array and display? //direct back to the form with appropriate messages, form fields retained //exit here? or no? //focus now has to pass to the form again - need any code for this? or will it happen automatically? } if isset($_POST['submit'])) { //Should this part be within the else block? //If it is out side, will it get rendered on load? //if there are no errors proceed to process the form //there are further checks that need talking to the database //once those checks and approvals are over the data gets saved to the database } ?&gt; &lt;html&gt; &lt;body&gt; &lt;form action="&lt;?=$_SERVER['PHP_SELF']?&gt;" method = "POST"&gt; &lt;!-- 12 fields to be filled out by user --&gt; &lt;input type = "submit" name = "submit" value = "submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>What am I missing? Please remember that this is a workflow I am trying to clean up. The original code is so souped up now that I don't want to expose it here - chances are that I will get a lot of advice about sanitation of input and escaping of output! I have deliberately left out those bits in this development environment where only I have access to the forms and database. Sanitizing and escaping only adds clutter at this point.</p> <p>That said, once I have the workflow right I will add those bits too :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload