Q&A Script?

Prof.

Senior Member
Joined
Oct 19, 2009
Messages
824
Reaction score
5
Location
Cape Town
I'm currently designing a website and I'm looking for a script that accomplishes a dynamic Questions&Answers-type arrangement in a very simple layout.

Any random user should be able to type in a question and maybe a reCaptcha and then I should be notified of the question via email (probably php_mail function). I should then be able to open an admin page on the site where I can read questions and reply to them. Once I reply to a question, the question and my answer should be visible on the website on the public Q&A page.

So it's basically a dynamic interactive FAQ and contact-page mashup.

Is there any script that can accomplish something similar to this out there that I can just "customize" into oblivion or should I just code this thing from scratch?
 
I would probably be better of just writing my own mini-system, what do you guys think?
 
I can give you the script to send an email from php...

Save this page as email_worker.php

Code:
<?php

if(isset($_POST['email'])) {
     
    $email_to = "[email protected]";
    $email_subject = "Hello There";
     
     
    function died($error) {
	$_SESSION['pageresult'] = "Error. Email has NOT been sent. Please check you have filled in all the fields.";
        header("Location: http://www.yoursite.co.za/email_worker.php");  
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])) {
	$_SESSION['pageresult'] = "Error. Email has NOT been sent. Please check you have filled in all the fields.";
        header("Location: http://www.yoursite.co.za/email_worker.php");      
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $message = $_POST['message']; // required
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br/>';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br/>';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br/>';
  }
  if(strlen($message) < 2) {
    $error_message .= 'The Message you entered does not appear to be valid.<br/>';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
$_SESSION['pageresult'] = "Email has been sent. Thank you we will contact you shortly.";
header("Location: http://www.yoursite.co.za/email_worker.php");
}
?>

Then save this page as what ever you like..

Code:
<form name="contactform" method="post" action="email_worker.php">
<table width="400">
<tr>
 <td>
  <label for="first_name">First Name:</label>
 </td>
 <td>
  <input  type="text" name="first_name" maxlength="50" style="width:100%">
 </td>
</tr>
<tr>
 <td>
  <label for="last_name">Last Name:</label>
 </td>
 <td>
  <input  type="text" name="last_name" maxlength="50" style="width:100%">
 </td>
</tr>
<tr>
 <td>
  <label for="email">Email Address:</label>
 </td>
 <td>
  <input  type="text" name="email" maxlength="80" style="width:100%">
 </td>
</tr>
<tr>
 <td>
  <label for="comments">Message:</label>
 </td>
 <td>
  <textarea  name="message" maxlength="1000" cols="25" rows="6" style="width:100%"></textarea>
 </td>
</tr>
<tr>
<td>&nbsp</td>
 <td style="text-align:center">
  <input type="submit" value="Submit" style="height: 30px; width:101%">
 </td>
</tr>
</table>
</form>
 
Here I found a little user login template I made a little while back, it's very basic but I'm sure with a little customization it will do what you need. Along with the above script your half way there.. :)

http://dl.dropbox.com/u/70411993/mysite.7z

Peace.

You will probably want to send the question to the database at the same time you send the mail. In the sites code there is a add member page if you dissect that and maybe add it to the email processing form. Then the login to the secure page would just read from the database.. :)
 
Last edited:
This is a better way of checking if the person is logged in... the method in that file doesn't really work to great.. change the session file to this...

Code:
<?PHP
if(session_id() == '')
{
session_start();
}
if ($pagesec == true)
{
if($_SESSION['loggedIn'] != true)
{
header('location: http://www.yoursite.co.za/index.php');
exit;
}
if($pageadmin == true)
{
if($_SESSION['usergroup'] != 'admin')
{
header('location: http://www.yoursite.co.za/index.php');
exit;
}
}
}
?>

then on every page that needs to be secure put this on top..

Code:
<?PHP
$pagesec = true;
$pageadmin = true;
include("session.php");
?>

Obviously change out all the variable names to random string to help with security..

Edit: You must set the usergroup and loggedIn session variables when the user logs in..
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X