<?php
include_once "../includes/connectConfig.php";

if($_POST)
{
	$nzletter_email = test_input($_POST['newsletter_email']);
	
	if(!empty($nzletter_email) && filter_var($nzletter_email,FILTER_VALIDATE_EMAIL)){
		
		$chk_em = gtOne("news_subscribedlist"," ns_emails = '$nzletter_email' ",'ns_status');
		if($chk_em == 1){
			
			$output["respo"] = "Email Already Registered.";
			echo json_encode($output);
			exit;
			
		}else{
			if($chk_em == 2){
				$ins_usr = $db_con->prepare("UPDATE news_subscribedlist ns_status = '1' WHERE ns_emails=:nems");
				$ins_usr->bindParam(':nems',$nzletter_email);
				$ins_usr->execute();
				
				$output["respo"] = "Email Saved. Thanks for subscribing Us.";
				echo json_encode($output);
				exit;
			}
			else{
				$ins_usr = $db_con->prepare("INSERT INTO news_subscribedlist (ns_emails, ns_status) VALUES(:nems, '1')");
				$ins_usr->bindParam(':nems',$nzletter_email);
				$ins_usr->execute();
				
				$output["respo"] = "Email Saved. Thanks for subscribing Us.";
				echo json_encode($output);
				exit;
			}
			
		}
		
	}
	else{
		$output["respo"] = "Invalid email address.";
		echo json_encode($output);
		exit;
	
		
	}
}
?>
