Hey Everyone,
Post your code for the Feb 24 "Let's Build Something here".
Remember if you precede each line of your code by at least one white space character it will be output preformatted in your post.
Best, Chris
<?php
function messageController() {
data = _GET["textbox"];
if (data)
seenWordsView(explode(" ", data));
else
formView($data);
}
function formView($data) { # Display form ?>Enter your message here: <form method="get" action="message.php"> <input type="text" name="textbox" size="10" /> <input type="submit" name="sendform" value="Don't Panic!" /> </form><?php }
function seenWordsView($data) { # Heading ?><h1>Words Seen</h1><?php
foreach ($data as $word)
echo "<div>$word</div>";
echo "<br><br><a href=\"message.php?\">Back</a>";
}
messageController();
?>
(Edited: 2016-02-24)<?php
/*
*/
/**
*/
function messageController()
{
data=_GET;
if (data)
{
seenWordsView(data);
} else {
formView($data);
}
}
/**
*/ function formView($data) { ?> <form method=get action=message.php> <label for='message_label'>Enter your message here:</label> <input type='text' name='viewdata' size='20'/> <input type='submit' value='Submit'/> </form>
<?php
}
/**
*/
function seenWordsView(data)
{
?>
<h1>Words Seen</h1>
<?php foreach (data as output) {
echo "<div>output<div>";
}
?>
<a href="./message.php">Link Back</a>
<?php
}
//Call view controller
messageController();
(Edited: 2016-02-24)<?php //Tyler Jones : CS174 : Feb 24
// control which view based on if data is true
function messageController()
{
data = _GET;
if (data) {
seenWordsView(data);
} else {
formView($data);
}
}
// no data entered yet --> output form to get the data
function formView($data) { ?> <form method="get" action="message.php"> <label> Enter your message here: </label> <input type="text" name="textbox" size="10" /> <input type="submit" value="Submit"/> </form> <?php }
// show the words that were entered into the form (each in own div tag)
function seenWordsView(data)
{
?>
<h1> Words Seen </h1>
<div> The words you entered are: </div>
<?php
foreach (data as seenWord) {
echo "<div>";
echo seenWord;
echo "<div>";
}
?>
<br><a href="./message.php">Click</a> to submit again
<?php
}
messageController();
(Edited: 2016-02-24)@dennis and @tyler. Your code needs to somewhere split the text field data from the form into words. As it stands now it is cycling over the $_GET fields (not words) on the page. I just updated that slide because I had been vague and send output all the words submitted on the page. I meant all the words in the text field data.
<?php
function messageController()
{
data = _GET;
formView(data);
if (data != NULL) {
ob_clean(); // clears the formView to prepare the page for the seenWordsView
output = str_replace(" ", "</div><div>", data["message_text"]);
seenWordsView(output);
}
}
function formView(data)
{
?>
<form method="get" action="message.php">
<label for="message_text">Enter your message here:</label>
<input type="text" name="message_text" size="10" />
<input type="submit"/>
</form>
<?php
}
function seenWordsView(data)
{
?>
<head>Words Seen</head>
<?php
echo "<div>data</div>"
?>
<a href="message.php">Back to form</a>
<?php
}
messageController();
Hi Alejandro,
Best, Chris
(Edited: 2016-02-25)<?php function messageController() {
data=(_GET);
if(data)
{
seenWordsView(data);
}
else
{
formview($data);
}
}
function formView(data)
{
// data has any data set up from the controller
//outputs the message form
?>
<form method="get" action="message.php">
<label for="input_field">Enter the text</label>
<input type="text" name ="input_field" size="25" />
<input type="submit" name="submit" value="Submit form"/>
</form>
<?php
}
function seenWordsView(data)
{
//outputs seen words, allowed a foreach loop and to
//echo variables. Not allowed to call preg_ functions or explode
?>
<h1>words that are seen in the textfield</h1>
<?php
foreach(data as h)
echo "<div>h</div>";
?>
<a href="./message.php">link back</a>
<?php
}
messageController();
?>
Hi Sarika,
The "for" attribute of a label needs to point to an "id" attribute of some target element not the "name" attribute. Your code doesn't output each word from the form submission in a different div, it outputs each field that was submitted instead.
Best, Chris
Here's mine:
<?php
function messageController() { //decide which view to use and set up data for output
if(empty($_POST['data'])){
formView($_POST[data]);
} else {
seenWordsView($_POST[data]);
}
}
function formView(data)
{
// data has any data set up from the controller
//outputs the message form
echo "<form method='POST' action='<?php echo $_SERVER[PHP_SELF];?>'><label for='input'>Enter your message here:<input type='text' id='input' name='data'><input type='submit' value='Submit'></form>";
}
function seenWordsView(data)
{
//outputs seen words, allowed a foreach loop and to
//echo variables. Not allowed to call preg_ functions or explode
words = explode(" ", data);
echo "<h1>Words Seen</h1>";
foreach (words as value) {
echo "<div>". value ."</div>";
}
echo "Click <a href='/message.php'>here</a> to go back to the form page ";
}
messageController();
?>
(Edited: 2016-02-26)