<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}
?>


<html>

<?php include 'head.php'; ?>

<div id="aside">
	<!--Link for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>
	
	<fieldset style="width:85%"><legend>HELP</legend>
	<p>If you have any question about this item please fill in this form.</p>
	<p>We will try to answer in the shortest time possible to the sender's e-mail address.</p>
</div>

<div id="content">

<?php

if ($_SERVER["REQUEST_METHOD"] != "POST")
{
//check $id setted or not
//if (!isset($id))
//{
//echo "Sorry, there appears to have been an error.<br>";
//echo "<a href='index.php'><p style='text-align:center'>Go back to home</a></p>";
//exit;
//}

//get the ID from the querystring.
$id = $_GET['id']; //get the value from the address bar
if(!is_numeric($id))
{ //check is a number.
echo "Sorry, there appears to have been an error.";
echo "<a href='index.php'><p style='text-align:center'>Go back to home</a></p>";
exit;
}

//sql connection details
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);


$query=Mysql_query("SELECT * FROM natty WHERE ItemID=$id ");
$row=mysql_fetch_object($query);
echo mysql_error();

//get the value of each column and add to local variables
$FeedbackName = $row->ItemName;

//close the connection to the database.
mysql_close($con); 

}


 // define variables and set to empty values
$NameErr = $EmailErr = $SubjectErr = "";
$Name = $Subject = $Comments = $Email = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

	$validations = 0; //reference variable for validations
	
	$FeedbackName = test_input($_POST["FeedbackName"]);
	
	if (isset($Comments))
	{
	$Comments = test_input($_POST["Comments"]);
	}
   
  if (empty($_POST["Name"]))
    {
	$NameErr = "(Name is required)";
	$validations++;
	}
  else
    {$Name = test_input($_POST["Name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$Name))
      {
      $nameErr = "Only letters and white space allowed";
	  $validations++;
      }
	}
	
  if (empty($_POST["Subject"]))
    {
	$SubjectErr = "(Subject is required)";
	$validations++;
	}
  else
    {$Subject = test_input($_POST["Subject"]);}
	
  if (empty($_POST["Email"]))
    {
	$EmailErr = "(Email is required)";
	$validations++;
	}
  else
    {$Email = test_input($_POST["Email"]);
	// check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$Email))
      {
      $EmailErr = "Invalid email format";
	  $validations++;
      }
	}
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

//if all validation was right it must be 0
if (isset($validations))
{
	if ($validations == 0)
	{

$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);

//replace single quotes from text field to insert to sql
$Email2 = str_replace("'", "''", $Email);
$Subject2 = str_replace("'", "''", $Subject);
$Comments2 = str_replace("'", "''", $Comments);

//construct a valid SQL statement that will be send to the database server.
$sql="INSERT INTO feedbacks (FeedbackItem, FeedbackName, FeedbackSubject, FeedbackComment, FeedbackEmail, FeedbackState) 
VALUES 
('$FeedbackName', '$Name', '$Subject2', '$Comments2', '$Email2', 'Waiting')";

//display the SQL we will sent to server -  for debugging only
//echo $sql;

// run the SQL query on the server (i.e. send the SQL to the database server)

if (!mysql_query($sql,$con))
{
die ('Error: ' . mysql_error());
}

echo "<h1>Question recorded</h1><br>";
echo "<p>We have received your message. We try to answer as soon as possible. </p>";

//close the connection to the database.
mysql_close($con); 
}
}

?>


<!--HTML form for ask from item inputs-->
<br>
<fieldset style="width:60%"><legend>ASK A QUESTION</legend>

<h2>Ask from <?php echo $FeedbackName;?></h2>

<p><span class="error">* required fields</span></p>

<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<input type="hidden" name="FeedbackName" value="<?php echo $FeedbackName; ?>">

Name: <span class="error">*</span><span class="error"> <?php echo $NameErr;?> </span><br>
<input type="text" name="Name" size="50" value="<?php echo $Name; ?>"><br><br>

Subject: <span class="error">*</span><span class="error"> <?php echo $SubjectErr;?> </span><br>
<input type="text" name="Subject" size="70" value="<?php echo $Subject; ?>"><br><br>

Comments: <br><textarea name="Comments" rows="10" cols="60"><?php echo $Comments; ?></textarea><br><br>

E-mail: <span class="error">*</span><span class="error"> <?php echo $EmailErr;?> </span><br>
<input type="text" name="Email" size="50" value="<?php echo $Email; ?>"><br><br>

<input type="submit" name="Submit" value="Send"><br>
</form></fieldset>


</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>


</div>
</body>
</html>