I am having trouble showing the output that i called in printf in php tutorial. I just followed the tutorial but still can't figure out what is wrong in displaying the variables inside the printf in localhost. Is anyone can help me. Thank you.
<?php
$name = '';
$password = '';
$gender = '';
$color = '';
$languages = [];
$comments = '';
$tc = '';
if (isset($_POST['submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
};
if (isset($_POST['password'])) {
$password = $_POST['password'];
};
if (isset($_POST['gender'])) {
$gender = $_POST['gender'];
};
if (isset($_POST['color'])) {
$color = $_POST['color'];
};
if (isset($_POST['languages'])) {
$languages = $_POST['languages'];
};
if (isset($_POST['comments'])) {
$comments = $_POST['comments'];
};
if (isset($_POST['tc'])) {
$tc = $_POST['tc'];
};
//here's the problem i cant resolve printing out the output//
printf('User name: %s
<br>Password: %s
<br>Gender: %s
<br>Color: %s
<br>Language(s): %s
<br>Comments: %s
<br>T&C: %s',
htmlspecialchars($name, ENT_QUOTES),
htmlspecialchars($password, ENT_QUOTES),
htmlspecialchars($gender, ENT_QUOTES),
htmlspecialchars($color, ENT_QUOTES),
htmlspecialchars(implode('', $languages), ENT_QUOTES),
htmlspecialchars($comments, ENT_QUOTES),
htmlspecialchars($tc, ENT_QUOTES));
}
?>
<form action=""
method="post">
User name: <input type="text" name="name"><br>
Password: <input type="password" value="password"><br>
Gender:
<input type="radio" name="gender" value="f"> female
<input type="radio" name="gender" value="m"> male
<input type="radio" name="gender" value="o"> other<br/>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00">red</option>
<option value="#0f0">green</option>
<option value="#00f">blue</option>
</select><br>
Languages spoken:
<select name="languages[]"multiple size="3">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select><br>
Comments: <textarea name="comments"></textarea><br>
<input type="checkbox" name="tc" value="ok"> I accept the T&C<br>
<input type="submit" value="Register">
</form>`
CodePudding user response:
The problem here is if (isset($_POST['submit']))
there is no any form field with the name submit
and it will never become true to execute. Remove the if Condition with submit or Else give the sumbit button name as Submit
<input type="submit" value="Register" name="submit">
CodePudding user response:
Errors in your previous code are:
- No semicolons
;
are needed after endingif
statements. - No
name
attribute is specified for password input in the form. - Posting form data as
if (isset($_POST['submit'])) {...}
without specifying thename="submit"
attribute in the form. - Invalid use of
implode
function which will generate output be likeenfrit
or vice versa.
Additional Tips
- Add the
required
attribute to eachinput
to prevent from sending empty form data. - You have already specified
method="post"
in the form tag so, no need for validating each input field asif (isset($_POST['fieldname'])) {...}
.if (isset($_POST['submit'])) {...}
will send all the form data asPOST
method.
And lastly, here is your updated code.
<?php
if (isset($_POST['submit'])) {
$name = '';
$password = '';
$gender = '';
$color = '';
$languages = [];
$comments = '';
$tc = '';
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset($_POST['gender'])) {
$gender = $_POST['gender'];
}
if (isset($_POST['color'])) {
$color = $_POST['color'];
}
if (isset($_POST['languages'])) {
$languages = $_POST['languages'];
}
if (isset($_POST['comments'])) {
$comments = $_POST['comments'];
}
if (isset($_POST['tc'])) {
$tc = $_POST['tc'];
}
//print output
printf('User name: %s
<br>Password: %s
<br>Gender: %s
<br>Color: %s
<br>Language(s): %s
<br>Comments: %s
<br>T & C: %s
<br><br>',
htmlspecialchars($name, ENT_QUOTES),
htmlspecialchars($password, ENT_QUOTES),
htmlspecialchars($gender, ENT_QUOTES),
htmlspecialchars($color, ENT_QUOTES),
htmlspecialchars(implode(', ', $languages), ENT_QUOTES),
htmlspecialchars($comments, ENT_QUOTES),
htmlspecialchars($tc, ENT_QUOTES));
}
?>
<form action="" method="post">
User name: <input type="text" name="name">
<br>
Password: <input type="password" name="password">
<br>
Gender:
<input type="radio" name="gender" value="f"> female
<input type="radio" name="gender" value="m"> male
<input type="radio" name="gender" value="o"> other
<br>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00">red</option>
<option value="#0f0">green</option>
<option value="#00f">blue</option>
</select>
<br>
Languages spoken:
<select name="languages[]" multiple size="3">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select>
<br>
Comments: <textarea name="comments"></textarea>
<br>
<input type="checkbox" name="tc" value="ok"> I accept the T & C
<br>
<input type="submit" name="submit" value="Register">
</form>