I am having a problem with getting a php script to work correctly.
Website in HTML: http://www.architecturalinstallationteam.com/
Website after ported to PHP: http://www.architecturalinstallationteam.com/sandbox/
First off, the script works correctly by itself when not included in any page. http://www.architecturalinstallationteam.com/sandbox/include/contact.php
The code is:
<?php
switch($_POST['view'])
{
case 1: /* first time submit, show preview */
$type = "hidden";
$vis_type = "none";
$Name = $_POST['Name'];
$Title = $_POST['Title'];
$Company = $_POST['Company'];
$Telephone = $_POST['Telephone'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$view = 2;
$EXTRA_INFO = "<div id='container'>
<strong>Are your sure you want to submit:</strong>
<div id='left'>
<p>Name:</p>
<p>Title:</p>
<p>Company:</p>
<p>Telephone:</p>
<p>Email:</p>
<p>Company:</p>
</div><!-- left -->
<div id='right'>
$Name<br>
$Title<br>
$Company<br>
$Telephone<br>
$Email<br>
$Message<br>
</div>
<!-- right -->
<div class='clear'></div>
</div><!-- container -->";
$FORM = "";
$INPUT_BUTTON = "<input type='submit' name='submit' value='Edit'><input type='submit' name='submit' value='Send'>";
break;
case 2: /* second time submit, if edit pressed, then edit else submit */
if($_POST['submit'] == "Edit")
{ /* Edit and reset to first time here */
$Name = $_POST['Name'];
$Title = $_POST['Title'];
$Company = $_POST['Company'];
$Telephone = $_POST['Telephone'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$type = "text";
$vis_type = "block";
$view = 1;
$INPUT_BUTTON = "<input type='submit' name='preview' value='Preview'>";
//$FORM = "<form action='$PHP_SELF' method='post'>
// <input type='$type' name='0' size='4' value='Name:'>
// <input type='hidden' value='$view' name='view'>
// <input type='$type' name='Name' value='$Name'><br>";
$EXTRA_INFO = "<strong>Edit Message:</strong><br>";
}
else
{ /* Submit info */
// Get POST variables
$EmailFrom = "AIT Website";
$EmailTo = "
[email protected]";
$Subject = "AIT Website - Contact Requested";
$Name = Trim(stripslashes($_POST['Name']));
$Title = Trim(stripslashes($_POST['Title']));
$Company = Trim(stripslashes($_POST['Company']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// Create Email
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Title: ";
$Body .= $Title;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// Send Email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// Redirect
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php?p=sent\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php?p=error\">";
//$EXTRA_INFO = "Error! Message not successfully sent.<br><pre>$Name</pre>";
//$type = "hidden";
//$vis_type = "none";
}
}
break;
default: /* either we've never been here or something has screwed our counter */
$EXTRA_INFO = "<strong>Enter information below:</strong><br>";
$INPUT_BUTTON = "<input type='submit' name='preview' value='preview'>";
$type = "text";
$vis_type = "block";
$view = 1;
break;
}
/* display our page accordingly */
echo $EXTRA_INFO;
echo "<form action='$PHP_SELF?p=contact_us' method='post'>
<div id='container'>
<div id='left' style='display: $vis_type'>
<p>Name:</p>
<p>Title:</p>
<p>Company:</p>
<p>Telephone:</p>
<p>Email:</p>
<p>Company:</p>
</div><!-- left -->
<div id='right'>
<input type='hidden' value='$view' name='view'>
<input type='$type' name='Name' value='$Name'><br>
<input type='hidden' value='$view' name='view'>
<input type='$type' name='Title' value='$Title'><br>
<input type='hidden' value='$view' name='view'>
<input type='$type' name='Company' value='$Company'><br>
<input type='hidden' value='$view' name='view'>
<input type='$type' name='Telephone' value='$Telephone'><br>
<input type='hidden' value='$view' name='view'>
<input type='$type' name='Email' value='$Email'><br>
<input type='hidden' value='$view' name='view'>
<input type='$type' name='Message' value='$Message'><br>
</p>
</div><!-- right -->
<div class='clear'></div>
</div><!-- container -->";
echo $INPUT_BUTTON;
echo "</form>";
?>
This code is included into include/contact_us.php which is dynamically included into the index with url parameter ?p=contact_us
My problem is that it doesn't function correctly - all it does is go to index.php when I submit form.
I think the problem lies in the form action='$PHP_SELF' but I have only basic knowledge of PHP to figure out how to get around this.
PLEASE...if someone can help me correct the existing code/optimize it, or if they have/know of a code that works better in the situation please share. I do not want to overwrite the HTML version of the website with the PHP version until I have fixed this section.
Note: I did not write this preview/edit script completely but I did improve it to work with hidden div and added the extra variables to suit my needs.
Thanks for any help,
- Revlet
EDIT: I tried to make a preview page before using $_SESSION variables and multiple includes but I couldn't get it to work. I prefer the less confusing scripts because I can easily add a captcha and manipulate them with greater ease. So if anyone can make a better script that would be ideal. Thanks again!