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

how can you set an on click function in php

Hey guys, I do like to know how can i set an on click function in PHP such that when i click on a picture on my home page it will direct me to another page on the same project (an include)

3 Answers

Julian Aramburu
Julian Aramburu
11,368 Points

Hi buddy! I think what you are trying to do here would be best done using just plain HTML or maybe JQuery depending on the effect that you are trying to achieve.

I sounds as though you just need to add a link in your php, very similar to how you would using HTML really :)

To link dynamically you put a php block as the href like below:

  <a href="<?php echo $link ?>">link</a>

You dont need to do this if you are hard coding the likes however. The above is only appropriate if the links are being generated.

Are you using WordPress or just PHP?

Craig

You guessed it right Craig. I'm still new and learning coding skills. I am using PHP. My problem is i am using composer and maybe i am having difficulties to include pages. I set my picture into a clickable link and if i hover above it it shows me the desired destination (page) but if i click on it it gives me the error 404 page not found my code is as below;

Index. php

<?php

require 'vendor/autoload.php'; date_default_timezone_set('Africa/Harare');

use Monolog\Logger; use Monolog\Handler\StreamHandler;

function phpAlert($msg_error) { echo '<script type="text/javascript">alert("' . $msg_error . '")</script>';

}

//$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->parserOptions = array( 'debug' => true ); $view->parserExtensions = array( new \Slim\Views\TwigExtension(), );

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

$app->get('/Resorts Sites in Afica', function() use($app){ $app->render('Resorts Sites in Afica.twig'); })->name('resorts');

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

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

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

})->name('search1');

$app->post('/contact', function() use($app){ $name= $app->request->post('name'); $age=$app->request->post('age'); $email=$app->request->post('email'); $expected_date=$app->request->post('expected_date'); $country_to_visit=$app->request->post('country_to_visit'); $reason_for_visit=$app->request->post('reason_for_visit'); $payment_method=$app->request->post('payment_method'); $msg=$app->request->post('msg');

if(!empty($name) && !empty($email) && !empty($msg)) { $cleanName = filter_var($name, FILTER_SANITIZE_STRING); $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL); $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING); } else { phpAlert( "All the fields are needed. Please fill all fields and send again" ); die(); $app->redirect('/contact');

}

$transport = Swift_MailTransport::newInstance('/usr/sbin/sendmail -bs'); $mailer = \Swift_Mailer::newInstance($transport);

$message= \Swift_Message::newInstance(); $message->setSubject('Email From Our Website'); $message->setFrom(array($cleanEmail=>$cleanName)); $message->setTo(array('selesgama@gmail.com')); $message->setBody($cleanMsg);

$result = $mailer->send($message); if($result > 0){

phpAlert(   "Thank you for contacting us. We will get to you soon."   ); 
die();
$app->redirect('/');

} else{ phpAlert( "There was an error sending the message. Try again later." ); die();

} $app->redirect('/contact'); });

$app->run();

?>

main.twig

<!DOCTYPE html> <html> <head> {% block head %} <meta charset="utf-8"> <title>{% block title %}HOLIDAY IN PARADISE | A Day in Africa{% endblock title %}</title> <link rel="stylesheet" href="css/normalize.css"> <link href='http://fonts.googleapis.com/css?family=Changa+One|Open+Sans:400,400italic,700,700italic,800' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/main.css"> <link rel="stylesheet" href="css/responsive.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="shortcut icon" href="<img/favicon.ico"> {% endblock head %} </head>

<header> <div id="page-logo">
<a <a href= "{{ baseUrl()}}" id="logo"> <h1>HOLIDAY IN PARADISE</h1> <h2>A Day in Africa</h2> </a> <nav> <li><a href= "{{ baseUrl()}}">Home</a></li> <li><a href="{{siteUrl('/search')}}">About</a></li> <li><a href="{{siteUrl('/contact')}}">Contact</a></li> <li><a href="{{siteUrl('/search1')}}">Search</a></li>

  </nav>
</header>
<div id="wrapper">

  {% block content %} 

   {% endblock content %}

  <footer>
    {% block footer %}
    <a href="http//:gmail.com/selesgama@gmail.com"><img src="img/mail.png" alt="Gmail Logo" class="social-icon"></a>
    <a href="http//:twitter.com/Selestino01"><img src="img/twitter.png" alt="Twitter Logo"class="social-icon"></a>
    <p>&copy; 2015 Resorts Africa.</p>
    {% endblock footer %}
  </footer>
</div>

</body> </html>

home.twig

{% extends 'main.twig' %}

    {% block content %}
    <section>
    <ul id="gallery">
      <li>
        <A HREF="Resorts Sites in Afica.twig"><IMG BORDER="0">
          <img src="img/victoria_falls3.jpg" alt="">
          <p>Resorts Sites in Afica.</p>
        </a>
      </li>
      <li>

    </ul>
  </section>
    {% endblock content %}

the page i want my site visitor to go if he/she click the above picture is the one below which i labeled Resorts Sites in Afica

{% extends 'main.twig' %}

    {% block content %}

<div class="resortsAfrica"> 
<ul id="resorts">
  <li>
    <div class="content-left"> 
    <a href="img/victoria_falls3.jpg">
    <img src="img/victoria_falls3.jpg" alt="">
    </a> 
    </div> 
    <div class="content-right"> 
    <p><b> The Victoria Falls<b></p>
    <p></p>
    </div> 
    </div> 
  </li>

  </ul>
  </div>
    {% endblock content %}

I have opened a template folder which i have put all my .twig files including the Resorts in Africa.twig

Hope this will make it easier to see where i am loosing it.

Thanks

Selestino.