Reputation: 43
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.
Upvotes: 1
Views: 8812
Reputation: 268
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>
Upvotes: 1
Reputation: 43
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?
Upvotes: 0
Reputation: 92
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.
Upvotes: 1
Reputation: 19112
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.
Upvotes: 0