There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I have to admit, I was totally thrown by this aspect of question 1: “and the first letter in the input isNOTthe correct letter”.
Should it have been obvious that accessing a particular character within a string is done using something like $string[0]? I know previous lessons have demonstrated that you can access values within arrays using indexes, but I can’t recall one that showed that it’s possible with a non-array variable.
I totally understand it now, but not sure if this should have been something I picked up on immediately.
The code in this exercise (and all subsequent) does not seem to work in the real world.
This is specifically what I’m having trouble with
<input type=“text” id=“c-word” name=“c-word” value=<?=$_POST["c-word"]?>>
I get an undefined index error when I try and use a value=<?=$_POST["input-name"]?>
I copied the code from this exercise into my environment and tried to run it and it does not work like it does here in the Codecademy environment.
So how is this supposed to work in a real environment?
This happens when you try to access a variable that has no value assigned to it.
You can either declare your variable, or use the Null coalescing operator (compatible with PHP 7.x). It returns its first operand if it exists and is not NULL ; otherwise it returns the second.
<?= $_POST['c-word'] ?? '' ?>
If you echo gettype($_POST['c-word']);, you’d see that it’s NULL. Therefore the second operand of the Null coalescing operator will be returned which, in this case, is an empty string.
Ok so far so good, no error but when I submit an incorrect email (for example) I get my verbiage saying that the email is invalid but it then clears the form. I thought the whole point of doing the value= thing was to keep the fields filled out if the email field failed validation.
Alright, getting better. But what about all the other data in the form fields that aren’t email. Like names, job titles, manager names, check boxes, radio buttons…? Those all get reset. Do I have a script like the above for all form fields?
Thanks for all that. They way you laid out makes more sense though. The $_POST isn’t defined when the form is first loaded.
I’m a little disappointed that they left all of that out of the lesson. Seems like it should be included.
The code works, but still getting an arror at point 2.
<form method="post" action="">
Enter a word that starts with the letter "a":
<br>
<input type="text" name="a-word" id="a-word" value="<?= $_POST["a-word"]; ?>">
<br>
<p class="error" id="a-error">
<?= checkWord($_POST["a-word"],"a"); ?>
</p>
<br>
Enter a word that starts with the letter "b":
<br>
<input type="text" id="b-word" name="b-word"
value="<?=$_POST["b-word"]; ?>">
<br>
<p class="error" id="b-error">
<?= checkWord($_POST["b-word"],"b"); ?>
</p>
<br>
Enter a word that starts with the letter "c":
<br>
<input type="text" id="c-word" name="c-word"
value="<?=$_POST["c-word"]; ?>">
<br>
<p class="error" id="c-error">
<?=checkWord($_POST["c-word"],"c"); ?>
</p>
Thank you. That’s why I stopped using Codecademy some month ago. It’s so annoying. They should check results and not formatting. Anyway, its still my favorite site to learn new languages…
function checkWord($inp,$abc){
if( ($inp[0]) != $abc){
return "* This word must start with the letter abc!";
} else {
return "";
}
}
So in my above code I was given response that says my code return '* This word must start with the letter z!'
I can understand if the correct answer to the exercise requires ( && _SERVER[“REQUEST_METHOD”] === “POST” )
but how is it relevant to the output of this function ?
Something that has annoyed me to no end is that they teach you about the ternary operator, which is beautifully simple and efficient, and then they NEVER LET YOU USE IT AGAIN. Would it be possible to change the grader so that it would allow the ternary operator?
Hello, I’ve noticed that despite entering the words that starts with a, b and c letters, an error always occur “* This word must start with the letter a/b/c!”. I’ve passed the exercise, but no matter if word begins with correct or incorrect letter, error is always displayed. Has anoyone the same problem? I’m curious if the problem is in my code or the platform?
<?php
// Define checkWord() here:
function checkWord($input, $letter){
if ($_SERVER["REQUEST_METHOD"] === "POST" && strtolower($input[0]) !== $letter) {
return "* This word must start with the letter ${letter}!";
} else {
return "";
}
}
?>
<h1>Time to Practice our ABCs</h1>
<form method="post" action="">
Enter a word that starts with the letter "a":
<br>
<input type="text" name="a-word" id="a-word" value="<?=$_POST["a-word"] ?? '' ?>">
<br>
<p id="a-error"><?= checkWord($_POST["name"], "a")?></p>
<br>
Enter a word that starts with the letter "b":
<br>
<input type="text" id="b-word" name="b-word" value="<?=$_POST["b-word"] ?? '' ?>">
<br>
<p id="b-error"><?= checkWord($_POST["name"], "b")?></p>
<br>
Enter a word that starts with the letter "c":
<br>
<input type="text" id="c-word" name="c-word" value="<?=$_POST["c-word"] ?? '' ?>">
<br>
<p id="c-error"><?= checkWord($_POST["name"], "c")?></p>
<br>
<input type="submit" value="Submit Words">
</form>
<div>
<h3>"a" is for: <?= $_POST["a-word"];?><h3>
<h3>"b" is for: <?= $_POST["b-word"];?><h3>
<h3>"c" is for: <?= $_POST["c-word"];?><h3>
<div>
with the id’s (a-error, b-error, c-error) it looks like it is missing the class error. Just noticed this on your code. Not sure if that would help. Have a safe day!