• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem inserting an array into another array

#1
[eluser]markanderson993[/eluser]
I am creating a registration script to work with Codeigniter, however I cannot seem to be able to insert an array into another array. How can I insert the $errors array into the $data array being passed along to the html page?

Code:
<?php // home.php (Class)

Class Register extends Controller {

    function Register()
    {
        parent::Controller();
        $this->load->helper('form');
    }
    
    function index()
    {    
        $this->template->load('register/register_view');
    }
    
    function handle()
    {
        
    
    
        if (empty($_POST['username']))
        {
            $errors[0] = 'Please enter your username';
        } else {
            $username = $_POST['username'];
            global $username;
        }
        
        if ( (empty($_POST['password'])) || (empty($_POST['conf_password'])) )
        {
            $errors = 'Please enter your password';
        } else {
            $password = $_POST['password'];
            global $password;
        }
        
         if ( (empty($_POST['email'])) || (empty($_POST['conf_email'])) )
        {
            $errors = 'Please enter your email';
        } else {
            $email = $_POST['email'];
            global $email;
        }
        
        if ($errors)
        {
            global $errors;
            
            $data['problems'] = array('One','Two','Three');
            $this->template->load('register/register_view',$data);
        } else {
            echo 'Huge Success!';
        }
        
    }

} // End Class Home

?>

** Note: the global command will be used in the next step of developing this application **

Code:
<?php
    if (isset($problems))
    {
        foreach($problems as $message) echo "$message<br />";
    }
?&gt;

<b>Registration Page</b>

<div id="registration"><table>
    &lt;?=form_open('register/handle')?&gt;
    <tr><td>Username:</td><td>&lt;input type="text" name="username" /&gt;&lt;/td></tr>
    <tr><td>Password:</td><td>&lt;input type="password" name="password" /&gt;&lt;/td></tr>
    <tr><td>Confirm Password:</td><td>&lt;input type="password" name="conf_password" /&gt;&lt;/td></tr>
    <tr><td>Email:</td><td>&lt;input type="text" name="email" /&gt;&lt;/td></tr>
    <tr><td>Confirm Email:</td><td>&lt;input type="text" name="conf_email" /&gt;&lt;/td></tr>
    <tr><td colspan="2">&lt;input type="submit" name="submit" value="Register" /&gt;&lt;/td></tr>
</table></div>&lt;!--Registration--&gt;

#2
[eluser]Aea[/eluser]
Code:
$data['errors'] = $errors

But do note that you aren't treating $errors like an array but rather as a string, your code should look like...

Code:
if ( (empty($_POST['email'])) || (empty($_POST['conf_email'])) )
        {
            ***$errors[] = 'Please enter your email';
        } else {
            $email = $_POST['email'];
            global $email;
        }

#3
[eluser]markanderson993[/eluser]
Wow! Aea i've been pulling my hair out for past 3 hours trying to make this work! Thank you so much Smile

#4
[eluser]markanderson993[/eluser]
Alright the script is nearing completion however, when the script is executed I get a headers already sent error Sad Here is my code: What am I doing wrong to deserve such a painfully annoying error??!

Also, there are various session classes that can make your redux auth even more secure. After reading the posts on http://ellislab.com/forums/viewthread/72...20/#408551 I tried to use the code provided under some of the links such as DB2. Where am I supposed to install these scripts and how do i configure them to work with codeigniter?

Code:
&lt;?php // home.php (Class)

Class Register extends Controller {

    function Register()
    {
        parent::Controller();
        $this->load->helper('form');
    }
    
    function index()
    {    
        $this->template->load('register/register_view');
    }
    
    function handle()
    {
    
        if (empty($_POST['username']))
        {
            $errors[] = 'Please enter your username';
        }
        
         if ( (empty($_POST['password'])) || (empty($_POST['conf_password'])) )
        {
            $errors[] = 'Please enter your password';
        } else {
        
            if ( ($_POST['password']) !== ($_POST['conf_password']) )
            {
                $errors[] = 'The passwords you entered did not match';
            }
            
        }
        
         if ( (empty($_POST['email'])) || (empty($_POST['conf_email'])) )
        {
            $errors[] = 'Please enter your email';
        } else {
        
            if ( ($_POST['email']) !== ($_POST['conf_email']) )
            {
                $errors[] = 'The email adresses you entered did not match';
            }
            
        }
        
        if (empty($_POST['question']))
        {
            $errors[] = 'Please enter your secret question';
        }
        
        if (empty($_POST['answer']))
        {
            $errors[] = 'Please enter your secret answer';
        }
        
        if (!empty($errors))
        {
            $data['errors'] = $errors;
            $this->template->load('register/register_view',$data);
        } else {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $question = $_POST['question'];
            $answer = $_POST['answer'];
            $this->redux_auth->register($username, $password, $email, $question, $answer);
            echo 'Congratulations you have been successfully registered!';
        }
        
    }

} // End Class Home

?&gt;

Code:
&lt;?php if(isset($errors)): ?&gt;
    
    <div class="errors">
        &lt;?php foreach($errors as $message) echo "$message<br />" ?&gt;
    </div>
    <br />
    
&lt;?php endif; ?&gt;

<b>Registration Page</b>

<div id="registration"><table>
    &lt;?=form_open('register/handle')?&gt;
    <tr>
        <td>Username:</td>
        <td>&lt;input type="text" name="username" value="&lt;?php if(isset($_POST['username'])) echo $_POST['username']; ?&gt;" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Password:</td>
        <td>&lt;input type="password" name="password" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Confirm Password:</td>
        <td>&lt;input type="password" name="conf_password" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Email:</td>
        <td>&lt;input type="text" name="email" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Confirm Email:</td>
        <td>&lt;input type="text" name="conf_email" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Secret Question:</td>
        <td>&lt;input type="text" name="question" value="&lt;?php if(isset($_POST['question'])) echo $_POST['question']; ?&gt;" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Answer:</td>
        <td>&lt;input type="text" name="answer" value="&lt;?php if(isset($_POST['answer'])) echo $_POST['answer']; ?&gt;" /&gt;&lt;/td>
    </tr>
    <tr>
        <td colspan="2">&lt;input type="submit" name="submit" value="Register" /&gt;&lt;/td>
    </tr>
</table></div>&lt;!--Registration--&gt;

#5
[eluser]Aea[/eluser]
If you're using the output class you shouldn't be echo/printing anything within your code.

#6
[eluser]markanderson993[/eluser]
Where am I using the output class? Can I change it to allow me to echo/print anything after my code?

#7
[eluser]Aea[/eluser]
Err sorry, low on Caffeine at the moment Wink

Usually that error crops up if you're utilizing the output class, see: http://ellislab.com/codeigniter/user-gui...utput.html

#8
[eluser]markanderson993[/eluser]
I'm still having a heck of time finding where I'm using the output class :/ I checked Google for this error and didn't find any results so this error must be unique to the code written above. Can anyone point out where I am making an error and post the code to fix it?

Also, my question about installing a more secure session class is still on the table. If anyone knows a helpful tutorial on how to use a DB2 class instead of the original session class in CodeIgniter, please post.

#9
[eluser]charlie spider[/eluser]
you've got an echo in your controller:

echo 'Congratulations you have been successfully registered!';

which is being executed prior to any view being loaded.

load your success message into a variable that is echo'd from your register_view page, or create a registration_success_view page and display your success message there.

#10
[eluser]markanderson993[/eluser]
Thanks for the reply. Alright so I changed my code at the bottom. Problem is, I'm still getting the error.

Code:
&lt;?php // home.php (Class)

Class Register extends Controller {

    function Register()
    {
        parent::Controller();
        $this->load->helper('form');
    }
    
    function index()
    {    
        $this->template->load('register/register_view');
    }
    
    function handle()
    {
    
        if (empty($_POST['username']))
        {
            $errors[] = 'Please enter your username';
        }
        
         if ( (empty($_POST['password'])) || (empty($_POST['conf_password'])) )
        {
            $errors[] = 'Please enter your password';
        } else {
        
            if ( ($_POST['password']) !== ($_POST['conf_password']) )
            {
                $errors[] = 'The passwords you entered did not match';
            }
            
        }
        
         if ( (empty($_POST['email'])) || (empty($_POST['conf_email'])) )
        {
            $errors[] = 'Please enter your email';
        } else {
        
            if ( ($_POST['email']) !== ($_POST['conf_email']) )
            {
                $errors[] = 'The email adresses you entered did not match';
            }
            
        }
        
        if (empty($_POST['question']))
        {
            $errors[] = 'Please enter your secret question';
        }
        
        if (empty($_POST['answer']))
        {
            $errors[] = 'Please enter your secret answer';
        }
        
        if (!empty($errors))
        {
            $data['errors'] = $errors;
            $this->template->load('register/register_view',$data);
        } else {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $question = $_POST['question'];
            $answer = $_POST['answer'];
            $this->redux_auth->register($username, $password, $email, $question, $answer);
            $this->template->load('register/register_success_view');
        }
        
    }

} // End Class Home

?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.