PDA

View Full Version : Simple PHP Setup Question - Echo Function?


kremo5le
06-15-2012, 01:52 AM
Hello guys,

I am starting to learn some simple PHP from scratch. I have worked for a number of years with already-written PHP and am able to change things here and there but I am not a PHP programmer. However, I want to learn some basics.

I have gone online, including the PHP website, but I am just super confused and all I am looking for to do is perhaps the most common use for PHP (along with forms, that is).

THE SITE

So I am working on a responsive site: http://m.eelio.net/. All I want to learn to do is to create PHP files that are called by the main template.

For example, I want to create a "follow_me.php" file that is called by the template. Same goes with the footer, menu, etc.

I tried the echo function, which I believe is all I need, but I was unsuccessful. I am not sure if the file "follow_me.php" should have JUST the HTML or if it should be wrapped around <?php > tags. I am not sure exactly what to write within the index.php file neither.

Ideally, I would like to keep the php files in a folder and not in the root directory (I am an organization freak, so perhaps .../php/... and robots.txt it out). But maybe there are better practices?

THANKS! :smile:

edbr
06-15-2012, 02:01 AM
you need to use include this will include your sections.
ie if you have a file called header.inc (or text or htm etc) it will include the contents as is so if header.inc contains

<div>
<p>This is my header</p>
</div>

you can in your web page use
<?php include("folder/header.inc") ;?>
and that will show the contents of that file

davidj
06-15-2012, 09:31 AM
Quick best practice note.

Use a .php extension in all php files especially when including into another file.

If a user tries to target and download a .php file then that file will be handed over and processed through php this means that what is returned to the requester is a file void of any code. If however you target an .inc file the browser will prompt to download the file in all its glory as it wont know how to render the inc file directly. Imagine having a connect.inc file which contained your database connection block which was targeted and downloaded!

kremo5le
06-20-2012, 07:07 AM
That was easy! Thank you both.

I really don't know what a .inc file is so all my files are .php. :razz:

edbr
06-20-2012, 07:48 AM
no prob, old format used for non important text that will be read on web anyway. could be anything as I said including php of course

kremo5le
06-20-2012, 05:54 PM
no prob, old format used for non important text that will be read on web anyway. could be anything as I said including php of course

It is all good. Old or new, you guys got me running and also interested in learning more about PHP.

NEW QUESTION

I now created a form based on a tutorial yet I stripped everything I was not interested in. http://m.eelio.net/talk.php. For example, the captcha was really simple with just a single answer (what is 2+2?). I would like to implement a more advanced captcha system: what do you recommend?

I would also want to learn how to make certain fields required. All in PHP. :razz:

edbr
06-21-2012, 02:11 AM
recaptcha is popular but there are many freebie scripts on the web yahoogle away
here is a tut that explains the nuts and bolts http://net.tutsplus.com/tutorials/php/build-your-own-captcha-and-contact-form/
or here http://frikk.tk

as for required it a matter of checking if the field is empty or not
simple example
if ($email == "") {
echo "<h4>You must enter an e mail address</h4>";

}

kremo5le
06-25-2012, 06:32 PM
as for required it a matter of checking if the field is empty or not
simple example
if ($email == "") {
echo "<h4>You must enter an e mail address</h4>";

}

I think I will do reCAPTCHA. Thanks!

Regarding the empty field code, I added this but it did not work as it continued showing that new line added constantly and I still could send emails. :O/


<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: my website';
$to = 'my@website.net';
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<h4>Your message has been sent!</h4>';
} else {
echo '<h4>Something went wrong, go back and try again!</h4>';
}
}

if ($email == "") {
echo "<h4>You must enter an e mail address</h4>";

}

?>

edbr
06-26-2012, 02:03 AM
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: my website';
$to = 'my@website.net';
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($email == "") {
echo "<h4>You must enter an e mail address</h4>";

}
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<h4>Your message has been sent!</h4>';
} else {
echo '<h4>Something went wrong, go back and try again!</h4>';
}
}



?>

kremo5le
06-28-2012, 04:41 AM
Now it always shows:

"You must enter an e-mail address"

http://m.eelio.net/talk.php

CAPTCHA

I also added the script to show reCAPTCHA as you see. However, I am not even sure what to do next (which is to hook up the validation) based on these instructions: https://developers.google.com/recaptcha/docs/verify

By the way, thanks for your help! :-D