2

Here is my code:

    <html>
      <head>
        <title>Vowel Checker</title>
      </head>
    <body>
      <h1>Is It A Vowel?</h1>
      <form action="Vowels.php" method="post">
        <p>Your Letter:</p><input type="text" name="letter">
        <input type="submit">
      </form>
      <?php
        $l = $_POST['letter'];
        $l = strtolower($l);
        if ($l != ""){
          switch ($l) {
            case "a":
            case "e":
            case "i":
            case "o":
            case "u":
              echo '<h1 style="color: green;">Your letter is a vowel</h1>';
              break;
            case "y":
              echo '<h1 style="color: orange;">Your letter is sometimes a vowel</h1>';
              break;
            default:
              echo '<h1 style="color: red;">Your letter is not a vowel</h1>';
              break;
          } 
        }
      ?>
    </body>
    </html>

If you use http://www.compileonline.com/execute_php_online.php and set $l equal to a letter (since my form submission doesn't work with online compilers) to test it out, it works properly. But I've tired JSfiddle, WAMP, and XAMPP, and it hasn't worked. I made the code on a mac using sublime text and MAMP.

This is how the program runs from WAMP/XAMPP in Google Chrome (I've also tried Firefox and IE):

https://i.sstatic.net/zzPHh.png

And if you look in inspect element, you see it has added a comment in the opening php tag:

https://i.sstatic.net/fENC7.png

I find this very strange it this error is impeding my school work =[ so id really love some help.

thank!!!

2
  • 1
    Notice the URL? You're not invoking it through the server, you're actually opening the file directly. You want to do something like localhost/index.php or 127.0.0.1/index.php (which is the loopback IP address) Commented Aug 15, 2014 at 12:07
  • Thank you very much my friend I have it running now! Perhaps you can see my comment on Spork's answer? I have a small error. Commented Aug 15, 2014 at 12:16

2 Answers 2

2

By looking at your screenshot https://i.sstatic.net/zzPHh.png . Your path in the browser is not correct. go to "localhost" in your browser and then navigate to your project folder. "localhost/YOUR_PROJECT_DIRECTORY/Vowels.php"

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i have it going now thank you for your time! lovely for you to help
1

Juding from https://i.sstatic.net/zzPHh.png, you are opening the PHP file directly in a browser.

What's going wrong here is that you are not accessing a web server (in your XAMPP case this is Apache) and therefore no PHP interpreter is being called. What you end up seeing is what shows up as a browser that interprets a text file (parsing HTML from it, and not recognizing the PHP, nor interpreting it as code).

Access this file through your http://localhost/ . Go from there. If you have no webserver, read up on webservers first.

5 Comments

OMG i feel so stupid haha yes i got this to work almost straight away!
although the program works how i intend, i still get this error (on internet explorer) i.imgur.com/tA1v0M2.png this is line 18: $l = $_POST['letter'];
@user2756012 It's because on first load, $_POST is empty because you haven't posted any data to it. You should only attempt to grab something from $_POST if you know its there (i.e. after the form is submitted).
okay that makes sense i guess. sorry if you already kind of explained this but how can I avoid this error? thanks
Don't read from $_POST until something has been POSTed. Try having a go at it, google around a bit about POST, the community here will be able to help you if you get stuck on document a question like you did here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.