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

PHP

Form validation not working on Safari, but it does work on Chrome?

Form validation not working on Safari, but it does work on Chrome? Can someone please explain why form validation doesn't work on certain browsers? the message goes through even though the @ sign is missing. I do get an alert on Chrome.

Is there a solution to this?

Here it is Sergey :

https://w.trhou.se/6ommndm7p8

// ------------------------------ index.php

<?php

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('America/New_York');

//use Monolog\Logger;
//use Monolog\Handler\StreamHandler;
//
//$log = new Logger('name');
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
//
//$log->addWarning('Foo');

$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));

$view = $app->view();
$view->parseOptions = array(
    'debug' => true
);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get('/', function () use($app){
  $app->render('about.twig');
})->name('home');

$app->get('/contact', function () use($app){
  $app->render('contact.twig');
})->name('contact');

$app->post('/contact', function () use($app){
  var_dump($app->request->post());
});

$app->run();

// ------------------------------------------------------- contact. twig

{% extends 'main.twig' %}

{% block content %}
      <strong>Contact</strong>
      <h2>Ralph Waldo Emerson</h2>

      <p>Unfortately, Ralph Waldo Emerson has been deceased for over 100 years so he's not particularly expediant at replying to email but you can make an attempt below. However, if you require face to gravestone contact you can visit his remains at:</p>
      <address>
        <h4>Sleepy Hollow Cemetery</h4>
        <p>34 Bedford Street<br>
        Concord, MA 01742, United States<br>
      <a href="https://www.google.com/maps/place/Sleepy+Hollow+Cemetery/@42.464126,-71.343098,15z/data=!4m2!3m1!1s0x0:0x9c41d0f83df689a6?sa=X&ei=ZCgLVZb5Io_hoASc7oHwCQ&ved=0CH0Q_BIwCw">Google Map</a></p>
      </address>

      <form action="" method="post">
        <fieldset>
          <input name="name" type="text" placeholder="Full Name">
          <input name="email" type="email" placeholder="Email Address" required>
          <textarea name="msg" placeholder="Your message..."></textarea>
        </fieldset>
        <input type="submit" class="button">
      </form>
 {% endblock content %}

// ------------------------------- main.twig

<!doctype html>

<html lang="en">
<head>
  {% block head %}
    <meta charset="utf-8">
    <title>{% block title %}Ralph Waldo Emerson {% endblock title %}</title>
    <meta name="description" content="Ralph Waldo Emerson">
    <meta name="author" content="Treehouse">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/master.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="js/global.js"></script>
  {% endblock head %}
</head>

<body>
<!--
  <div id="feedback" class="success">
    <h3>Success!</h3>
    <p>You're reading all about Emerson.</p>
  </div>
-->


  <header>
    <h1>Ralph Waldo Emerson</h1>
    <nav>
      <a href="{{ baseUrl() }}" class="selected">About</a>
      <a href="{{ siteUrl('/contact') }}">Contact</a>
    </nav>
  </header>

  <div class="emerson">
    {% block hero %}<img src="images/emerson.jpg" alt="Picture of Ralph Waldo Emerson">{% endblock hero %}
  </div>

  <main>
   {% block content %}
   {% endblock content %} 
  </main>

  <footer>
    {% block footer %}
      <p>A project from <strong><a href="http://teamtreehouse.com">Treehouse</a></strong></p>
      <nav>
        <a href="{{ baseUrl() }}" class="selected">About</a>
        <a href="{{ siteUrl('/contact') }}">Contact</a>
      </nav>
    {% endblock footer %}
  </footer>

</body>
</html>

2 Answers

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

It looks fine, except zou did not provide path in form:

<form action="" method="post">

As I see from your code, you are expecting data at /contact:

$app->post('/contact', function () use($app){
  var_dump($app->request->post());
});

So you should provide this path in action attribute

<form action="/contact" method="post">

Thanks, Sergey!