Page 1 of 1

Help with class error

Posted: Tue Jul 14, 2009 9:12 pm
by gimpact
Hello,
I made this small script to learn the usage of class in php. Can some one please tell me why i am getting this error?

Thank you,

Register.php

Code: Select all

<?php
 include 'PrintNow.php';
 
if(isset($_REQUEST['submit'])){
    // Get values from user
    $firstname = $_REQUEST['firstname'];
    $lastname = $_REQUEST['lastname'];
    $email = $_REQUEST['email1'];
    $re_typedEmail = $_REQUEST['email2'];
    $password = $_REQUEST['password1'];
    $re_typedPassword = $_REQUEST['password2'];
 
    //Check for null values
    if(($firstname == null) || ($firstname == "")){
        header("Location: ../index.php?value=firstname");
    }elseif(($lastname == null) ||($lastname == "")){
        header("Location: ../index.php?value=lastname");
    }elseif(($email == null) || ($email == "") ||
        ($re_typedEmail == null) || ($re_typedEmail == "")){
        header("Location: ../index.php?value=email");
    }elseif(($password == null)|| ($password == "") ||
        ($re_typedPassword == null) || ($re_typedPassword == "")){
        header("Location: ../index.php?value=password");
    }
 
    //Check for Email and Password mismatch
    if($email != $re_typedEmail){
        header("Location: ../index.php?value=email1");
    }elseif($password != $re_typedPassword){
        header("Location: ../index.php?value=password1");
    }
 
    $printnow = new PrintNow;
    $printnow ->printNow($firstname, $lastname);
    
 
}else{
    header("Location: ../index.php");
}
?>
 
PrintNow class

Code: Select all

<?php
 
class PrintNow {
 
    public $firstname;
    public $lastname;
 
    function printNow($firstname, $lastname){
        $this-> firstname = $first;
        $this-> lastname = $last;
 
        echo $this-> firstname;
        echo "<br>";
        echo $this-> lastname;
    }
 
}
?>
 
Error that I am getting

Code: Select all

Warning: Missing argument 1 for PrintNow::printNow(), called in C:\xampp\htdocs\register\inc\Register.php on line 33 and defined in C:\xampp\htdocs\register\inc\PrintNow.php on line 8
 
Warning: Missing argument 2 for PrintNow::printNow(), called in C:\xampp\htdocs\register\inc\Register.php on line 33 and defined in C:\xampp\htdocs\register\inc\PrintNow.php on line 8

Re: Help with class error

Posted: Tue Jul 14, 2009 9:53 pm
by Christopher
I think you need to get rid of all of the errors first (such as using $first and $last in printNow() ). And I really don't know what you are trying to do with all of those redirects, but please stop! ;) There should be one redirect max.

Remember that HTTP parameters are pass as "index.php?name=value", so "index.php?firstname=Bob&lastname=Smith" will populate $_GET['firstname'] and $_GET['lastname']. Doing "index.php?value=firstname" will populate $_GET['value'].

Re: Help with class error

Posted: Tue Jul 14, 2009 10:32 pm
by gimpact
Thank you for replying.

If i remove $first and $last in function printNow() then how can i make my function print some thing? Do you think it would be wise to return the string that i want to print and then print it Register.php?

Those redirects are error redirects. Did you mean that I should assign all error values to a variable and have one redirect? Too make my code look neat, i have, you know, typed and indented well. :-)

All codes before calling PrintNow class works fine. I have tested it using echo statement.

Re: Help with class error

Posted: Tue Jul 14, 2009 10:38 pm
by Christopher
gimpact wrote:If i remove $first and $last in function printNow() then how can i make my function print some thing? Do you think it would be wise to return the string that i want to print and then print it Register.php?
The parameters are $firstname and $lastname. The variables $first and $last are not defined.
gimpact wrote:Those redirects are error redirects. Did you mean that I should assign all error values to a variable and have one redirect? Too make my code look neat, i have, you know, typed and indented well. :-)
The usual practice is to redirect when there are no errors. That gets rid of the resubmit problem with forms. You should use logic for the errors and collect the error messages in a string or array of strings. Then redisplay the form with the error messages if there are errors.