0
Php help
Hi all, Trying to figure how to do this with php. Create a form class with a constructor that accepts the following parameters. o form method o form action Any assistance please. What would the code look like with explanation.
12 Respuestas
+ 2
I've made a simple example for you. There are libraries to help build html forms. If you want it for learning purposes check out my code.
https://code.sololearn.com/wvK98y8dxB02/?ref=app
+ 1
In the bottom part of the code are two examples of how to use the Form class. Lets see one of them:
// This creates a new form
$myForm = new Form();
// The first text input is added
$myForm->add('text', 'username');
// The first submit button is added
$myForm->add('submit');
// Output the form to the document
echo $myForm;
When the last line is executed, the __toString() method of Form class is called.
+ 1
The __toString() method generates a string, containing html markup for the form and the elements within it. Here is the code explained:
// Method to get the form as a string
public function __toString() {
// Assign to a variable the opening <form> tag with method and action
$html = "<form method=\"$this->method\" action=\"$this->action\">";
// For each element added to the form concat to html an <input> tag
// Here is where the text input and the submit button added to the form are created as html tags
foreach ($this->elements as $elem)
$html .= "<input type=\"{$elem['type']}\" name=\"{$elem['name']}\" /><br />";
// Concat to the html variable the closing </form> tag
$html .= "</form>";
// Return the string
return $html;
}
0
Thanks
0
Ernesto how does the code render a form on the output with the html <form> element in it. I don't see it anywhere but the output shows two text areas and two submit buttons.
I am use to seeing code between html form tags but is this code is renders it anyway. What code is producing the two submit buttons and the text areas?
0
I think I would where the form is made below. But where is the submit button coded?
// Method to get the form as a string
public function __toString() {
$html = "<form method=\"$this->method\" action=\"$this->action\">";
0
// Method to get the form as a string
public function __toString() {
$html = "<form method=\"$this->method\" action=\"$this->action\">";
foreach ($this->elements as $elem)
$html .= "<input type=\"{$elem['type']}\" name=\"{$elem['name']}\" /><br />";
$html .= "</form>";
return $html;
0
I love it PHP is becoming my favorite web development programming language is like HTML with super powers
0
( ! ) Fatal error: Cannot redeclare class Form in C:\wamp64\www\PHPConstructor3\other-page.php on line 32
Call Stack
#
Time
Memory
Function
Location
1 0.0006 247352 {main}( ) ...\index.php:0
2 0.0012 265672 include_once( 'C:\wamp64\www\PHPConstructor3\other-page.php' ) ...\index.php:4
getting errors above I made a other-page.php file
I used created a file include_once "other-page.php";
This is my code. Can you tell me what I did wrong and give me an example of the other-page.php code would look like?
This what I coded
<?php
/**
*
*/
class MyClass {
public $var = 'I love OOP';
public function __construct($text){
$this->var = $text;
}
public function my_function() {
echo $this->var;
}
}
$myclass = new MyClass('Meh, OOP is alright');
$newclass = new MyClass('Hi');
$myclass -> my_function();
$newclass -> my_function();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User has submitted the form and entered this name : <b> $name </b>";
echo "<br> You can use the following form again to enter a new name.";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form">
<br>
</form>
</body>
</html>
0
I see nothing wrong with the code you have posted here. However the message "Fatal error: Cannot redeclare class Form" suggests that the Form class is being declared or included in more than one place. You have to check all your other php files.
0
Can you code a simple other-page.php file that would link to the index file you made? I think I am confusing how the code should like and how to incorporate ""require or include " properly
0
I figured it out how would I enable and disable the submit button using Javascript based on code you did? This code works but don't know how to incorporate it.
<!DOCTYPE html>
<html>
<body>
<button id="myBtn">My Button</button>
<p>Click the button below to disable the button above.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("myBtn").disabled = true;
}
</script>
</body>
</html>