Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ashley Crossland
10,868 PointsForm submission to echo differently depending on user inputs.
Hello,
I have a simple form with just one text input and a submit button:
<form method="POST" action="results.php">
<label for="results_submit_code">Type in your code here:</label>
<input type="text" name="results_submit_code" value="?????">
<input type="submit" value="Submit" class="results_submit_button">
</form>
So ideally, what I want to happen is for the user to type in a unique code which they are given e.g. 0000, and then the form to echo out a message linked with 0000. Another user might have the code 1111. Using the same form, a different message would be echoed after inserting 1111.
I am thinking of making an array e.g.
<?php
$code = array();
$code [0000] = "message for 0000";
$code[1111] = "message for 1111";
?>
So I'm thinking maybe I put the array into the value? And using the if/else statements to say if 0000 is entered echo 0000? Not sure where to start really, all help much appreciated!
3 Answers
Scott Evans
4,236 Points<?php
if(isset($_POST['results_submit_code'])){
$code = array(0000 => "0000 Message", 1111 => "1111 Message");
$result = $code[intval($_POST['results_submit_code'])];
if($result != null){
echo $result;
}else{
echo "No meesage for this code";
}
}
?>
<form method="POST" action="results.php">
<label for="results_submit_code">Type in your code here:</label>
<input type="text" name="results_submit_code" placeholder="?????">
<input type="submit" value="Submit" class="results_submit_button">
</form>
Give that a try :D
Ashley Crossland
10,868 PointsLegend, thanks Scott, didn't expect to get a working result, especially not that quickly, I owe you!
Scott Evans
4,236 PointsAnytime dude!