PHP Image Rotator and Contact Form

Messages
245
Name
Matt
Edit My Images
Yes
Hi,

I have decided to compile a couple of useful PHP scripts which are frequently asked for/useful. All the code here is written by me, and works the way I wanted at the time of writing the code. All of the code here is tested, however, if you have any problems, feel free to PM me or post in this thread for help.

Image Changer
This code refreshes an image every time the page is reloaded. It counts the number of files in a directory, then uses that image to create a random number. Images must be named in numerical order, e.g. 1.jpg, 2.jpg, 3.jpg etc. Directory and file extension can be changed by changing the commented variables.

PHP:
<?php 
$dir = "images/"; //change to the images directory 
$ext = ".jpg"; //change to file extension 
$count = count(glob($dir.'*')) -1; 
$i = rand(1, $count); 

echo "<img src=\"".$dir.$i.$ext."\" alt=\"\" />"; 
?>

Advanced Contact Form
This is a slightly more advanced contact form from the bog standard. This includes error checking (null fields), with error reporting and a process to return the original form input back to the form after error checking, so the user does not have to re-type their inputted details.

Front-end with my fields that I build the contact form with:
HTML:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Us</title>
</head>

<body>
<?php
if($_GET['mode'] == "complete") {
?>
<h1>Thank you</h1>
Thank you for contacting us! We aim to respond to all queries within 48 hours.
<?php
} else {
?>
<h1>Contact Form</h1>
Fields marked with <span class="highlight_red">*</span> are required.
<?php
switch ($_GET['type']) {
case "fields":
echo "<br /><span class=\"highlight_red\">Please fill in all fields!</span>";
break;
default:
echo "";
}
?>
<form name="contact" method="post" action="back/contact.php">
Name <span class="highlight_red">*</span>: <br />
<input type="text" name="name" id="name" value="<?php echo $_SESSION['name']; ?>" />
<br />Email <span class="highlight_red">*</span>: <br />
<input type="text" name="email" id="email" value="<?php echo $_SESSION['email']; ?>" />
<br />Phone Number: <br />
<input type="text" name="phone" id="phone" value="<?php echo $_SESSION['phone']; ?>" />
<br />Enquiry Type <span class="highlight_red">*</span>: <br />
<select name="enq" id="enq">
<option selected="selected" value="0">Please Select...</option>
<option value="1">General Enquiry</option>
<option value="2">Sales Enquiry</option>
<option value="3">Other</option>
</select>
<br />Other Information/message <span class="highlight_red">*</span>: <br />
<textarea name="message" id="message" cols="45" rows="5"><?php echo $_SESSION['message']; ?></textarea>
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
<?php
}
?>
</body>
</html>
<?php
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['phone']);
unset($_SESSION['enq']);
unset($_SESSION['message']);
?>
Sorry for lack of indented code on the above HTML, I cut that up from my website and couldn't be bothered to re add the indentation. There are some style classes within that that will not work, I aplogise for that.

PHP Back-end:
PHP:
<?php
session_start();

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$enqS = $_POST['enq'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$time = date('r');

$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
$_SESSION['phone'] = $phone;
$_SESSION['enq'] = $enqS;
$_SESSION['message'] = $message;


if($name == NULL || $email == NULL || $enqS == "0" || $message == NULL) {
	header("Location: ../contact.php?mode=error&type=fields&sess=set");
} else {

	switch ($enqS) {
		case "1":
			$enq = "General Enquiry";
		break;
		case "2":
			$enq = "Sales Enquiry";
		break;
		case "3";
			$enq = "Other";
		break;
		default:
			$enq = "Unknown";
	}

	if($phone == NULL) {
		$phone = "None given.";
	}

	$to = "contact@example.com";
	$subject = "Example Website Form Submission - ".$enq;
	$mes = "Example Website Contact Form Submission:\n
		Sent: ".$time."\r
		IP: ".$ip."\r
		Name: ".$name."\r
		Email: ".$email."\r
		Phone: ".$phone."\r
		Enquiry Type: ".$enq."\r
		Message: ".$message."\n
		Do not reply directly to this email - reply to ".$email;
	$from = "form@example.com";
	$head = "From: $from";
	$mail = mail($to, $subject, $mes, $head);

	if($mail) {
		header("Location: ../contact.php?mode=complete");
	} else {
		header("Location: ../contact.php?mode=error&type=mail&sess=set");
	}
}

?>

I hope those scripts were useful to someone. Again, if something doesn't work, or you need help, please contact me.

I wrote these scripts so please do not claim them as your own.

Cheers,
Matt
 
In the form why are you assigning variables to the post data and then session to variable.. why not directly session to post data and use that in your code..

also in the form its POST but in the code it looks for GET

to be honest i cant see a good reason for using session for anything anyway.. just use the POST data :)
 
In the form why are you assigning variables to the post data and then session to variable.. why not directly session to post data and use that in your code..

also in the form its POST but in the code it looks for GET

to be honest i cant see a good reason for using session for anything anyway.. just use the POST data :)
I am assigning POST data to a variable to allow me to use the variables easier, without having to type out the whole of the super global, and then I am SETTING the sessions to the variable value (there is not point typing out the POST again when I have a shorter variable there to use).

The form processing script is looking for POST, not GET, I think you are looking at the wrong thing there.

The session thing is to take the data from the processing script back to the original form when an error occurs. You cannot do this with POST data as you cannot (correct me if I am wrong) create a HTTP POST from PHP.
 
When making a form with PHP I find its best to use one page containing form, thank you and error... this flow doesnt need sessions is cleaner and less code IMHO

show the form if POST false or error true...

show thank you message if POST true and error false

have the POST variables inside the form input field values then if first time the field will be blank (cus POST is empty) but if showing because of errors then they wil be populated with the POST data..



Hows that? :)
 
When making a form with PHP I find its best to use one page containing form, thank you and error... this flow doesnt need sessions is cleaner and less code IMHO

show the form if POST false or error true...

show thank you message if POST true and error false

have the POST variables inside the form input field values then if first time the field will be blank (cus POST is empty) but if showing because of errors then they wil be populated with the POST data..



Hows that? :)
Yeah, I totally agree with you, I normally do forms like that, however this was "stuck on" my original site, and I deemed it easier for me to just use an external processor.

Thanks for the feedback :)
 
Yeah, I totally agree with you, I normally do forms like that, however this was "stuck on" my original site, and I deemed it easier for me to just use an external processor.

Thanks for the feedback :)

I see code.. I tinker.... sorry :)
 
Thanks mate :) i have no idea how to use contact forms but i will use the image changer thing. Thanksss :)
 
I was looking to implement both of these features on a new site I'm doing, so will look back at these. Thank you!
 
Back
Top