Problem inserting an array into another array - El Forum - 06-29-2008
[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 />";
}
?>
<b>Registration Page</b>
<div id="registration"><table>
<?=form_open('register/handle')?>
<tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
<tr><td>Confirm Password:</td><td><input type="password" name="conf_password" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td>Confirm Email:</td><td><input type="text" name="conf_email" /></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Register" /></td></tr>
</table></div><!--Registration-->
Problem inserting an array into another array - El Forum - 06-29-2008
[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;
}
Problem inserting an array into another array - El Forum - 06-29-2008
[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
Problem inserting an array into another array - El Forum - 06-29-2008
[eluser]markanderson993[/eluser]
Alright the script is nearing completion however, when the script is executed I get a headers already sent error 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/72525/P120/#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: <?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
?>
Code: <?php if(isset($errors)): ?>
<div class="errors">
<?php foreach($errors as $message) echo "$message<br />" ?>
</div>
<br />
<?php endif; ?>
<b>Registration Page</b>
<div id="registration"><table>
<?=form_open('register/handle')?>
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="password" name="conf_password" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Confirm Email:</td>
<td><input type="text" name="conf_email" /></td>
</tr>
<tr>
<td>Secret Question:</td>
<td><input type="text" name="question" value="<?php if(isset($_POST['question'])) echo $_POST['question']; ?>" /></td>
</tr>
<tr>
<td>Answer:</td>
<td><input type="text" name="answer" value="<?php if(isset($_POST['answer'])) echo $_POST['answer']; ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Register" /></td>
</tr>
</table></div><!--Registration-->
Problem inserting an array into another array - El Forum - 06-29-2008
[eluser]Aea[/eluser]
If you're using the output class you shouldn't be echo/printing anything within your code.
Problem inserting an array into another array - El Forum - 06-29-2008
[eluser]markanderson993[/eluser]
Where am I using the output class? Can I change it to allow me to echo/print anything after my code?
Problem inserting an array into another array - El Forum - 06-29-2008
[eluser]Aea[/eluser]
Err sorry, low on Caffeine at the moment
Usually that error crops up if you're utilizing the output class, see: http://ellislab.com/codeigniter/user-guide/libraries/output.html
Problem inserting an array into another array - El Forum - 06-30-2008
[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.
Problem inserting an array into another array - El Forum - 06-30-2008
[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.
Problem inserting an array into another array - El Forum - 06-30-2008
[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: <?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
?>
|