Page 1 of 1

using foreach to validate form info, then send to URI

Posted: Sat Sep 23, 2006 1:10 am
by akimm
I hope that title is descriptive enough, otherwise I did honestly try to abide by the forum rules. However, this code is as the title suggests foreach loops to validate input, if its validated (this is where my problem is, well one of them), I want to send the user to the next page. admin.php

Thanks for any help.

Code: Select all

<?php
$admin = "<a href=" . $_SERVER['PHP_SELF'] . "?admin.php";
$submit = 0;
$user = "sean";
$pass = "butchy";
$submit2 = 0;
foreach ($_POST as $user => $value)
{
    if (!$value)
    {
        echo '<h2>The form field ' . $user . ' did match please try again.  Thank you!</h2>';
        $submit++;
    }
}
foreach ($_POST as $pass => $value2)
{
if (!value2)
	{
if($submit > 0)
{
if($submit2 > 0)
{
	if ($_POST['submit']) {
		$admin;
		} else {
	  die();
  }
?>
<form method="POST" action="pass.php">
<p><b>Date</b><input type="text" value="<b><?php echo date('F j, Y, g:i a');?></b>" name="date" size="30">
<p><b>Your name</b>
<input type='text' value="<?php echo $value1;?>" name='name' size='30'></p>

<p><b>Your email</b>
<input type='text' value="<?php echo $value1;?>" name='email' size='30'></p>

<p><b>article title</b>
<input type='text' value="<?php echo $value1;?>" name='title' size='30'></p>

<p><b>article</b>
<TEXTAREA NAME='article' COLS='120' ROWS='20' WRAP='virtual'></TEXTAREA></P>
<p><b><input type="submit" value="submit your aritlce" name="submit">
</b></p>
<?php
	   }
     }
  }
}
 ?>

Posted: Sat Sep 23, 2006 4:38 am
by jamiel
Start again, you have got yourself in a muddle. What are you actually trying to do in the bigger picture on this page?

Posted: Sat Sep 23, 2006 4:51 am
by aaronhall
If you're only trying to validate the username and password, just use an "if" construct and compare the input to the $user and $pass variables. If they both match, include the admin.php file (make sure that file has some validation so people can't just access it directly).

ok

Posted: Sat Sep 23, 2006 10:48 am
by akimm
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


My purpose for this is as follow,

this form
[syntax="html"]
<form action="pass.php" method="POST">
<p><b>your name</b>
<input type="text" name="name" size="30"></p>
<p><b>your pass</b>
<input type="text" name="pass" size="30"></p>

<input type="submit" name="submit" value="submit for check" size="30"></p>
will process the form input, and send it to that php i showed you all[/syntax]

Code: Select all

<?php 
$admin = "<a href=" . $_SERVER['PHP_SELF'] . "?admin.php"; 
$submit = 0; 
$user = "sean"; 
$pass = "butchy"; 
$submit2 = 0; 
foreach ($_POST as $user => $value) 
{ 
    if (!$value) 
    { 
        echo '<h2>The form field ' . $user . ' did match please try again.  Thank you!</h2>'; 
        $submit++; 
    } 
} 
foreach ($_POST as $pass => $value2) 
{ 
if (!value2) 
        { 
if($submit > 0) 
{ 
if($submit2 > 0) 
{ 
        if ($_POST['submit']) { 
                $admin; 
                } else { 
          die(); 
  } 
?> 
<form method="POST" action="pass.php"> 
<p><b>Date</b><input type="text" value="<b><?php echo date('F j, Y, g:i a');?></b>" name="date" size="30"> 
<p><b>Your name</b> 
<input type='text' value="<?php echo $value1;?>" name='name' size='30'></p> 

<p><b>Your email</b> 
<input type='text' value="<?php echo $value1;?>" name='email' size='30'></p> 

<p><b>article title</b> 
<input type='text' value="<?php echo $value1;?>" name='title' size='30'></p> 

<p><b>article</b> 
<TEXTAREA NAME='article' COLS='120' ROWS='20' WRAP='virtual'></TEXTAREA></P> 
<p><b><input type="submit" value="submit your aritlce" name="submit"> 
</b></p> 
<?php 
           } 
     } 
  } 
} 
 ?>


then i want $admin to take me to admin.php which will be my next prob


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Sep 23, 2006 3:51 pm
by aaronhall
http://www.php.net/header

Though there are much better ways to approach a password-secured page

Posted: Sat Sep 23, 2006 4:49 pm
by akimm
Well what are the better ways, I ask these questions so I can become better versed, I'm not asking you to do the work for me, but rather guide me if you have an idea.

Posted: Sat Sep 23, 2006 5:00 pm
by aaronhall
aaronhall wrote:If you're only trying to validate the username and password, just use an "if" construct and compare the input to the $user and $pass variables. If they both match, include the admin.php file (make sure that file has some validation so people can't just access it directly).

Code: Select all

<?
if($_POST['user'] == $user && $_POST['pass'] == $pass) {
	require ('/some/path/behind/the/public/directory/admin.php');
} else {
	// echo form again
}
?>
Also check out http://www.php.net/features.http-auth

Posted: Sat Sep 23, 2006 6:03 pm
by akimm
Thanks, I do appreciate it.