Conditional submit ...

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
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

Conditional submit ...

Post by pepe_lepew1962 »

Hello:

I am trying to change the action portion of my form based on criteria, in this case if the counter is equal to 14 then load thankyou.php, else continue on the PHP Self. What this is is a application form that tests whether all the data is okay. I will work on the santizing later but wanted to get this done as it seems to be the hardest, at least for me.


<?php
$frmSubmit = "";
$errFName = "";

if($_POST["ac"]=="login")
{
// If First Name is NOT letters, dash or spaces then display error message.
if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["fname"]) === 0)
$errFName = '<class="errText">First name must be from letters, dashes, spaces and must not start with dash';
else
$frmCounter = $frmCounter + 1;
// Add validation & Santization here
// more of the above ...
// more of the above ...
// more of the above ...
// more of the above ...

if $frmCounter = 14
$frmSubmit = ThankYou.php;
else
$frmSubmit = $PHP_SELF;

}

?>

<body>

<form name="main" action="<?php $frmSubmit ?>" method="POST"><input type="hidden" name="ac" value="login">
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Conditional submit ...

Post by sergio-pro »

Hi,

Looks like you misunderstand how forms work.
The form action should always be $PHP_SELF, so that the form data comes to your server script first, where it is validated.
Then if form data has error - the form should be displayed again,
if no error - just redirect to Thank You page.

The better code is:

Code: Select all

 
 
<?php
$frmSubmit = "";
$errFName = "";
$hasError = false;
 
if($_POST["ac"]=="login")
{
// If First Name is NOT letters, dash or spaces then display error message.
if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["fname"]) === 0) {
$errFName = '<class="errText">First name must be from letters, dashes, spaces and must not start with dash';
$hasError = true;
} else {
// Add validation & Santization here
// more of the above ...
// more of the above ...
// more of the above ...
// more of the above ...
 
if (!$hasError)
    Redirect to ThankYou.php;
else
    Show $PHP_SELF page;
 
}
 
?>
 
 
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Conditional submit ...

Post by watson516 »

pepe_lepew1962 wrote:Hello:

I am trying to change the action portion of my form based on criteria, in this case if the counter is equal to 14 then load thankyou.php, else continue on the PHP Self. What this is is a application form that tests whether all the data is okay. I will work on the santizing later but wanted to get this done as it seems to be the hardest, at least for me.


<?php
$frmSubmit = "";
$errFName = "";

if($_POST["ac"]=="login")
{
// If First Name is NOT letters, dash or spaces then display error message.
if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["fname"]) === 0)
$errFName = '<class="errText">First name must be from letters, dashes, spaces and must not start with dash';
else
$frmCounter = $frmCounter + 1;
// Add validation & Santization here
// more of the above ...
// more of the above ...
// more of the above ...
// more of the above ...

if $frmCounter = 14
$frmSubmit = ThankYou.php;
else
$frmSubmit = $PHP_SELF;

}

?>

<body>

<form name="main" action="<?php $frmSubmit ?>" method="POST"><input type="hidden" name="ac" value="login">
First off, put your code into [ code][ /code] or [ code=php][ /code] tags for easier reading.

Code: Select all

<?php
...
if($frmCounter==14) $frmSubmit = "ThankYou.php"; else $frmSubmit = $_SERVER['PHP_SELF'];
...
?>
 
<form name="main" action="<?php echo $frmSubmit; ?>" method="POST">...</form>
Your original IF statement needed some reworking and when you want something to show up in your html via php such as your form there, you need to echo it. $_SERVER['PHP_SELF'] is the proper '$PHP_SELF'
Post Reply