Jump to content

many variable, many if statement scenario, another way?


johnseito

Recommended Posts

Ok, after debugging my code, here is a full working example:

<?php
$default_vals = array('firstname' => '- Enter First Name -', 'lastname' => '- Enter Last Name -','state' => ' - Enter State -');
$err_fields = array();
foreach ($_POST as $fld => $val) {
    switch ($fld) {
        case 'firstname':   // these are al the fields that must be filled in
        case 'lastname':
        case 'street':
        case 'city':
        case 'state':
        case 'zip':
              if (array_key_exists($fld,$default_vals))
                      $val = str_replace($default_vals[$fld],'',$val);   // remove the default value from the entered value
              if (strlen(trim(stripslashes($val))) == 0) { //field is blank
                  $err_fields[] = $fld;
              }
              break;
        case 'something':
        case 'somethingelse':
//
//            do another validation on different content
//
              break;
     }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title></title>
</head>

<body>
<?php
$flds = array('firstname','lastname','street','city','state','zip');
$tmp = array();
$tmp[] = '<form method="post" action="">';
foreach ($flds as $fld) {
    $red = (in_array($fld,$err_fields))?' style="color:red"':'';
    $val = (isset($_POST[$fld]) && $_POST[$fld] != $default_vals[$fld])?htmlentities(trim(stripslashes($_POST[$fld])),ENT_QUOTES):$default_vals[$fld];
    $tmp[] = '<label for="' . $fld . '" ' . $red . '>' . ucwords($fld) . '</label>';
    $tmp[] = '<input type="text" name="' . $fld . '" id="' . $fld . '" value="' . $val . '"><br>';
}
$tmp[] = '<input name="submit" type="submit" value="Enter the information">';
$tmp[] = '</form>';
echo implode("\n",$tmp) . "\n";
?>


</body>
</html>

 

Ken

Link to comment
Share on other sites

  • 2 weeks later...

kenrbnsn -- 

 

I have looked at your code, it looks good, but here is the problem I am facing with your code.  For example

I wanted a form like

1. About yourself

          My name  _________    __________            (first and last name)

          Birthday  _________    ______  _________  (month, day and year)

          Address  _________  _____________          (Address, state)

 

2. Select an ID and password

        Email        _______

        Username  _______

        Password  ________

 

your code only give one field per line.  I did the two field or three field per line but what I am having trouble are with adding 2. Select an ID and password this to the form and adding style and width and height of the form and fields, etc. 

 

Do you know how I can do this?  examples would be great. 

 

thanks,

 

Link to comment
Share on other sites

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.