Jump to content

need to add validating to PHP survey script


Yona

Recommended Posts

<?php
include('header.php');
  /**
   * 
   * @package jQuery PHP Poll and Survey
   * @author NETCSHOP.COM
   * @copyright 2018
   * @version v1.0.0
   *
   **/
/* ---------------------- Email Settings ---------------------- */
$to = 'your_email@example.com'; 	// Specifies the receiver email address to send survey report
$subject = 'My subject'; 			// Specifies the subject of the email
$from = 'your_email@example.com';	// Optional: Specifies additional headers, like From, Cs or Bcc

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
/* ------------------------------------------------------------ */

/*-------- SAMPLE DATA --------*/

$questions['1']['status'] = 'enabled';
$questions['1']['info'] = 'Tooltip for question 1';
$questions['1']['type'] = 'text';
$questions['1']['ques'] = 'What is your email address?';

$questions['2']['status'] = 'enabled';
$questions['2']['info'] = 'Tooltip for question 2';
$questions['2']['type'] = 'radio';
$questions['2']['ques'] = 'What is your gender?';
$questions['2']['opts'][] = 'Male';
$questions['2']['opts'][] = 'Female';

$questions['3']['status'] = 'enabled';
$questions['3']['info'] = 'Tooltip for question 3';
$questions['3']['type'] = 'checkbox';
$questions['3']['ques'] = 'What is your favorite search engine?';
$questions['3']['opts'][] = 'Google';
$questions['3']['opts'][] = 'Bing';
$questions['3']['opts'][] = 'Yahoo';
$questions['3']['opts'][] = 'DuckDuckGo';

$questions['4']['status'] = 'enabled';
$questions['4']['info'] = 'Tooltip for question 4';
$questions['4']['type'] = 'select';
$questions['4']['ques'] = 'What is your favorite color?';
$questions['4']['opts'][] = 'Red';
$questions['4']['opts'][] = 'Orange';
$questions['4']['opts'][] = 'Yellow';
$questions['4']['opts'][] = 'Green';
$questions['4']['opts'][] = 'Blue';
$questions['4']['opts'][] = 'Purple';
$questions['4']['opts'][] = 'Brown';
$questions['4']['opts'][] = 'Black';
$questions['4']['opts'][] = 'White';

$questions['5']['status'] = 'enabled';
$questions['5']['info'] = 'Tooltip for question 5';
$questions['5']['type'] = 'textarea';
$questions['5']['ques'] = 'Please share your opinion:';
$questions['5']['placeholder'] = 'Write your feedback here';
$questions['5']['rows'] = '7';
$questions['5']['cols'] = '106';

$questions['6']['status'] = 'enabled';
$questions['6']['info'] = 'Tool for question 6';
$questions['6']['type'] = 'rate';
$questions['6']['ques'] = 'Please rate your overall satisfaction:';
$questions['6']['opts'][] = 'Very Poor';
$questions['6']['opts'][] = 'Poor';
$questions['6']['opts'][] = 'Average';
$questions['6']['opts'][] = 'Good';
$questions['6']['opts'][] = 'Very Good';
$questions['6']['opts'][] = 'Excellent';

$questions['7']['status'] = 'enabled';
$questions['7']['optional'] = 'yes';
$questions['7']['info'] = 'Tooltip for question 7';
$questions['7']['type'] = 'radio';
$questions['7']['ques'] = 'Was your original question answered?';
$questions['7']['opts'][] = 'Yes';
$questions['7']['opts'][] = 'No';

$questions['8']['status'] = 'disabled';
$questions['8']['optional'] = 'yes';
$questions['8']['info'] = 'Tooltip for question 8';
$questions['8']['type'] = 'radio';
$questions['8']['ques'] = 'Do you like survey tools?';
$questions['8']['opts'][] = 'Yes';
$questions['8']['opts'][] = 'No';
	
if(isset($_POST['submit'])){
	$msg = "<html><body>";
	$msg .= "<table>";
	$i = 1;
	foreach ($_POST as $key => $value) {
		if($key != 'submit')
		{
			$msg .= "<tr><td>Q ".$i.": </td><td>".$questions[$key]['ques']."</td></tr>";
			$msg .= "<tr><td>A ".$i.": </td><td>".$value."</td></tr>";
		}
		$i++;
	}
	$msg .= "</table>";
	$msg .= "</html></body>";
	if(mail($to, $subject, $msg, $headers)){
		echo 'Thanks for taking this survey, your answers has been sent successfully.';
	} else{
		echo 'Unable to send email. Please try again.';
	}
}else{
	print "<div class=\"loader\"></div>";
	print "<form method=\"POST\">";
	foreach($questions as $key => $value)
	{
		$status = $questions[$key]['status'];
		$type = $questions[$key]['type'];
		$opts = $questions[$key]['opts'];
		$info = $questions[$key]['info'];
		
		if($type == 'text' && $status == 'enabled')
		{
			print "<div class=\"field\">";
			print "<p>".$questions[$key]['ques']." <img src=\"assets/icons/info.png\" title='".$info."' alt=\"Survey\" /></p>";
			print "<input type=\"text\" name=".$key." />";
			print "</div>";
		}
		if($type == 'radio' && $status == 'enabled')
		{
			print "<div class=\"field\">";
			print "<p>".$questions[$key]['ques']." <img src=\"assets/icons/info.png\" title='".$info."' alt=\"Survey\" /></p>";
			foreach($opts as $op_key => $op_value)
			{
				print "<label><input type=\"radio\" name=".$key." value=".$op_value." />".$op_value."</label>";
				
			}
			print "</div>";
		}
		if($type == 'checkbox' && $status == 'enabled')
		{
			print "<div class=\"field\">";
			print "<p>".$questions[$key]['ques']." <img src=\"assets/icons/info.png\" title='".$info."' alt=\"Survey\" /></p>";
			foreach($opts as $op_key => $op_value)
			{
				print "<label><input type=\"checkbox\" name=".$key." value=".$op_value." />".$op_value."</label>";
			}
			print "</div>";
		}
		if($type == 'select' && $status == 'enabled')
		{
			print "<div class=\"field\">";
			print "<p>".$questions[$key]['ques']." <img src=\"assets/icons/info.png\" title='".$info."' alt=\"Survey\" /></p>";
			print "<select name=".$key.">";
			print "<option value=\"0\">Choose...</option>";
			foreach($opts as $op_key => $op_value)
			{
				print "<option value=".$op_value." />".$op_value."</option>";
			}
			print "</select>";
			print "</div>";
		}
		if($type == 'textarea' && $status == 'enabled')
		{
			print "<div class=\"field\">";
			print "<p>".$questions[$key]['ques']." <img src=\"assets/icons/info.png\" title='".$info."' alt=\"Survey\" /></span></p>";
			print "<textarea rows=".$questions[$key]['rows']." cols=".$questions[$key]['cols']." name=".$key."></textarea>";
			print "</div>";
		}
		if($type == 'rate' && $status == 'enabled')
		{
			print "<div class=\"field\">";
			print "<p>".$questions[$key]['ques']." <img src=\"assets/icons/info.png\" title='".$info."' alt=\"Survey\" /></p>";
			foreach($opts as $rate_key => $rate_value)
			{
				print "<input type=\"radio\" name='".$key."' value='".$rate_value."' /><img src=\"assets/images/stars-".$rate_key."\" /> ".$rate_value."<br>\n";
			}
			print "</div>";
		}
	}	
	print "<div class=\"field\">";
	print "<input type=\"submit\" class=\"BtnSend\" name=\"submit\" value=\"Send\">\n";
	print "</form></div>";
}
include('footer.php');
?>   

This is the survey script. I need to check that fields are filled.

Link to comment
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.