Help with structure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhilAJ
Forum Newbie
Posts: 9
Joined: Fri May 04, 2012 1:47 am

Help with structure

Post by PhilAJ »

Hope someone can give me some advice - I think I'm almost there with PHP/HTML but just need confirmation of how to do a couple of things as I'm getting into a bit of a knot with combining the php and HTML and getting the Ifs right;

Basically, I am trying to do a site with a page that is first loaded with a form to be filled in; When submitted - validate the date in php and write it to a database, redisplay the form either with errors or with the same data in the form. If the form is called a second time - retrieve the data and display.

In simple terms;(and without the sql bits and pieces shown);


If a POST was done;

Validate the data
If Ok
Write to Database
Redisplay the page with the form
else
Display error message onto form
endif

else

Read data from Database
If nothing there
Display empty html Form ....
Display html submit button
else
Display form with retrieved data
display submit button
endif


From various tutorials I have come up with this, it does work to a point - but I’m not convinced it’s the easiest way of doing – as the form has lots of fields and this appears to duplicate a lot;

<?php if ($_POST): ?>

<!---Call from a POST - check fields and write them away....-->
<?php
If F1 invalid - msg = "Field 1 Error"
If NO Errors - sg = "All OK"
Put the msg into the Page ?? How do I do this
?>

<p> Some DIFFERENT TEXT </p>
<form action="thispage.php" method="post" name="page" id="page" >
<input name="Field1" type="text" id="F1" >
<input name="Field2" type="text" id="F2" >
<input name="postbbutton" type="submit" id="pb" value="pb">
</form>



<?php else: ?>
<!-- Must be in from a Non Post call -->
<?php
// Retrieve data from database if present……
// Place the data from dbase into the Form ?How do I do this ? <?

<p> Some TEXT </p>
<form action="thispage.php" method="post" name="page" id="page" >
<input name="Field1" type="text" id="F1" >
<input name="Field2" type="text" id="F2" >
<input name="postbbutton" type="submit" id="pb" value="pb">
</form>

<?php endif; ?>


My main issues are;

a) AM I using the PHP 'If then Else' correctly ?
b) Do I really need to duplicate the form in both sides of the if ?
c) How do I get the data from the php sctipt into the form for redisplay.

Probably some basic stuff here - but any help much appreciated.

Thanks

Phil
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Help with structure

Post by Robert07 »

Hi Phil,
Though I've seen something like the if else syntax you are using (it may be valid), I prefer to use the curly bracket notation myself:
if (condition) {
do something;
} else if (condition) {
do something else;
}

There are many ways to do anything you want to do. If I were you I would separate the form handling from the output. This means at the top of the page I would check for a posted parameter that would indicate the form was submitted, then update the database if needed and put the latest values of the parameters into local variables. Then down below I would print out the form and use something like <input name="field1" value="<? echo $field1; ?>"> to populate the form.
PhilAJ
Forum Newbie
Posts: 9
Joined: Fri May 04, 2012 1:47 am

Re: Help with structure

Post by PhilAJ »

Thanks Robert

If I understand you right, do you mean to have the form as a file that hust reads from the database and populates as you said; but the validation is in a spearate form. If so - how do I get any validation errors back onto the displayed form page - if they are set in a separate file?

Maybe I'm missing some basic understanding

Regards

Phil
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Help with structure

Post by Robert07 »

I usually have a variable called $msg or something that I populate with any server side validation errors, then I echo that variable at the top of the form if it is not empty (instead of submitting the data). That gives the user some direction so they can fix the error and resubmit the form.
throwcode
Forum Newbie
Posts: 2
Joined: Sat May 05, 2012 10:23 pm

Re: Help with structure

Post by throwcode »

Hi PhilAJ,

There are a lot of different approaches out there - my favorite uses a centralized controller. I use this controller template from don howard's blog, http://sitepop.wordpress.com/2012/01/03 ... -template/.

He also has an article on handling errors that you might be interested in - http://sitepop.wordpress.com/2012/04/12 ... ut-errors/.

If your using Netbeans for your IDE checkout his article on structure. It saved me a lot of time and headaches.

TC
PhilAJ
Forum Newbie
Posts: 9
Joined: Fri May 04, 2012 1:47 am

Re: Help with structure

Post by PhilAJ »

Thanks for the help guys.

Phil
Post Reply