Page 1 of 1

Support Ticket System

Posted: Sat Jun 16, 2012 8:18 pm
by SicX
Hey guys,
I want a "ticket-system" on my homepage with fields which are required to send this ticket. So far I've done the "layout" with the forms. My problem: I don't know how to do those fields which are required and sending this ticket to my email address.

(Some words are german (because I'm german), but I think you will probably understand it)

support-ticket.php:
<div id="kontaktformular" style="width: 500px" align="left">

<form action="kontakt-ticket.php" method="get">

<p><b>Name *</b></p>     
  <center><input name="name" size="67"></center>

<p><b>E-Mail *</b></p>
 <center><input name="email" size="67"></center>

<p><b>Betreff *</b></p>
 <select name="betreff" size="1">

<option>Hilfe</option>

<option>Shop</option>

<option>Spieler melden</option>

<option>Bug melden</option>

<option>Entbannen</option>

</select>

<p><b>Nachricht *</b></p>

<textarea name="text" cols="50" rows="20" size="70"></textarea><br />

<input name="submit" type="submit" value="Absenden">

</form>
<p><i>*Diese Felder sind Pflichtfelder und müssen ausgefüllt werden!  /* All fields are required! (: */</i></p>
</div>	
kontakt-ticket.php:
<- Here is my problem, don't really know how to do! (bad php knowlege :? ) ->

edit: If a field, which is required, isn't filled out, there should be an "error output" like: "Please type your email here" or something like this and the text in the other forms are still there - is this possible?

SicX :)

Re: Support Ticket System

Posted: Sat Jun 16, 2012 8:50 pm
by Temor
Hey.
I'd suggest you take a look at a tutorial on post handling. This one for example:



It will make this a lot easier for you :)

Read on after you've watched it.

You want to check if each field is filled using the empty() function. If the field is empty, you're going to add an error to an array.
here is an example:
<?php
$errors = array();
if(empty($_POST['name'])){
$errors[] = 'Please fill in a name.';
}
?>
( having square brackets [] after a variable adds the value to the end of the array. )

Once you've done this check for every field, you'll want to check if the $errors array is empty. If it is, it means there are no errors and every field is filled.
If there' are no errors, you can use the mail() function to send an email.
You can read all about the function here:
http://php.net/manual/en/function.mail.php

If you're having a hard time figuring out the link, you can always watch one of Jacek's tutorials. He's got a few videos where he uses the mail() function.
You can find them all here:
http://betterphp.co.uk/tutorials.html


If your $errors array is not empty, i.e there is an error, you want to print these to the page. The easiest way to do this is to use a foreach loop which loops once for every entry in the array.
<?php
foreach($errors as $error){
echo $error;
}
?>
You should run this only if the form has been submitted. You can check if the form has been submitted by checking if a $_POST variable is set using the isset() function.
<?php
if(isset($_POST['name'])){
// Check for errors

// Send email

// Print errors
}
?>
Speaking of which, i'd suggest you use POST instead of GET as your form method. Having all that information in the url is just messy to be honest, and if there is sensitive information being submitted, having it in the url makes it very much visible.

If you have any other questions, feel free to ask them here :)

Re: Support Ticket System

Posted: Sun Jun 17, 2012 1:19 am
by SicX
Hey! :)
The mail() function is now working perfectly! The only thing which doesn't work are the required fields... :(
Wouldn't just a simpel code like this work?
if (isset($_POST["name"]) . ($_POST["email"]) . ($_POST["text"])) {
mail()
} else {
error output
}
I don't know exactly what to do, I've tried everything which seems logical to me .. :/
Also I don't know how this:
<?php
foreach($errors as $error){
echo $error;
}
?>
should work with this:
<?php
$errors = array();
if(empty($_POST['name'])){
$errors[] = 'Please fill in a name.';
}
?>

Re: Support Ticket System

Posted: Sun Jun 17, 2012 1:30 am
by Temor
You could do that. But it won't tell you which field you missed, only that you missed a field.

This creates an empty array and then adds one entry, "Please fill in a name" if the name field is empty.
<?php
$errors = array();
if(empty($_POST['name'])){
$errors[] = 'Please fill in a name.';
}
?>
You would have to create one of these for every field. The text inside the [' '] corresponds to the field name.
<input type="text" name="name" /> =
if(empty($_POST['name'])){
$errors[] = 'Please fill in a name.';
}

<input type="email" name="email" /> =
if(empty($_POST['email'])){
$errors[] = 'Please fill in an email.';
}
This takes the $errors array and counts the entries. In this case it's one.
It then loops once, and outputs the message. If you had more than one entry it would post every one. It's faster than manually echoing out every variable.
<?php
foreach($errors as $error){
echo $error;
}
?>

Re: Support Ticket System

Posted: Sun Jun 17, 2012 2:05 am
by SicX
It's getting a long night... :P

Should this part right here, in the file with the forms or in the file with the mail()-function?
<?php
foreach($errors as $error){
echo $error;
}
?>
If I'm using this in the file with the forms it always prints the error messages (even with filled forms - and not under the forms where it should be)!

Re: Support Ticket System

Posted: Sun Jun 17, 2012 12:03 pm
by Temor
This is how it should look if you're using this form:
<div id="kontaktformular" style="width: 500px" align="left">
 
<form action="kontakt-ticket.php" method="post"> <!-- Notice I changed get to post-->
 
<p><b>Name *</b></p>    
  <center><input name="name" size="67"></center>
 
<p><b>E-Mail *</b></p>
 <center><input name="email" size="67"></center>
 
<p><b>Betreff *</b></p>
 <select name="betreff" size="1">
 
<option>Hilfe</option>
 
<option>Shop</option>
 
<option>Spieler melden</option>
 
<option>Bug melden</option>
 
<option>Entbannen</option>
 
</select>
 
<p><b>Nachricht *</b></p>
 
<textarea name="text" cols="50" rows="20" size="70"></textarea><br />
 
<input name="submit" type="submit" value="Absenden">
 
</form>
<p><i>*Diese Felder sind Pflichtfelder und müssen ausgefüllt werden!  /* All fields are required! (: */</i></p>
</div> 
<?php
// Check to see if the form has been submiitted.
if(isset($_POST['name'])){
	$errors = array(); // Create an empty array.
		if(empty($_POST['name'])){ // Check to see if name field is empty.
			$errors[] = 'Enter a a name'; // Add an error message if the field is empty.
		}
		if(empty($_POST['email'])){ // Check to see if email field is empty.
			$errors[] = 'Enter an email'; 
		}
		if(empty($_POST['betreff'])){ // Check to see if betreff is empty.
			$errors[] = 'Please choose an option'; 
		}
		if(empty($_POST['text'])){ // Check to see if text field is empty.
			$errors[] = 'Please enter some text'; 
		}
		
		if(empty($errors)){ // Check to see if the errors array is empty. If it is, it means all fields are filled.
			mail(); // Send email
		}
		
		if(empty($errors) == false){ // Check to see if there are errors in the array.
			foreach($errors as $error){ // Loop through all the errors.
				echo $error; // Print each error to the screen.
			}
		}
	
	}
?>
You can put all of this in the same file if you want. Just put the php file in the top of your html file and change the file name to .php and leave the action for the form empty. This tells the form to send the post information to the same page as itself.

<?php
// Check to see if the form has been submiitted.
if(isset($_POST['name'])){
	$errors = array(); // Create an empty array.
		if(empty($_POST['name'])){ // Check to see if name field is empty.
			$errors[] = 'Enter a a name'; // Add an error message if the field is empty.
		}
		if(empty($_POST['email'])){ // Check to see if email field is empty.
			$errors[] = 'Enter an email'; 
		}
		if(empty($_POST['betreff'])){ // Check to see if betreff is empty.
			$errors[] = 'Please choose an option'; 
		}
		if(empty($_POST['text'])){ // Check to see if text field is empty.
			$errors[] = 'Please enter some text'; 
		}
		
		if(empty($errors)){ // Check to see if the errors array is empty. If it is, it means all fields are filled.
			mail(); // Send email
		}
	}
	
		/* You can move this part to anywhere on the page if you want the errors to display elsewhere. Just put <?php  ?> tags around the code if you're putting it in the html parts. */
		if(empty($errors) == false){ // Check to see if there are errors in the array.
			foreach($errors as $error){ // Loop through all the errors.
				echo $error; // Print each error to the screen.
			}
		}
	
?>
<div id="kontaktformular" style="width: 500px" align="left">
 
<form action="" method="post"> <!-- Notice I changed get to post- and left the action field empty. -->
 
<p><b>Name *</b></p>    
  <center><input name="name" size="67"></center>
 
<p><b>E-Mail *</b></p>
 <center><input name="email" size="67"></center>
 
<p><b>Betreff *</b></p>
 <select name="betreff" size="1">
 
<option>Hilfe</option>
 
<option>Shop</option>
 
<option>Spieler melden</option>
 
<option>Bug melden</option>
 
<option>Entbannen</option>
 
</select>
 
<p><b>Nachricht *</b></p>
 
<textarea name="text" cols="50" rows="20" size="70"></textarea><br />
 
<input name="submit" type="submit" value="Absenden">
 
</form>
<p><i>*Diese Felder sind Pflichtfelder und müssen ausgefüllt werden!  /* All fields are required! (: */</i></p>
</div> 

I hope you understand it a bit better now :)

Re: Support Ticket System

Posted: Sun Jun 17, 2012 12:28 pm
by SicX
Thank you!! :)
This is awesome, you're genius! :D

Re: Support Ticket System

Posted: Sun Jun 17, 2012 12:37 pm
by Temor
I just hope you learned something :)

Re: Support Ticket System

Posted: Sun Jun 17, 2012 12:57 pm
by SicX
Of course! :) The next thing I will do on my own is a contact form with fields that are required ;-)
Let's see how this goes! :P

Re: Support Ticket System

Posted: Sun Jun 17, 2012 1:00 pm
by Temor
Good luck :)

Re: Support Ticket System

Posted: Sun Jun 17, 2012 2:25 pm
by SicX
Okay, contact form works perfectly!
Thanks again :)