Jump to content

Recommended Posts

is this possible ???

 

i have something like:

 

<?php
if($_POST['variable']==""){ $error['variable']="this variable is empty"} else{ unset($error['variable']);}?>

 

i have made a switch statement that does the same thing pretty much but im unsure how to unset the errors

$error = array(); // start with a clean bill of health

if (empty($_POST['variable'])) {
    $error['variable'] = "This variable was empty.";

if (count($error) > 1) {
    echo "You had errors which were: <br />";
    echo implode("<br />", $error);
}else {
    echo "No errors, woohoo!";
}

 

No need to unset it, if you do not set it in the first place.

its not working right. it doesnt display the form just says my outpt is zero and that i am healthy.

 

<?php
function is_int_val($data) {
if (is_int($data) === true) return true;
elseif (is_string($data) === true && is_numeric($data) === true) {
   return (strpos($data, '.') === false);
}
   return false;
}

$height= trim($_POST['height']);
$weight=trim($_POST['weight']);
$heighttwo= $height*$height;
@$total=$weight/$heighttwo;
$newtotal= $total*703;
$output = sprintf('%01.2f',$newtotal);
switch($_POST['height']){

case($_POST['height']==""||empty($_POST['height'])):
$error['height']="<font color=red>Please enter a height!</font>";
break;

case(!is_numeric($_POST['height'])):
$error['height']="<font color=red>Only Numbers Allowed!</font>";

case(!is_int_val($_POST['height'])):
$error['height']="<font color=red>Only Numbers Allowed!</font>";
}

switch($_POST['weight']){

case($_POST['weight']==""||empty($_POST['weight'])):
$error['weight']="<font color=red>Please enter a height!</font>";
break;

case(!is_numeric($_POST['weight'])):
$error['weight']="<font color=red>Only Numbers Allowed!</font>";

case(!is_int_val($_POST['weight'])):
$error['weight']="<font color=red>Only Numbers Allowed!</font>";
}
if($error){?>

<form method="POST">

<b>Height(inches):</b><input name="height" type="text" size="3"><?php if ($_POST) { echo $error['height'];} ?><b>Weight(lbs.):</b><input name="weight" type="text" size="3"><?php if ($_POST) { echo $error['weight'];} ?>
<input type="submit" name="Calculate!" value="Calculate!">

</form>
<?php
}else{
switch($newtotal)
{
    case ($newtotal < 18):
    $msg = "<font color=red>You're Underweight! Youre BMI is:$output%</font>";
    break;

    case ($newtotal >= 18 && $newtotal < 24.9):
    $msg = "<font color=green>You're Healthy! Youre BMI is:$output%</font>";
    break;

    case ($newtotal >= 24.9 && $newtotal < 29.9):
    $msg = "<font color=orange>You're Overweight! Youre BMI is:$output%</font>";
    break;

    default:
    $msg = "<font color=red>You Are Obese Please seek Immediate Medical Help!</font>";
    break;
}
echo $msg;
}
?>

switch. I do not believe you can use conditional cases like you are. Use IF/Else If/Else instead.

 

If you can do that, then yea. Why not just use an if/else if/else statement. It would be much less code and easier to read, in my opinion.

you can too use conditional statements with cases  :P

 

But why. It makes more code for you to write. I do not see the purpose of using switch case in this scenario. It just does not make sense to me, as it is much more code than if/else if/else would take.

 

EDIT:

As to your problem:

<?php if ($_POST) { echo $error['height'];} ?><b>Weight(lbs.):</b><input name="weight" type="text" size="3"><?php if ($_POST) { echo $error['weight'];} ?>

 

You are checking if $_POST and echoing an error? Why ??? how does $_POST = an error? This is probably what you were intending to do:

<?php echo isset($error['height'])?$error['height']:''; ?>

 

You should be checking if the error index has been set, not if $_POST as that will execute no matter what if the form has been submitted.

 

EDIT: EDIT:

Also:

if($error){?>

 

Why do you not use proper checking conditions in the if statement?

if (count($error) > 1 || !isset($_POST['Calculate!'])){?>

 

That way, only display the form if there have been errors recorded to the array. And you will know that the condition will not fluke out just because $error has been set.

 

EDIT: Added the !isset to the calculate. I would suggest not using non-alpha numeric characters in your form item names. This way, if there were no errors or if the form has not been submitted then show the form, else show the msg.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.