So, I'm trying to create a very basic form using HTML/PHP. Yes, I am a beginner.
I'm going for something along the lines of this.
But instead, I'm getting this.
The form has text input for a few fields such as "Name", "Username", "Email", and then a drop down list to select a browser of your choice. Very basic stuff. I'm trying to return the user input to the screen. The only problem is that when the page loads, I have an empty drop-down box with no browsers to choose from.
I created a PHP class called "Select" that is very simple. It has a few basic functions. I also created two other files, postInfo.php (used to return user info) and userForm.php (the file to create the form itself).
I've tried debugging and testing and I'm almost certain there is nothing wrong the Select class, and the two other files have been run through code validators and nothing comes up. Everything should be fine, but this is starting to give me a headache.
Here's my Select class:
<?php
$browsers = array ("Firefox", "Chrome", "Internet Explorer", "Safari", "Opera", "Konqurer", "Other");
class Select {
private $name;
private $value;
#Getter & Setter methods
public function setName($name) {
$this -> name = $name;
}
public function getName() {
return $this -> name;
}
public function setValue($value){
if (!is_array($value)){
echo ("No array values found.");
}
$this->value = $value;
}
public function getValue() {
return $this -> value;
}
#Method to implement browsers
private function createBrowserList($value){
foreach($value as $val){
echo "<option value=\"$val\">" . $val . "</option>
";
}
}
#Method to create the selection field
public function createSelections(){
echo "<select name=\"" . $this->getName() . "\">
";
$this -> makeOptions ($this->getValue());
echo "</select>" ;
}
}
?>
My postInfo.php file:
<?php
$name = $_POST["name"];
$username = $_POST["username"];
$email = $_POST["email"];
$browser = $_POST["browser"];
echo "$name <br>";
echo "$username <br>";
echo "$email <br>";
echo "$browser <br>";
?>
And my userForm.php file (used to create the actual form)
<html>
<body>
<form action ="postInfo.php" method="post" id="form">
Name:<input type ="text" name="name"> <br>
Username:<input type ="text" name="username"> <br>
Email:<input type ="email" name="email"> <br>
<?php
include "selectClass.php";
$selection = new Select();
$selection -> setName("Select Browser");
$selection -> setValue($browsers);
$selection -> createSelections();
?>
<input type = "submit" name = "submit" value = "Go!">
</form>
</body>
</html>
public function createSelections(){
echo "<select name=\"" . $this->getName() . "\">
";
$this -> createBrowserList ($this->getValue());
echo "</select>" ;
}
You could use something like this:
<?php
$browservalue=array();
$browsers = array ("Firefox", "Chrome", "Internet Explorer", "Safari",
"Opera", "Konqurer", "Other");
foreach ($browsers as $x){
$browservalue[]=strtolower($x);
}
$browsersqnty=sizeof($browsers);
?>
and then inside the html where you want to echo browsers you can do this:
Browsers:<select name='browsers'><?php
for($i=0;$i<$browsersqnty;$i++){
echo "<option value='$browservalue[$i]'>$browser[$i]</option>";
}
?></select>