0

Hi I want to make form dynamically by just passing array of stuffs. I have tried but cannot may be due to my limitation in PHP. Here is what I have so far. Please help me!

class MyForm{
    private  $formArray;
    //pass array to build a form in form of name=>type eg username=>text to a constructor
    public  function   __construct($postArray) {
        //make the form POST variables available to the class
        $this->formArray = $postArray;
        //print_r($this->formArray);
    }

    //construct a form
    public  function  buildForm($actionFilePath='login.php'){
        echo "<form action='$actionFilePath' method='POST' id='loginform'>";
        echo "<table border='1', cellpadding='5'> ";
        foreach ($this->formArray as $name => $type) {
            echo "<tr>
                 <td><input type='$type' name='$name' id='$name' /></td>
                 </tr>";
            

        }//end foreach

        echo "</table>";
    }



}// end form class
3
Contributors
19
Replies
21
Views
7 Years
Discussion Span
Last Post by Stefano Mtangoo
0

It looks like you have all of the essentials, except one thing...no FormArray definition in the code above.
Nothing in -> nothing out.
Put some names and types in there and it should work.

0

It looks like you have all of the essentials, except one thing...no FormArray definition in the code above.
Nothing in -> nothing out.
Put some names and types in there and it should work.

No JRM,
The array is passed in the constructor during instantiation of the class.
see this comment

//pass array to build a form in form of name=>type eg username=>text to a constructor

Also the code works but I don't know how to add a field for labels. This code just displays controls and no labels. I lack technique to add labels

Thanks for reply

Edited by Stefano Mtangoo: n/a

0

No JRM,
The array is passed in the constructor during instantiation of the class.
see this comment

My point was that THERE IS NO ARRAY defined in the original, therefore the constructor has nothing to output. You say it works, I guess you must have it defined somewhere, then.
In the the first post you said it DIDn't work, but now you need labels?
That' s easy, just use a label tag before the input tag

<td><label FOR ='$name'> $name </label><input type='$type' name='$name' id='$name' /></td>
0

You could have a slight problem with id='$name' name='$name' . Sometimes it's important to have the same 'name' for radio buttons, or even the same arrayname for textboxes (etc) that you WANT to be placed into an array. Obviously, each individual form widget MUST have different 'id' values.

Don't know if I'm being clear or not. In other words, 'id' not always equal to 'name'.

0

My point was that THERE IS NO ARRAY defined in the original, therefore the constructor has nothing to output. You say it works, I guess you must have it defined somewhere, then.
In the the first post you said it DIDn't work, but now you need labels?
That' s easy, just use a label tag before the input tag

<td><label FOR ='$name'> $name </label><input type='$type' name='$name' id='$name' /></td>

OOps!
you are right! I was not clear to the post. Sorry for that.
Here is the thing. The array is defined when using the class I will put the code a bit later. The problem is (Which sadly I failed to address well) I want the label to be part of array. In fact I want the whole form to be constructed dynamically so it will be flexible.

I hope I'm clear now

0

You could have a slight problem with id='$name' name='$name' . Sometimes it's important to have the same 'name' for radio buttons, or even the same arrayname for textboxes (etc) that you WANT to be placed into an array. Obviously, each individual form widget MUST have different 'id' values.

Don't know if I'm being clear or not. In other words, 'id' not always equal to 'name'.

Sure, they must be different. I did name=id to make array simple. If I had to make another array containing the ids it would be complex. But If there is a way to do that, I will be glad to learn :)

0

here is the code to do a test. The above code is in the include file

<html>
    <head><title>Test Login Form</title></head>

    <body>
        <?php

            require('inc.form.php');
            echo '<h1>Register</h1>';
            $formArray = array('username'=>'text', 'password'=>'password', 'submit'=>'submit');
            //print_r($formArray);
            $form = new MyForm($formArray);
            $form->buildForm('inc.form.php');
        ?>


    </body>



</html>
0

Don't you need to echo the form?
try it this way:

html>
    <head><title>Test Login Form</title></head>

    <body>
        <?php

            require('inc.form.php');
            echo '<h1>Register</h1>';
            $formArray = array('username'=>'text', 'password'=>'password', 'submit'=>'submit');
            //print_r($formArray);
            $form =  MyForm::buildForm($formArray);
            echo $form;
        ?>


    </body>

...but you say it works...?

Edited by JRM: n/a

0

Naw, on second thought, that's not going to work the way Idid it.
If builtForm took the array and made the form from it,instead of having a default file as an argument, then it would.

You have convoluted layers of complexity that I don't understand the purpose for.
To many unexpected twists and turns for my simple mind...sorry...

0
$form->buildForm('inc.form.php');

Specifies ACTION in the form

Edited by Stefano Mtangoo: oops, what is wrong with me?

0
$form->buildForm('inc.form.php');

Specifies ACTION in the form

IMHO, too many inner workings to do a simple thing.
You don't even need a class unless you have future plans for more members.
A class is a collection of functions of similar use.
A constructor is for overriding or adding to a class member.
A login form is a login form, I doubt that you will have a reason to modify it's process.
My approach would be to just define a function that makes the login form.
I don't see why you want to use an array either, how are you going to change it in the future?
A class is if you had a bunch of form generators as some sort of abstract class, but in this context I don't see the merit.

0

you are right, that is one of the bunch of things in that class.
The class will have to handle all form activities like validating names, birthdays emails, etc.

Why would I want to do that way? well, I want user to define his array of what he needs and then the function will construct login form. If you need two fileds, then pass only the needed array, want gazillion? do the same! you get my point?

This is small thing that class will do. And I want it to be extensible and very generic

0

OK, but the constructor seems to be excess baggage if you want the function to take an array anyway.

lass MyForm{
    private  $formArray;
    //pass array to build a form in form of name=>type eg username=>text to a constructor


/*You are making the array available to the entire class. What is the point of that ? A specific array would need to go to a specific method anyway, so why not just make it a second argument to the function.
Nix this...????  */
    public  function   __construct($postArray) {
        //make the form POST variables available to the class
        $this->formArray = $postArray;
        //print_r($this->formArray);
    }
/*Here you setting this function to login.php form the get-go, no flexibility there! What would be better is take in the variable test it, if null set to the default. put the second array argument in and you have a versatile funtion that is simple to use. */
    //construct a form
    public  function  buildForm($actionFilePath='login.php'){
        echo "<form action='$actionFilePath' method='POST' id='loginform'>";
        echo "<table border='1', cellpadding='5'> ";
        foreach ($this->formArray as $name => $type) {
            echo "<tr>
                 <td><input type='$type' name='$name' id='$name' /></td>
                 </tr>";
            

        }//end foreach

        echo "</table>";
    }



}// end form class
0

First thanks for taking your time to answer to the post

OK, but the constructor seems to be excess baggage if you want the function to take an array anyway.

Yes I might be wrong but my point was that, the array be global to the class because there are other functions that will have to use array like sanitizing, validating et al

What do you propose as alternative to this?

class MyForm{
    private  $formArray;
    //pass array to build a form in form of name=>type eg username=>text to a constructor


/*You are making the array available to the entire class. What is the point of that ? A specific array would need to go to a specific method anyway, so why not just make it a second argument to the function.
Nix this...????  */
    public  function   __construct($postArray) {
        //make the form POST variables available to the class
        $this->formArray = $postArray;
        //print_r($this->formArray);
    }

Hope I have explained my intention

/*Here you setting this function to login.php form the get-go, no flexibility there! What would be better is take in the variable test it, if null set to the default. put the second array argument in and you have a versatile funtion that is simple to use. */
    //construct a form
    public  function  buildForm($actionFilePath='login.php'){
        echo "<form action='$actionFilePath' method='POST' id='loginform'>";
        echo "<table border='1', cellpadding='5'> ";
        foreach ($this->formArray as $name => $type) {
            echo "<tr>
                 <td><input type='$type' name='$name' id='$name' /></td>
                 </tr>";


        }//end foreach

        echo "</table>";
    }



}// end form class

end quote.

Here I have put it as default value. If you specify one, you will override the default. If you forgot to fill, it will mislead you to login.php. Again it was for solely testing purpose.

Can you write on with all those proposals, the simple? I love simple things, the beauty of simplicity ;)

Edited by mike_2000_17: Fixed formatting

0

Yes I might be wrong but my point was that, the array be global to the class because there are other functions that will have to use array like sanitizing, validating et al

Let's say that the form parameters are collected from a configuration form page. The array is generated from that, so the form handler does the dirty work. Your "sanitizing" takes place long before that array shows up to be used as an argument (hopefully).
So using the "KISS" method, the final method looks like this:

#
public function buildForm($actionFilePath='login.php', $Array){
            //test for $actionFile Path, throw error and/or default 
            // test for $Array , throw  error if null or wrong type, etc
           // make the form
          //return the form as a string
         }
0

Let's say that the form parameters are collected from a configuration form page. The array is generated from that, so the form handler does the dirty work. Your "sanitizing" takes place long before that array shows up to be used as an argument (hopefully).
So using the "KISS" method, the final method looks like this:

#
public function buildForm($actionFilePath='login.php', $Array){
            //test for $actionFile Path, throw error and/or default 
            // test for $Array , throw  error if null or wrong type, etc
           // make the form
          //return the form as a string
         }

your idea seems practical. But could you elaborate more. Do you mean I should have another php file to hold form configurations or what? I'm lost here so elaborate please! :confused:

0


your idea seems practical. But could you elaborate more. Do you mean I should have another php file to hold form configurations or what? I'm lost here so elaborate please!

That's just it. This is YOUR brainchild, not mine. I really haven't a clue about what you have in mind (nor do I want one) for the "big picture".

All I am saying is, plan your code so that it is as simple as possible, Annotating functions and classes is helpful if you have to revisit it a year or two later.

That form generator can be expanded to take more arguments such as attributes, etc.
I don't know how fancy you need it or what the end game is.
Maybe if you sat down and did a flow chart outline of what you are attempting might help you?

Writing a bunch of disembodied code without a "roadmap" will make you crazy.

0

That's just it. This is YOUR brainchild, not mine. I really haven't a clue about what you have in mind (nor do I want one) for the "big picture".

All I am saying is, plan your code so that it is as simple as possible, Annotating functions and classes is helpful if you have to revisit it a year or two later.

That form generator can be expanded to take more arguments such as attributes, etc.
I don't know how fancy you need it or what the end game is.
Maybe if you sat down and did a flow chart outline of what you are attempting might help you?

Writing a bunch of disembodied code without a "roadmap" will make you crazy.

:)

0

I did it finally. I have to add another array with label. Also I passed array by reference.
Thanks JRM for challenges and contrib.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.