NameSilo

PHP Question

Spaceship Spaceship
Watch

spacetrain

Established Member
Impact
8
Hey,

I have the following code in a script:


PHP:
<?php
if ( empty($tireqty) == 1 ) {
echo '<font color=#ff0000>';
echo 'You did not enter an amount for tires <br /><br />';
echo '</font>';

if ( empty($oilqty) == 1 ){
echo '<font color=#ff0000>';
echo 'You did not enter an amount for oil <br /><br />';
echo '</font>';


if ( empty($sparkqty) == 1 ){
echo '<font color=#ff0000>';
echo 'You did not enter an amount for spark plugs <br /><br />';
echo '</font>';
include 'orderform.html';
exit;
}
include 'orderform.html';
exit;
}
include 'orderform.html';
exit;
}
?>

What i want it to do is display a message when a value isnt entered into a field but i want to display an error for each missed field at once and if they arent there show the order form again instead of running the rest of the code. However with the code above if the first one exists it doesnt check for the rest of the statements and there must be an easier way than copying the next two into the else section?

Im learning php so i just want to know as i will probably use this in a future script.

Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Something like this?
PHP:
<?php

if (empty($tireqty))
{
    echo '<font color=#ff0000>You did not enter an amount for tires <br /><br /></font>';
    include 'orderform.html';
}
else if (empty($oilqty))
{
    echo '<font color=#ff0000>You did not enter an amount for oil <br /><br /></font>';
    include 'orderform.html';
}
else if (empty($sparkqty))
{
    echo '<font color=#ff0000>You did not enter an amount for spark plugs <br /><br /></font>';
    include 'orderform.html';
}
else
{
    // They are not empty
}

?>
 
0
•••
That would only display one error at a time, but thanks.

I managed to find a solution and edit it a bit, the code i used was:

PHP:
if ( empty($tireqty) == 1 || empty($oilqty) == 1 ||  empty($sparkqty) == 1 )
{
echo '<font color=#ff0000>';
echo 'You didnt enter the following fields: <br />';
if(empty($tireqty) == 1 )
   { echo 'tires<br />'; }
if(empty($oilqty) == 1 ){echo 'oil<br />'; }
if(empty($sparkqty) == 1 ){echo 'spark plugs<br />'; }
echo '<br /><br />';
include 'orderform.html';
exit;
}

Means it shows as:

You didnt enter the following fields
{lists every field not entered}
 
0
•••
Oh, yes, sorry :) Glad ya figured it out.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back