CiTi · Computer Coding · Year One · Unit 3

True or False: Making Pages Decide with showif & hideif

Student Walkthrough

The Big Idea

So far every page you built shows the same thing to everyone. Today your pages learn to decide. Every decision starts with a question that has only two answers, true or false. That kind of question is called a boolean.

1. AskIs the visitor logged in? Did this visit come from my form?
2. AnswerThe server checks a variable: true or false.
3. Decide[showif] or [hideif] picks what goes on the page.
4. SendOnly the winning part is sent. The rest never leaves the server.

First you practice in a Logic Lab page. Then you fix the problem from the contact form lesson: send.html sends you an email every time anyone loads it.

Part A: Watch

Video 1What is a boolean?about 1 min
Key points from this video
  • A boolean is a question with two answers: true or false.
  • WebDNA stores the answer in a variable as yes or no.
  • Name it for the question and end it in var: loggedinvar.
Video 2showif and hideifabout 1 min
Key points from this video
  • [showif] puts its content on the page when the question is true.
  • [hideif] is the mirror: it leaves its content out when the question is true.
  • Variable, operator, value. No spaces, no quotes.
In your own words: what is a boolean? Give one example from a website you use.

Part B: Build the Logic Lab

  1. Make the lab page. In CyberDuck, inside mysite, make a folder named logic with a New File named index.html. Open it with Sublime. This is a practice page: it does not go in your nav.

  2. Paste the whole lab. Your path is already filled in if you typed it above.

[include file=/citi/coding/2027/1/YOURINTS/mysite/includes/vars.inc] [text]titlevar=Logic Lab[/text] [include file=[incvar]header.inc] [include file=[incvar]nav.inc] <div class="bodycopy"> <div class="col-right"> <h2>Logic Lab</h2> [!] The answers. Change these, save, refresh. [/!] [text]loggedinvar=yes[/text] [text]scorevar=82[/text] [text]namevar=Jordan Diaz[/text] [!] 1. showif and its mirror, hideif [/!] [showif [loggedinvar]=yes]<p>Welcome back!</p>[/showif] [hideif [loggedinvar]=yes]<p>Please log in.</p>[/hideif] [!] 2. Numbers [/!] [showif [scorevar]>67]<p>You passed.</p>[/showif] [hideif [scorevar]>67]<p>Keep practicing.</p>[/hideif] [!] 3. Contains [/!] [showif [namevar]^Diaz]<p>A Diaz is here.</p>[/showif] [!] 4. Two questions: nest them [/!] [showif [loggedinvar]=yes] [showif [scorevar]>67]<p>Logged in AND passed.</p>[/showif] [/showif] </div> </div> [include file=[incvar]footer.inc]
  1. Save and open it in your browser. Go to mysite/logic/ on your live site. You should see Welcome back!, You passed., A Diaz is here., and Logged in AND passed.

  2. Read the anatomy. Every question has three parts and nothing else: the variable in brackets, an operator, a value. [showif [loggedinvar]=yes]. No spaces. No quotes.

  3. Flip it. Change loggedinvar=yes to loggedinvar=no. Predict what changes, then save and refresh. Do the same with the score: try 90, 50, and 68.

  4. View the source. Right-click the page and choose View Page Source. The losing messages are not hidden. They are not there at all. The server never sent them.

With loggedinvar=no, which messages showed? Why did Logged in AND passed disappear even though the score is still high?

The operators

OpMeansExampleTrue when
=equals[showif [loggedinvar]=yes]loggedinvar is yes
!is not equal to[showif [loggedinvar]!yes]loggedinvar is anything but yes
>greater than (numbers)[showif [scorevar]>67]68 and up
<less than (numbers)[showif [scorevar]<67]66 and down
^contains (text)[showif [namevar]^Diaz]Diaz anywhere in namevar

Text comparisons ignore capital letters: YES matches yes. > and < compare numbers.

Mirror pairs. A [showif] and a [hideif] with the same question always show exactly one of the two. That is how you say “otherwise” for now.
Nest for “and.” Each tag asks one question. To ask two, put one [showif] inside another, and indent the inside one.

Part C: Break It On Purpose

Video 3Comparing thingsabout 1 min
Key points from this video
  • = equals, ! not equal, > greater than, < less than, ^ contains.
  • At exactly 67, both >67 and <67 are false. Use the mirror [hideif] for the other side.
  • One question per tag. Nest two tags to mean and.

One change at a time. Predict first, test, then undo it before the next one. Do at least 1, 2, and 5.

1. Change the Keep practicing line to [showif [scorevar]<67]<p>Keep practicing.</p>[/showif] and set the score to exactly 67

I predict
What happened
Why

2. Put spaces around the equals sign: [showif [loggedinvar] = yes]

I predict
What happened
Why

3. Put quotes around the value: [showif [loggedinvar]="yes"]

I predict
What happened
Why

4. Try to combine two questions with &: [showif [loggedinvar]=yes&[scorevar]>99] (the score is not over 99)

I predict
What happened
Why

5. Try an else: [showif [loggedinvar]=yes]Hi[else]Bye[/showif]

I predict
What happened
Why

6. Delete one [/showif]

I predict
What happened
Why

Part D: Guard Your send.html

Video 4The page-load problemabout 1 min
Key points from this video
  • [sendmail] runs every time send.html loads.
  • A direct visit sends an email full of names in brackets.
  • A variable that was never sent prints its own name.
Video 5The hidden field guardabout 1 min
Key points from this video
  • The form always sends c_send=yes. A direct visit sends nothing.
  • Wrap [sendmail] in [showif [c_send]=yes]. Content in a false showif never runs.
  • It is a guard, not a lock.
  1. Check your form for the hidden field. Open contact/index.html. Right above the Send button there should be this line. If it is missing, add it now.

<input type="hidden" name="c_send" value="yes">
  1. Update send.html. Open contact/send.html. Make it match this. Two changes: [showif [c_send]=yes] wraps the whole [sendmail], and the message is now a mirror pair, Thanks or Oops. Keep your own image name.

[include file=/citi/coding/2027/1/YOURINTS/mysite/includes/vars.inc] [text]titlevar=Message Sent[/text] [!] Only send when the form said so. A direct visit has no c_send. [/!] [showif [c_send]=yes] [sendmail to=[emailvar]&from=[fromvar]&subject=MySite Contact Form] New message from the MySite contact form. Name: [c_name] Email: [c_email] Phone: [c_phone] Type: [c_type] Comments: [c_comments] -- Sent [date format=%B %d, %Y] at [time format=%I:%M %p] from IP [ipaddress] [/sendmail] [/showif] [include file=[incvar]header.inc] [include file=[incvar]nav.inc] <div class="bodycopy"> <div class="col-left"> <img src="[imgvar]mail.jpg" alt="an envelope"> </div> <div class="col-right"> [showif [c_send]=yes] <h2>Thanks!</h2> <p>Your message was sent. I will get back to you soon.</p> <p><a href="[pathvar]">Back to the home page</a></p> [/showif] [hideif [c_send]=yes] <h2>Oops</h2> <p>This page only works from the contact form.</p> <p><a href="[pathvar]contact/">Go to the contact form</a></p> [/hideif] </div> </div> [include file=[incvar]footer.inc]
  1. Test a direct visit. Type the address of your send.html straight into the address bar. You should see Oops, and no email should arrive.

  2. Test the real form, once. Fill in your contact form and send it. You should see Thanks, and the email should arrive. One real test is enough.

Why does a direct visit to send.html fail the test [c_send]=yes? What does [c_send] print on a direct visit?
A guard, not a lock. Variables can come from the address bar too: send.html?c_send=yes gets past it. The guard stops page loads, links, and bots, not someone who reads your form’s source.
Watch out: blank answers still send. The guard only checks where the visit came from, not whether the answers are any good. Someone can remove required in the browser’s inspector, or type only spaces, and the form still sends. Checking each answer on the server comes later.

Finish early or at homePart E: Empty or Never Sent?

Video 6Common uses, and what comes nextabout 1 min
Key points from this video
  • Thanks or Oops, one drop-down choice, welcome messages.
  • Empty check: start it empty, or compare it to its [raw] name.
  • Next: [if] [then] [else]. Text needs quotes.

A variable that was never sent prints its own name in brackets, like [c_name]. That means it is not empty, so [showif [c_name]!] (“not empty”) is true even on a direct visit. There are two safe ways to check:

1. Start it empty

When nothing else can set the variable, set it to nothing first. Then = with nothing after it is a real empty test.

[text]notevar=[/text] [showif [notevar]=]<p>No note yet.</p>[/showif]
2. Compare it to its own name

A form variable you should not wipe out? Check if it was ever declared. It matches its [raw] self only when it was never sent.

[showif [c_name]=[raw][c_name][/raw]] <p>No name was sent.</p> [/showif]
  1. Try way 1 in your Logic Lab. Add the notevar lines. Refresh: No note yet. shows. Change it to [text]notevar=hello[/text] and refresh.

  2. Try way 2 in your Logic Lab. Add the c_name check. Open the lab normally: No name was sent. Then add ?c_name=Sam to the end of the address and press Enter.

Where else on MySite could a page decide something? Name one place and the question it would ask.
Coming next: [if]. It holds both answers in one tag. Its rules are different: text needs quotes, or it quietly picks the else answer.
[if "[loggedinvar]"="yes"] [then]<p>Welcome back!</p>[/then] [else]<p>Please log in.</p>[/else] [/if]

showif Rules to Remember

Success Criteria Check-In

Check each one when you feel confident you can do it.

Exit Ticket

Your score test is [showif [scorevar]>67]. Write the tag that shows Keep practicing for every other score. Why is [showif [scorevar]<67] wrong? Why would [showif [c_send]!] NOT protect send.html?

My Notes

Use this space for anything: questions, reminders, code you want to keep.