Jump to content

[SOLVED] Loop Nestled in Html = Fail *Should Be simple


Grodo

Recommended Posts

Hello, Thank you for your time.

 

In the code attached below it does not seem like the loop is being activated am I doing something wrong?  $message is array and is marked as a global and contains up to a total of 6 values.  I am really stumped on this one PLEASE HELP :)

Thanks

Grodo

 

<div style="width: 518px; height: 287px;visibility:hidden" id="windowcontent">
            <table >
            <tr>
            <td>Your Form Has The Following Errors:</td>
            </tr>
		<tr>
		<td><?php for ($xi=1;$xi<=6;$xi++) { echo "$message[$xi]"; } ?><td>
		</tr>
            <tr><td colspan="2"><br />Click the X  to close</td></tr>
            </table>
</div>

It doesn't like the X

 

<?php 
$message = array("one","two","three","four","five","six","seven");

for ($i=1;$i<=6;$i++) { 
echo "$message[$i]"; 
} 
?>

 

if you don't want to use $i, then use $x or something.

 

<?php 
$message = array("one","two","three","four","five","six","seven");

for ($x=1;$x<=6;$x++) { 
echo "$message[$x]"; 
} 
?>

Well that expains it came back as NULL.  Seems to me like the function is not setting $message which lists all the errors.  Here is code for the function that should be set $message to the error.

 

function check_form() 
{ 
    global $HTTP_POST_VARS, $error, $print_again, $message; 
    // Validate first name
    $error['fname'] = false; 
    if($_POST["fname"]=="") { 
        $error['fname'] = true; 
        $print_again = true; 
        $message[0]="Please insert you first name<br>"; 
    } 
// Validate last name
$error['lname'] = false; 
    if($_POST["lname"]=="") { 
        $error['lname'] = true; 
         $print_again = true; 
        $message[1]="Please insert you last name<br>"; 
    } 
//  Validate Address
$error['address1'] = false; 
    if($_POST["address1"]=="") { 
        $error['address1'] = true; 
        $print_again = true; 
        $message[2]="Please enter a valid address<br>"; 
    } 
//  Validate City
$error['city'] = false; 
    if($_POST["city"]=="") { 
        $error['city'] = true; 
        $print_again = true; 
        $message[3]="Please enter a valid city<br>"; 
    }
//  Validate State
$error['state'] = false; 
    if($_POST["state"]=="") { 
        $error['state'] = true; 
        $print_again = true; 
        $message[4]="Please enter a valid State or Territory<br>"; 
    }
// Validate Country
$error['country'] = false; 
    if($_POST["country"]=="") { 
        $error['country'] = true; 
        $print_again = true; 
        $message[5]="Please enter a valid country<br>"; 
    }
// Validate Postal code
$error['zip'] = false; 
    if($_POST["zip"]=="") { 
        $error['zip'] = true; 
        $print_again = true; 
        $message[6]="Please enter a valid zip<br>"; 
    }

    if($print_again) { 
        show_form();
	echo "<SCRIPT LANGUAGE='javascript'>displayWindow();</SCRIPT>";
       } else { 
		show_form(); 
		$message="All Fields are valid <br> 
        Now, In this way you can validate the other textfield as well<br> 
        You can insert data into table"; 
       } 

}		

That doesnt seem like that the answer... But thanks!

 

Now do I put <td> <?php foreach ($message as $v) { echo "-$v"; } ?> </td> ?

 

Seems that $message is NULL but when i set a for loop in the function it displays the messages

 

Could it be because the function is not returning anything?

Ok i tried to move globals outside of the function and then my error highlighting stopped working.  I put the globals back and boom error highlighting works.  Seems like message is still coming null should I maybe make a $string = checkform().  Or is there something wrong with there globals

 

global $HTTP_POST_VARS, $error, $print_again, $message;

I think I know what might be wrong...

The div is at the top so when the browser reads it it sees $message and sets it to null so what i need to echo the div out escape quotes and then combine the rest this is my whole code I am still trying to figure out y its giving me an unexpected . error.  (Isnt the . operator in php the same as + in js?)

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head>
<LINK REL=StyleSheet HREF="dimming.css" TYPE="text/css">
	<SCRIPT LANGUAGE="JavaScript" SRC="dimmingdiv.js"> </script>
	<script language="javascript">
	    function displayWindow()
	    {
	        var w, h, l, t;
	        w = 400;
	        h = 200;
	        l = screen.width/4;
	        t = screen.height/4;
                
                // no title		        
	        // displayFloatingDiv('windowcontent', '', w, h, l, t);

                // with title		        
	        displayFloatingDiv('windowcontent', 'Floating and Dimming Div', w, h, l, t);
	    }
	</script>
<style type="text/css">
body, table
{
font-size:13px;
font-family:verdana,arial,helvetica,sans-serif;
color:#000;
background-color:#FFF;	
}
body
{
margin:50px;	
}
input, select, textarea
{
font-size:13px;
font-family:verdana,arial,helvetica,sans-serif;
}
h1
{
font-family:Arial, Verdana, Helvetica, Lucida, sans-serif;
font-size:22px;
color:maroon;
margin:0px 0px 30px 0px;
}
.formErrors
{
color:maroon;
}
</style> 
<title>Form Validation</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 
<?php

$HTTP_POST_VARS = NULL;
$print_again = NULL;
$error = NULL; 
$message = NULL;

function error_bool($error, $field) { 
         if($error[$field]) { 
             print("<td style=color:red>"); 
         } 
        else { 
            print("<td>"); 
        } 
    } 

function show_form() { 
global $HTTP_POST_VARS, $print_again, $error; 
?> 
<form method="post" action="">
<table bord"0" width="100%" cellpadding="5" cellspacing="0">
<tr>
<?php error_bool($error, "fname"); ?><b>First Name *</b></td>
<td><input type="text" name="fname" value="<?php $_REQUEST['fname'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "lname"); ?><b>Last Name *</b></td>
<td><input type="text" name="lname" value="<?php $_REQUEST['lname'] ?>" size="25"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php $_REQUEST['email'] ?>" size="25"></td>
</tr>
<tr>
<td>Company</td>
<td><input type="text" name="company" value="<?php $_POST['company'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "address1"); ?><b>Address 1*</b></td>
<td><input type="text" name="address1" value="<?php $_REQUEST['address1'] ?>" size="25"></td>
</tr>
<tr>
<td>Address 2</td>
<td><input type="text" name="address2" value="<?php $_REQUEST['address2'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "city"); ?><b>City*</b></td>
<td><input type="text" name="city" value="<?php $_REQUEST['city'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "state"); ?><b>State / Territory*</b></td>
<td><input type="text" name="state" value="<?php $_REQUEST['state'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "country"); ?><b>Country*</b></td>
<td><input type="text" name="country" value="<?php $_REQUEST['country'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "zip"); ?><b>Postal Code*</b></td>
<td><input type="text" name="zip" value="<?php $_REQUEST['zip'] ?>" size="25"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" value="<?php $_REQUEST['phone'] ?>" size="25"></td>
</tr>
<tr>
<td><b>Catalog</b></td>
<td>
   <input type="checkbox" name="toolmag">Tool Catalog<BR>

   <input type="checkbox" name="sinkmag">Vanity / Stainless Steel Sink Catalog<BR>

   <input type="checkbox" name="faucetmag">Faucet Catalog<BR>
</td>
</tr>
<tr>
<td>Special Instructions</td>
<td>
<textarea name="message" cols="50" rows="8"><?php $_REQUEST['message'] ?></textarea>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="submit" value="Send"> <input type="reset" value="Reset">
</td>
</tr>
</table>
</form>

<a href="javascript:displayWindow();">Here</a>


<? 
} 

if(isset($_POST["submit"])) { 
    check_form(); 
} else { 
    show_form(); 
} 

// Function to check the form
function check_form() 
{ 
global $HTTP_POST_VARS, $error, $print_again, $message;
// Validate first name
$error['fname'] = false; 
    if($_POST["fname"]=="") { 
        $error['fname'] = true; 
        $print_again = true; 
        $message[]="Please insert you first name<br>"; 
    } 
// Validate last name
$error['lname'] = false; 
    if($_POST["lname"]=="") { 
        $error['lname'] = true; 
         $print_again = true; 
        $message[]="Please insert you last name<br>"; 
    } 
//  Validate Address
$error['address1'] = false; 
    if($_POST["address1"]=="") { 
        $error['address1'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid address<br>"; 
    } 
//  Validate City
$error['city'] = false; 
    if($_POST["city"]=="") { 
        $error['city'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid city<br>"; 
    }
//  Validate State
$error['state'] = false; 
    if($_POST["state"]=="") { 
        $error['state'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid State or Territory<br>"; 
    }
// Validate Country
$error['country'] = false; 
    if($_POST["country"]=="") { 
        $error['country'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid country<br>"; 
    }
// Validate Postal code
$error['zip'] = false; 
    if($_POST["zip"]=="") { 
        $error['zip'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid zip<br>"; 
    }

    if($print_again) { 
        show_form();

	echo '<div style="width: 518px; height: 287px;visibility:hidden" id="windowcontent">
            <table >
            <tr>
            <td>Your Form Has The Following Errors:</td>
            </tr>
		<tr>
		<td>$message</td>
		</tr>
            <tr><td colspan="2"><br />Click the X  to close</td></tr>
            </table>
		</div>';

	echo "<SCRIPT LANGUAGE='javascript'>displayWindow();</SCRIPT>";
       } else { 
		show_form(); 
		$message[]="All Fields are valid <br> 
        Now, In this way you can validate the other textfield as well<br> 
        You can insert data into table"; 
       } 

}		
// for ($x=1;$x<=6;$x++) { echo "$message[$x]"; }
?> 
<!--  the following hidden div will be used by the script -->


</body> 
</html>

Alright! I got one of the messages to appear! However now the trick is to cycle through the array and display all errors.

 

The problem was I was echoing and had a var with a semicolon ending the line

 

echo ' Echo some stuff' .$message;. 'Some more stuff;

So that error has been fixed :)

Ok thanks to every one for the help i received the problem was a simple logic error....

 

The hidden div was on top when the browser read it it saw that it was calling $message at the time $message was = NULL; and unfortunally the variables in php dont update after they have been read.  For those following this thread here is the now working code.

 

Always Thanking,

GRODO

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head>
<LINK REL=StyleSheet HREF="dimming.css" TYPE="text/css">
	<SCRIPT LANGUAGE="JavaScript" SRC="dimmingdiv.js"> </script>
	<script language="javascript">
	    function displayWindow()
	    {
	        var w, h, l, t;
	        w = 400;
	        h = 200;
	        l = screen.width/4;
	        t = screen.height/4;
                
                // no title		        
	        // displayFloatingDiv('windowcontent', '', w, h, l, t);

                // with title		        
	        displayFloatingDiv('windowcontent', 'Floating and Dimming Div', w, h, l, t);
	    }
	</script>
<style type="text/css">
body, table
{
font-size:13px;
font-family:verdana,arial,helvetica,sans-serif;
color:#000;
background-color:#FFF;	
}
body
{
margin:50px;	
}
input, select, textarea
{
font-size:13px;
font-family:verdana,arial,helvetica,sans-serif;
}
h1
{
font-family:Arial, Verdana, Helvetica, Lucida, sans-serif;
font-size:22px;
color:maroon;
margin:0px 0px 30px 0px;
}
.formErrors
{
color:maroon;
}
</style> 
<title>Form Validation</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 
<?php

$HTTP_POST_VARS = NULL;
$print_again = NULL;
$error = NULL; 
$message = NULL;

function error_bool($error, $field) { 
         if($error[$field]) { 
             print("<td style=color:red>"); 
         } 
        else { 
            print("<td>"); 
        } 
    } 

function show_form() { 
global $HTTP_POST_VARS, $print_again, $error; 
?> 
<form method="post" action="">
<table bord"0" width="100%" cellpadding="5" cellspacing="0">
<tr>
<?php error_bool($error, "fname"); ?><b>First Name *</b></td>
<td><input type="text" name="fname" value="<?php $_REQUEST['fname'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "lname"); ?><b>Last Name *</b></td>
<td><input type="text" name="lname" value="<?php $_REQUEST['lname'] ?>" size="25"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php $_REQUEST['email'] ?>" size="25"></td>
</tr>
<tr>
<td>Company</td>
<td><input type="text" name="company" value="<?php $_POST['company'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "address1"); ?><b>Address 1*</b></td>
<td><input type="text" name="address1" value="<?php $_REQUEST['address1'] ?>" size="25"></td>
</tr>
<tr>
<td>Address 2</td>
<td><input type="text" name="address2" value="<?php $_REQUEST['address2'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "city"); ?><b>City*</b></td>
<td><input type="text" name="city" value="<?php $_REQUEST['city'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "state"); ?><b>State / Territory*</b></td>
<td><input type="text" name="state" value="<?php $_REQUEST['state'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "country"); ?><b>Country*</b></td>
<td><input type="text" name="country" value="<?php $_REQUEST['country'] ?>" size="25"></td>
</tr>
<tr>
<?php error_bool($error, "zip"); ?><b>Postal Code*</b></td>
<td><input type="text" name="zip" value="<?php $_REQUEST['zip'] ?>" size="25"></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" value="<?php $_REQUEST['phone'] ?>" size="25"></td>
</tr>
<tr>
<td><b>Catalog</b></td>
<td>
   <input type="checkbox" name="toolmag">Tool Catalog<BR>

   <input type="checkbox" name="sinkmag">Vanity / Stainless Steel Sink Catalog<BR>

   <input type="checkbox" name="faucetmag">Faucet Catalog<BR>
</td>
</tr>
<tr>
<td>Special Instructions</td>
<td>
<textarea name="message" cols="50" rows="8"><?php $_REQUEST['message'] ?></textarea>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="submit" value="Send"> <input type="reset" value="Reset">
</td>
</tr>
</table>
</form>

<a href="javascript:displayWindow();">Here</a>


<? 
} 

if(isset($_POST["submit"])) { 
    check_form(); 
} else { 
    show_form(); 
} 

// Function to check the form
function check_form() 
{ 
global $HTTP_POST_VARS, $error, $print_again, $message;
// Validate first name
$error['fname'] = false; 
    if($_POST["fname"]=="") { 
        $error['fname'] = true; 
        $print_again = true; 
        $message[]="Please insert you first name<br>"; 
    } 
// Validate last name
$error['lname'] = false; 
    if($_POST["lname"]=="") { 
        $error['lname'] = true; 
         $print_again = true; 
        $message[]="Please insert you last name<br>"; 
    } 
//  Validate Address
$error['address1'] = false; 
    if($_POST["address1"]=="") { 
        $error['address1'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid address<br>"; 
    } 
//  Validate City
$error['city'] = false; 
    if($_POST["city"]=="") { 
        $error['city'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid city<br>"; 
    }
//  Validate State
$error['state'] = false; 
    if($_POST["state"]=="") { 
        $error['state'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid State or Territory<br>"; 
    }
// Validate Country
$error['country'] = false; 
    if($_POST["country"]=="") { 
        $error['country'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid country<br>"; 
    }
// Validate Postal code
$error['zip'] = false; 
    if($_POST["zip"]=="") { 
        $error['zip'] = true; 
        $print_again = true; 
        $message[]="Please enter a valid zip<br>"; 
    }

    if($print_again) { 
        show_form();
	echo '<div style="width: 518px; height: 400px;visibility:hidden" id="windowcontent">
            <table >
            <tr>
            <td>Your Form Has The Following Errors:</td>
            </tr>
		<tr>
		<td>'.$message[0].'</td>
		</tr>
		<tr>
		<td>'.$message[1].'</td>
		</tr>
		<tr>
		<td>'.$message[2].'</td>
		</tr>
		<tr>
		<td>'.$message[3].'</td>
		</tr>
		<tr>
		<td>'.$message[4].'</td>
		</tr>
		<tr>
		<td>'.$message[5].'</td>
		</tr>
		<tr>
		<td>'.$message[6].'</td>
		</tr>
            </table>
		</div>';

	echo "<SCRIPT LANGUAGE='javascript'>displayWindow();</SCRIPT>";
       } else { 
		show_form(); 
		$message[]="All Fields are valid <br> 
        Now, In this way you can validate the other textfield as well<br> 
        You can insert data into table"; 
       } 

}		
// for ($x=1;$x<=6;$x++) { echo "$message[$x]"; }
?> 
<!--  the following hidden div will be used by the script -->


</body> 
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.