Hey friends,
I have a page that I've been working on for HOURS and finally almost have it perfect except for TWO things:
1. Every time I load the page it automatically inserts a blank row of data into my MySQL database. (Submitting it after filling out the for works fine.)
-How can I stop it from dong that every time the page loads?
I've been messing with if (!isset($_POST)) but I can't find the right placement.
2. My email at the bottom isn't sending. Can anyone see a better spot to place the mail() script? (This one is less important)
Thank you!
<?php
include("database.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$name=$_POST["name"];
$email=$_POST["email"];
$cat=$_POST["cat"];
$issue=$_POST["issue"];
$descript=$_POST["descript"];
$trouble=$_POST["trouble"];
$device=$_POST["device"];
$priority=$_POST["priority"];
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link href="./css/submitticket.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
<Title>IT Support - Submit Ticket</Title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
</head>
<body>
<?php
if(array_key_exists('submit',$_POST)){
// Fields that are on form
$expected = array('name', 'email', 'cat', 'issue', 'descript', 'trouble', 'device', 'priority');
// Set required fields
$required = array('name', 'email', 'descript', 'trouble', 'device');
// Initialize array for errors
$errors = array();
foreach ($_POST as $field => $value){
// Assign to $temp and trim spaces if not array
$temp = is_array($value) ? $value : trim($value);
// If field is empty and required, tag onto $errors array
if (empty($temp) && in_array($field, $required)) {
array_push($errors, $field);
}
}
//If good to go
if (empty($errors)){?>
<div class="green">Thank you.</div>
<?php
unset($errors);
}
}
?>
<div class="container">
<header>Submit Ticket</header>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="info">
<table>
<tr>
<td>
<!---<img class="small_logo" src=""> -->
</td>
</tr>
<tr>
<td>
<p>Name
</td>
<td>
<?php if (isset($errors) && in_array('name', $errors)){?>
<div class="red">Please enter name</div>
<?php } ?>
<input type="text" name="name" id="name" class="info" style="text-align: left;">
</td>
</tr>
<tr>
<td>
<p>Email
</td>
<td>
<input type="text" name="email" id="email" class="info">@example.com
</td>
</tr>
</table>
</div>
<p>Category: <input type="hidden" name="cat" id="cat" value="User accounts"><b>User accounts</b></p>
<p>Issue: <input type="hidden" name="issue" id="issue" value="Unable to Login"><b>Unable to login</b></p>
<p>Details<br />
<textarea name="descript" id="descript"></textarea></p>
<p>Previous Troubleshooting <span style="font-size: 12px;">(if applicable)</span><br />
<textarea name="trouble" id="trouble"></textarea></p>
<p>Device S/N, IP Address, and/or Location <br />
<b><textarea name="device" name="device" class="small"></textarea></b></p>
<p style="float:right; color: white; padding-top:10px;">Priority: <select name="priority" class="button">
<option value="Low">Low</option>
<option value="High">High</option>
</select>
<input type="button" value="Cancel" onClick="history.go(-1);return true;"class="button"> <input name="submit" id="submit" type="submit" value="Submit" class="button"></p>
</form>
<?php
$today = date("M d Y");
$sql="INSERT INTO info (Ticket, Dateadded, Name, Email, Issue, Cat, Descript, Trouble, Device, Priority)
VALUES
('$_POST[ticket]','$today','$_POST[name]','$_POST[email]','$_POST[issue]','$_POST[cat]','$_POST[descript]','$_POST[trouble]','$_POST[device]','$_POST[priority]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
$query = "SELECT ticket, dateadded, name, email, issue, descript, cat, device, trouble, priority FROM info ORDER by ticket DESC LIMIT 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$to = "{$row['email']}@me.com";
$subject = "UWS Support - {$row['cat']} - Ticket #{$row['ticket']}";
$message = "Thank you for submitting a request to get this issue resolved. Your ticket # is {$row['ticket']}. You will be notified by email if we need any more information or when we've fixed this issue.\n\n Issue: {$row['issue']}\n Details: {$row['descript']}\n Device: {$row['device']}\n\n-UWS Support Team\n\n\nIf you did not submit or submitted this Ticket in error please reply to this email so that we may cancel it.";
$from = "UWS_IT@group.example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
}
}
?>
</div>
</body>
</html>