Help with structure
Posted: Fri May 04, 2012 1:49 am
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
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