SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Help needed with OOP

    Aloha,

    I started learning OOP and for my first class I wanted to build 'form generator'. Class worked beautifully, so I wanted to make it more dynamic.

    Story:
    while the user hits 'submit' button, I want to display the form AGAIN, filled with whatever user entered (confirmation). Without class I do thing like that by checking hidden file and if it exists, I just print values for each field, eg.

    PHP Code:
    <input type="text" name="name" class="form1" value="<? if ($go == '1') { echo $name; } ?>
    now, I wanted to do the same inside my class, but I don't know how to make the script read that hidden var.

    Part of my class's code - submit function, where i put hidden var the way I would do without any class:
    PHP Code:
        function f_submit($name="Submit"$value="") {
            echo 
    "<br><br>";

            echo 
    "<input type=\"submit\"";
            echo 
    " name=\"" str_replace(" ""_"$name) . "\"";

                if (
    $value){
                    echo 
    " value=\"" .$value"\"";
                } else {
                    echo 
    " value=\"" .$name"\"";
                }
            echo 
    ">";
            echo 
    "<input type=\"hidden\" name=\"go\" value=\"1\">";
        } 
    and then in my other functions which print proper fields I check if the $go has a value, and if it has - print fields value.

    PHP Code:
        function f_input($title$name$value="") {
            echo 
    "<br><br>";
            echo 
    $title "<br>";
            echo 
    "<input type=\"text\"";

            if (!
    $name){
                
    $name str_replace(" ""_"$title);
                }

            echo 
    " name=\"" .$name"\"";
            echo 
    " value=\""
                if (
    $go == '1') echo $name;
            echo 
    "\"";
          } 
    and here this IF condition [if ($go == '1')] simply does not work. How can I make it work?

    Actually, this leads to second problem. While contruction
    PHP Code:
    <input type="text" name="name" class="form1" value="<? if ($go == '1') { echo $name; } ?>
    allowed me to print actual value of $name entered by user, the contruction for class will take a value for $name from funtion's argument, NOT from whatever user entered.

    How can I bypass that and make a function and class working?


    Thank you a lot for any help.
    G.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $go does not appear to be set anywhere. Do you mean $_POST['go']?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Sweatje,

    I tried $_POST['go'] before without results. Maybe I put that with some error, because I tried it again few secods ago and it beautifully worked

    Thank you!



    The IF condition worked, but as I expected it printed function's argument instead of the string I typed in the input field.

    PHP Code:
        function f_input($title$name$value="") {
            echo 
    "<br><br>";
            echo 
    $title "<br>";
            echo 
    "<input type=\"text\"";

            if (!
    $name){
                
    $name str_replace(" ""_"$title);
                }

            echo 
    " name=\"" .$name"\"";
            echo 
    " value=\""
                if (
    $_POST['go'] == '1') echo $name;
            echo 
    "\"";
          } 
    while creating a form and typing, eg.
    PHP Code:
    $form = new make_form();
    $form->f_input("Enter something"""""); 
    it produces:
    Enter something<br><input type="text" name="Enter_something" value="">

    and then, after hitting 'submit', the class prints 'Enter_something' in that input field as a value, not what I had typed there.

    Of course, beacuse $name is both function argument and input field name. But, how can I print input field's value, not argument's value?

    Thanks.
    G.

  4. #4
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed that by adding another $_POST value.

    PHP Code:
        function f_input($title$name$value="") {
            echo 
    "<br><br>";
            echo 
    $title "<br>";
            echo 
    "<input type=\"text\"";

            if (!
    $name){
                
    $name str_replace(" ""_"$title);
                }

            echo 
    " name=\"" .$name"\"";
            echo 
    " value=\""
                if (
    $_POST['go'] == '1') echo $_POST[$name]; else echo $value;
            echo 
    "\"";
          } 
    and it works now as it should.

    Thanks Sweatje for a hint!


    G.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •