PHP Help

Associate
Joined
28 Nov 2005
Posts
431
Location
Scotland
Having never done PHP before, I have managed to put together this basic script to handle the submittion of a form on my page:

Part of the script is to check whenther the email address is valid or not, if it is not valid, it alerts the user via a javascript alert box, however after doing so, the pages directs to the php script displaying a blank page - e.g. www.*domain*.co.uk/submit.php :( I dont want this to happen, I want it to stay on the original form page: www.*domain*.co.uk/enquiry.html.

I would also like, when the script displays the email is invalid, to clear the email input box on the page. Does anyone have any idea how I do this?


PHP
Code:
<?php
/* Set e-mail recipient */
$myemail  = "enquiries@*domain*.co.uk";

/* Check all form inputs using check_input function */
$regarding    = check_input($_POST['regarding']);
$firstname = check_input($_POST['firstname'], "Enter your first name");
$lastname = check_input($_POST['lastname'], "Enter your last name");
$companyname    = check_input($_POST['companyname']);
$address1  = check_input($_POST['address1']);
$address2  = check_input($_POST['address2']);
$city   = check_input($_POST['city']);
$region = check_input($_POST['region']);
$postcode   = check_input($_POST['postcode']);
$country = check_input($_POST['country']);
$email   = check_input($_POST['email']);
$number = check_input($_POST['number']);
$enquiry = check_input($_POST['enquiry']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{

	show_error("The E-mail address entered is not valid.");
	

}

$subject = "New $regarding Enquiry";

/* Let's prepare the message for the e-mail */
$message = "A enquiry form has been submitted:

Enquiry regarding $regarding

Name: $firstname $lastname

Company: $companyname

Address: 
$address1
$address2
$city
$region
$postcode
$country

E-mail: $email
Contact Number: $number

Enquiry:
$enquiry

";

$headers = "From: enquiryform@*domain*.co.uk";



/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
	echo "<script language=\"javascript\">alert('$myError');</script>";

	exit();
}
?>



HTML
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
	<script type="text/JavaScript" src="script.js"></script>
	<title>A-spec Solutions - Enquiry</title>
	<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>


	<div id="placeholder"><a href="index.html"><img src="gfx/name2.png" alt="A-spec Solutions" /></a></div>

	<div id ="menuback">
		<div id="menucontainer">
		
			<div class="split"></div>
			<div class="navlinks"><a href="index.html">Home</a></div>
			<div class="split"></div>
			<div class="navlinks"><a href="about.html">About</a></div>
			<div class="split"></div>
			<div class="navlinks"><a href="itsupport.html">IT Support</a></div>
			<div class="split"></div>
			<div class="navlinks"><a href="itsecurity.html">IT Security</a></div>
			<div class="split"></div>
			<div class="navlinks"><a href="ithome.html">Home IT</a></div>
			<div class="split"></div>
			<div class="navlinks"><a href="contact.html">Contact</a></div>
			<div class="split"></div>
		</div>
	</div>

	<div class="wrapper">
	
	<h2 class="headunderline">Enquery Form</h2>
	<br />
	<form action="submit.php" method="post">

	
		<table id="enq">
		<tr>
			<td>
		
				<div class="formtypelbl">
					<label for="regarding">Please select the service you are enquiring:</label>
				</div>
				<div>
					<select id="regarding" name="regarding">
						<option value="#" selected="selected"></option>
						<option value="support">IT Support</option>
						<option value="security">IT Security</option>
						<option value="home" >Home Support</option>
					</select>
				</div>
		
			</td>
		</tr>
		
		
		<tr>
			<td class="colour">
			<br /><br />
				<div class="formheader">Name</div><br />
				<div class="formlbl">
					<label for="firstname">First name:</label> 
				</div>
				<div class="formstandardinput">
					<input type="text" id="firstname" name="firstname" class="standard" />
				</div>
				<div class="formlbl">
					<label for="lastname">Last name:</label>
				</div>
				<div>
					<input type="text" id="lastname" name="lastname" class="standard" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td>
			<br /><br />
			<div class="formheader">Comapny Name (if applicable)</div><br />
				<div class="formlbl">
					<label for="companyname">Company name:</label>
				</div>
				<div>
					<input type="text" id="companyname" name="companyname" class="standard" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td class="colour">
			<br /><br />
				<div class="formheader">Address</div><br />
				<div class="formlbl">
					<label for="address1">Address line 1:</label>
				</div>
				<div>
					<input type="text" id="address1" name="address1" class="large" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td class="colour">
				<div class="formlbl">
					<label for="address2">Address line 2:</label>
				</div>
				<div>
					<input type="text" id="address2" name="address2" class="large" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td class="colour">
				<div class="formlbl">
					<label for="city">City:</label>
				</div>
				<div class="formstandardinput">
					<input type="text" id="city" name="city" class="standard" />
				</div>
				<div class="formlbl">
					<label for="region">Region:</label>
				</div>
				<div>
					<input type="text" id="region" name="region" class="standard" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td class="colour">
				<div class="formlbl">
					<label for="postcode">Postcode:</label>
				</div>
				<div class="formstandardinput">
					<input type="text" id="postcode" name="postcode" class="standard" />
				</div>
				<div class="formlbl">
					<label for="country">Country:</label>
				</div>
				<div>
					<input type="text" id="country" name="country" class="standard" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td>
			<br /><br />
				<div class="formheader">Contact Details</div><br />
				
				<div class="formlbl">
					<label for="email">Email:</label>
				</div>
				<div>
					<input type="text" id="email" name="email" class="standard" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td>
				<div class="formlbl">
					<label for="number">Contact number:</label>
				</div>
				<div>
					<input type="text" id="number" name="number" class="standard" />
				</div>
			</td>
		</tr>
		
		<tr>
			<td class="colour">
			<br /><br />
				<div>
					<label for="enquiry">Enquery:</label>
					<br />
					<textarea rows="10" cols="30" id="enquiry" name="enquiry" class="large"></textarea>
				</div>
			</td>
		</tr>
		
		<tr>
			<td>
				<div>
					<input type="submit" value="Submit" id="submit" />
				</div>
			</td>
		</tr>
		
	</table>

	</form>
		
	
	
	</div>
	<div id="footer">
		<p class="footercenter">&copy; 2010 Aspec Solutions, all rights reserved.<a href="http://www.linkedin.com"><img src="gfx/linkedin.png" alt="Linkedin" id="linkedin" class="fbimg"/></a><a href="http://www.facebook.com/#!/pages/A-spec-Solutions/167870849919537"><img src="gfx/facebook.png" alt="Facebook" id="facebook" class="fbimg"/></a></p>
	</div>
	
</body>

</html>
 
Last edited:
If I understand what you want to do then have the form itself on the actual PHP page, and then submit it to itself. You then check in your PHP script if the "submit" button was pressed and react accordingly.

You are then returning to the same form and outputing your error message and you can also clear the form contents. If you don't understand what I mean I can write the page out for you.
 
Believe it or not I've too just completed my first PHP input form and its quite similar to yours.

I am anew to PHP so what I say might not be the correct way but its working for me.

To make it stay on the same page I used this:
PHP:
<?php
	require_once './_scripts/send-invite.php';
	?>
<form action="index.php" method="post">

I called in the PHP that does the validation using the top section, and somehow (via black magic maybe?) the bottom section with index.php keeps it on the same page. But you will have to change index.php to what ever the current page is.

As for clearing the email field this might work:
PHP:
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{

	show_error("The E-mail address entered is not valid.");
	$email = NULL;

}

With that too you might have to edit the HTML adding this:
Code:
value="<?php echo "$email" ?>"

To the input field of the email, hopefully I've understood you correctly and I've been of some help, I'm still learning myself.
 
@analog

To an extent I understand! basically I have my enquiryform.html and my submit.php

the line:

require_once './_scripts/send-invite.php';

goes in the PHP yes? which should reference my submit.php?

e.g.

require_once 'submit.php';
 
Put your PHP script on your enquiry form page and change that to a PHP page. Then you can post back to itself and stay on the page.

That said, you are trying to achieve client side validation with server side scripting. You may as well just use JS for the validation and then post the data off when it passes.
 
Yea, as gord says, put everything in one file. Make the form action

PHP:
<?php echo($_SERVER['PHP_SELF']) ?>

Then at the top of the file, you can do something like

PHP:
if(isset($_POST['submit'])) 
{
   //submission code and redirect here
}

and if it isn't successful (and doesn't redirect) then it'll just continue processing the page and load the form again.

Also for the input value="" attribute, you'll want something like

PHP:
<?php if(isset($_POST['input_name'])) echo($_POST['input_name']) ?>

That'll keep the data in the form so the user doesn't have to re-enter it if it's unsuccessful.

Edit: for the email-clearing thing, just omit the value="" thing I mentioned above for that field and it'll leave it blank on each incorrect submission
 
Back
Top Bottom