using foreach to validate form info, then send to URI

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

using foreach to validate form info, then send to URI

Post 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
	   }
     }
  }
}
 ?>
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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).
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post 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]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

http://www.php.net/header

Though there are much better ways to approach a password-secured page
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Thanks, I do appreciate it.
Post Reply