TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Selfsubmit form problem (http://www.talkphp.com/absolute-beginners/3178-selfsubmit-form-problem.html)

Peuplarchie 07-26-2008 08:43 PM

Selfsubmit form problem
 
Good day to you all,
I'm working on a php text based login script.
I'm at building the script to add, delete or edit account.

Here my problem :
When I enter a name password, it add it twice to the txt file.
Also, it oly add the ame of the field and not it value.


Here's my code :

PHP Code:

<?php


$username 
$_POST['username'];
$password $_POST['password'];
$url $_POST['url'];

function 
add_user($user,$url,$pass)
{
    
$fopen fopen('info.text''a');
    
fwrite($fopen"\n,'".$user."' => '".$url."' => '".$pass."'");
    
fclose($fopen);
}

add_user('username','password','url');
?> 


<html>
   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
         <input type="text" name="username" />
         <input type="text" name="password" />
         <select name="url">
            <option value="Director/index.php">Director</option>
            <option value="Admin/index.php">Admin</option>
            <option value="User/index.php">User</option></select><br />

         <input type="submit" /><br />
         
         
      </form>
      
      
      Preview:<br />
      <?php if(isset($_POST['html'])) echo stripslashes($_POST['html']); ?>
      
      
      
      
   </body>
</html>


Thanks !
Take care !

Gareth 07-26-2008 11:38 PM

PHP Code:

<?php

# Function: Add User
    
function add_user($user$pass$url){
        
$fopen fopen('info.txt''a');
        
fwrite($fopen"\n".$user."' => '".$url."' => '".$pass."',");
        
fclose($fopen);
    }

# Check to see if the form is submitted
if ( isset ( $_POST['submit'] ) ){
    
    
# If yes: Grab submitted values !Should be sanitised!
    
$username $_POST['username'];
    
$password $_POST['password'];
    
$url $_POST['url'];

    
# Then use our function to add them to the txt
    
add_user($username$password$url );
    
} else {

# If the form hasnt been submitted, show the form!
?> 


<html>
   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
         <input type="text" name="username" value="" />
         <input type="text" name="password" value="" />
         <select name="url">
            <option value="Director/index.php">Director</option>
            <option value="Admin/index.php">Admin</option>
            <option value="User/index.php">User</option>
        </select><br />

        <!-- Like the inputs above, give the input a name so we can call it above -->
         <input type="submit" name="submit" /><br />         
      </form>
      
<?php

    
}
    
?>              
      
   </body>
</html>

I have added comments into the code. Ask if you need any help :)

EDIT:

I have added in some validation:

PHP Code:

<?php

# Function: Add User
    
function add_user($user$pass$url){
        
$fopen fopen('info.txt''a');
        
fwrite($fopen"\n'" $user."' => '".$url."' => '".$pass."',");
        
fclose($fopen);
    }

# Check to see if the form is submitted
if ( isset ( $_POST['submit'] ) ){
    
    
# If yes: Grab submitted values !Should be sanitised!
    
$username $_POST['username'];
    
$password $_POST['password'];
    
$url $_POST['url'];

        
# Check to make sure user has actually inputted something!
        # || means that either of the conditions needs to be true/false
        # Therefore this reads: if the username is  or AND the password is empty
        # show a message :(
        
if ( empty($username) || empty($password) ){
            
            
# If we need to show our user this; they have left an input empty..
            
echo ':(';
            
            
        } else {
            
            
# Then use our function to add them to the txt
            
add_user($username$password$url );
            echo 
':)';
        }

} else {

# If the form hasnt been submitted, show the form!
?> 


<html>
   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
         <input type="text" name="username" value="" />
         <input type="text" name="password" value="" />
         <select name="url">
            <option value="Director/index.php">Director</option>
            <option value="Admin/index.php">Admin</option>
            <option value="User/index.php">User</option>
        </select><br />

        <!-- Like the inputs above, give the input a name so we can call it above -->
         <input type="submit" name="submit" /><br />         
      </form>
      
<?php

    
}
    
?>              
      
   </body>
</html>


Peuplarchie 07-27-2008 07:01 AM

Thanks, it's now solved !


All times are GMT. The time now is 06:24 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0