Hello, Im having some trouble with some php and html and was wondering if someon
ID: 3716104 • Letter: H
Question
Hello,
Im having some trouble with some php and html and was wondering if someone could give me some help :)
I have the following code in a file calle formd.php
<fieldset>
<div class="required_field">
<label for="firstname">First Name:</label>
<input name="fname" id="firstname" type="text" value="<?php
if(isset($_POST['fname'])) echo htmlspecialchars($_POST['fname']);
?>"/>
<span id="firstnameError" class="error">
<?php if (isset($errors['fname'])) echo $errors['fname'] ?>
</span>
</div>
<div class="required_field">
<label for="email">Email:</label>
<input type="text" id="email" name="email" value="<?php
if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']);
?>"/>
<span id="emailError" class="error">
<?php if (isset($errors['email'])) echo $errors['email'] ?>
</span>
</div>
</fieldset>
This section works however I am having trouble with the next section of the work... The next section is suppossed to generate the above code with php however I cant seem to get it to work. The details are below..
Notice the replication in the above code. We can avoid the replication by creating functions (placed
in a separate include file (This section is in a file called processDatad.php) to output the required HTML:
function input_field($errors, $name, $label) {
echo '<div class="required_field">';
label($name, $label);
$value = posted_value($name);
echo "<input type="text" id="$name" name="$name" value="$value"/>";
errorLabel($errors, $name);
echo '</div>';
}
You will need to implement the missing functions (label, posted_value and errorLabel).
We can then generate both input fields by just calling:
input_field($errors, 'fname', 'First Name');
input_field($errors, 'email', 'Email');
Explanation / Answer
here is your all function with some test code : --------->>>>>>>>>>
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<?php
function label($name,$label){
echo '<label for="'.$name.'">'.$label.' : </label>';
}
function posted_value($name){
if(isset($_POST[$name])){
return htmlspecialchars($name);
}else{
return " ";
}
}
function errorLabel($error,$name){
echo '<span id="$name'.'Error'.'" class="error"> ';
if(isset($error[$name])){
echo $error[$name];
}
echo '</span>';
}
function input_field($errors, $name, $label) {
echo '<div class="required_field">';
label($name, $label);
$value = posted_value($name);
echo "<input type="text" id="$name" name="$name" value="$value"/>";
errorLabel($errors, $name);
echo '</div>';
}
$errors = array("email" => "email Error","fname" => "first name error");
input_field($errors,'email','Email');
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.