- 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.
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:
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:
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
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']);
?>
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