1

I am doing a project in PHP 5 where I want to set the values of form elements like name, email, password, etc to be the property attributes for objects in php. how do I do this? please help.

3
  • mmm.....i wont downvote because i didn't understood your question...wanna elaborate mate?? :) Commented Dec 31, 2013 at 15:15
  • Using HTML forms and PHP variables is simple but your question is not clear. The use of the phrase "property attributes for objects in php" in this context is especially confusing. Maybe explain the sequence of events instead? Commented Dec 31, 2013 at 15:26
  • thanks for your response. ok i want to create a form such that as users fill the form, the values of form elements get set automatically as property attributes of objects. where each form submitted automatically creates a new object. Commented Dec 31, 2013 at 15:41

4 Answers 4

1

You will have to create php objects, to store such attributes, or if you don't want to go that route you will have to just create a procefural function that stores those values and places it where it's suppose to go.

Object example

<?php

class User {
    private $name;
    private $email;
    private $password;

    public function __construct(array $data) {
        $this->name = isset($data['name'] ? trim($name) : null;
        $this->email = isset($data['name'] ? trim($email) : null;
        $this->password = isset($data['password']) ? trim($password) : null;
    }

    // Setters and getters defined here as well
    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = trim($name);
    }

}

So in your html form,

<form method="post" action="add_user.php">
    <input type="text" name="user[name]" id="name" />
    <input type="email" name="user[email]" id="email" />
    <input type="password" name="user[password]" id="password" />
    <input type="submit" value="Add User" />
</form>

Main thing to take notice is the method and action attribute of the form, method is how you are seending this over http, and action is to WHERE you are send these values, so in my dem o its sent to a script called add_user.php in the same directory, and via POST method.

the information will be receieved by php like:

$_POST['user'] => array('name' => '', 'email' => '', 'password' => '');

So what you do is just inside of your add_user.php script:

<?php

$userData = isset($_POST['user']) ? $_POST['user'] : array();

$User = new User($userData);

// FRom here on out you can do whatever you want with this.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your response. its useful.
1

I had the same problem. Hope it will useful for somebody. First you have to have object with __construct.

class User {
  public $name;
  public $email;

function __construct($name, $email ) {
    $this->date = $name;  
    $this->email = $email;
    }
}

Than make array for value which will be submit and connected with Object

     $userNew= [ new User($_GET['name'], $_GET['email'])     
];

Than your

<form action="whereYouWillHaveAllThisData.php" method="GET">
<input type="text"  name="name" >
<input type="text"  name="email" >
</form>

Comments

0

You can get the URL parameters by using the $_POST or $_GET variables, depending on which of those you used (by default, forms are GET).

If you want to do anything with the entered things in the form, just get the values via:

$x = $_GET['inputname']

To get the value of the input with name="inputname" if your request was a GET request. You can then do anything you want to do with that value.

5 Comments

set the values of form elements....are you setting the values or getting the values?? :)
@NoobEditor he wants to "set [those] values" to be the property attributes for objects. In other words, he wants to put the values of the form's inputs into an object. Atleast, that's what I got from this question.
m getting kinda old for this stuff i think :D
@mayanksingh i want to get values of form elements and set them to be property attributes for objects. such that each form submitted automatically creates a new object. thanks so much
@RichardFreeman Oh, you mean attributes of new input elements?
0

thank you all for your responses. but if i do something like this. will it be valid?

//class
public class user {
// variables
$name;
$age;

//constructor
public function __construct($age, $name){
this -> age = $age;
this -> name = $name;
}

//setter and getter methods for age
public function setAge($_POST['age']){
this -> age = $_POST['age'];
}
public function getAge(){
return this -> age;
}

//setter and getter methods for name
public function setName($_POST['name']){
this -> name = $_POST['name'];
}
public function getName(){
return this -> name;
}
} // end class

and the html form

<form method ="post" action ="htmlspecialchars($_SERVER['PHP_SELF'])">
Name: <input type="text" name="name">
Age: <input type ="text" name="text">
</form>

is the above code valid?

1 Comment

No, you have what appears to be php mixed into your html form (in the <form> action).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.