I am a beginner and trying to follow some tutorial videos for my school project. I am stuck when the data not inserted into the database. I double-check the code to tutorial and nothing wrong. There's no error message or notification too. I really appreciate your help.
action.php
<!--action.php-->
<?php
require 'config.php';
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$pcode = $_POST['pcode'];
$pqty = 1;
$stmt = $conn->prepare("SELECT product_code FROM cart WHERE product_code=?");
$stmt->bind_param("s",$pcode);
$stmt->execute();
$res = $stmt->get_result();
$r = $res->fetch_assoc();
$code = $r['product_code'];
if(!$code){
$query = $conn->prepare("INSERT INTO cart (product_name,product_price,product_image,qty,total_price,produk_code) VALUES (?,?,?,?,?,?)");
$query->bind_param("sssiss",$pname,$pprice,$pimage,$pqty,$pprice,$pcode);
$query->execute();
echo '<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item added to cart!</strong>
</div>';
}
else{
echo '<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item already added to your cart!</strong>
</div>';
}
}
?>
index.php
<!--index.php-->
<!DOCTYPE html>
<html>
<head>
<title>Map Store</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://kit.fontawesome.com/fc847822ba.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href="fontawesome/css/all.min.css"/>
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="index.php">Map Store</a>
<!-- Toggler/collapsibe Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link active" href="index.php">Products</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Category</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php">Checkout</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php"><i class="fas fa-shopping-cart text-white"> <span id="cart-item" class="badge badge-danger">0</span> </i></a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="row mt-2">
<div id="message">
</div>
<?php
include 'config.php';
$stmt = $conn->prepare("SELECT * FROM product");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()):
?>
<div class="col-lg-3">
<div class="card-deck">
<div class="card p-2 border-secondary mb-2">
<img src="<?= $row['product_image'] ?>" class="card-img-top" height="250">
<div class="card-body p-1">
<h4 class="card-title text-center text-info"><?= $row['product_name']?></h4>
<h5 class="card-text text-center text-danger"><?= number_format($row
['product_price'],2) ?>/-</h5>
</div>
<div class="card-footer p-1">
<form action="#" class="form-submit">
<input type="hidden" class="pid" value="<?= $row['id']?>">
<input type="hidden" class="pname" value="<?= $row['product_name']?>">
<input type="hidden" class="pprice" value="<?= $row['product_price']?>">
<input type="hidden" class="pimage" value="<?= $row['product_image']?>">
<input type="hidden" class="pcode" value="<?= $row['product_code']?>">
<button class="btn btn-info btn-block addItemBtn"><i class="fas fa-cart-plus "></i>Add to cart</button>
</form>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".addItemBtn").click(function(e){
e.preventDefault();
var $form = $(this).closest(".form-submit");
var pid = $form.find(".pid").val();
var pname = $form.find(".pname").val();
var pprice = $form.find(".pprice").val();
var pimage = $form.find(".pimage").val();
var pcode = $form.find(".pcode").val();
$.ajax({
url: 'action.php',
method: 'post',
data: {pid:pid,pname:pname,pprice:pprice,pimage:pimage,pcode:pcode},
success:function(response){
$("message").html(response);
}
});
});
});
</script>
</body>
</html>
config.php
<!--config.php-->
<?php
$conn = new mysqli("localhost","root","","checkout_system");
if($conn->connect_error){
die("Connection Failed!".$conn->connect_error);
}
?>
sorry i am new here and this my first post
The HTML elements in your form must have a name attribute to be available in $_POST, you set only a class :
<input type="hidden" name="pid" class="pid" value="<?= $row['id']?>">
I'm working on website allowing to manage candidacies.
Multiple features are already operational like advanced search, dynamic list of candidacies with ajax, sort, export content of candidacy in zip format in relation with GDPR. However, no architecture was developped for the moment.
So, from my login form, I would like to add MVC architecture.
Would I be correct in assuming that, the Model would contain the SQL select statements, the controller would handle the data e.g if (isset($_POST["identity"])) { and the view would contain the basic HTML form where the user would enter the data ?
The problem is that from my existing files, I don't really see how to add MVC.
My config.php will become model.php at the end, index.php, login.php are views, but for the controller, I am stuck ! :/
config. php :
<?php
$host = '127.0.0.1';
$db = 'candidates';
$user = 'root';
$pass = '';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try
{
$cnx = new PDO($dsn, $user, $pass, $opt);
}
catch(PDOException $e)
{
die('Connection failed :'. $e->getMessage());
}
?>
login.php :
<?php
session_start();
include('template.php');
if(isset($_SESSION['flash']))
{
foreach($_SESSION['flash'] as $type => $message)
{
echo $message;
}
unset($_SESSION['flash']);
}
if(isset($_SESSION['user']))
{
header('Location: ../index.php');
}
?>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<div class="container">
<div id="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href=""><img id="imgNavbar" src="../img/candidacy.jpg" alt="Home"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="../index.php">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item" style="margin-left: 0px">
<a class="nav-link" href="login.php">Connect</a>
</li>
</ul>
</div>
</nav>
</div>
<form method="post" action="checkLogin.php">
<div class="container">
<div class="card card-container">
<img id="profile-img" class="profile-img-card" src="../img/avatar.png" />
<p id="profile-name" class="profile-name-card"></p>
<form class="form-signin">
<span id="formIdentity" class="formIdentity"></span>
<input type="text" id="identity" name="identity" class="form-control" placeholder="Your identity" autofocus>
<input type="password" id="password" name="password" class="form-control" placeholder="Your password">
<button class="btn btn-lg btn-primary btn-block btn-signin" type="submit" id="submitBtn" name="submit" onclick="checkForm(this)">Connect</button>
</form><!-- /form -->
<a href="forgotPassword.php" class="forgot-password">
Forgot password ?
</a>
</div><!-- /card-container -->
</div><!-- /container -->
</form>
</div>
</body>
</html>
checkLogin.php
<?php
require('config/config.php');
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST['identity']) && isset($_POST['password']))
{
$stmt = $cnx->prepare("SELECT * from users WHERE nameUser = :username");
$stmt->bindValue(':username', $_POST['identity'], PDO::PARAM_STR);
$stmt->execute();
$users = $stmt->fetch(PDO::FETCH_ASSOC);
$result = $stmt->rowCount();
if($result > 0)
{
if(password_verify($_POST['password'], $users['passwordUser']))
{
$token = bin2hex(random_bytes(64));
$_SESSION['user'] = $users;
$_SESSION['token'] = $token;
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (120 * 60);
header('Location: ../index.php');
}
else
{
$_SESSION['flash']['info'] = "<div class='alert alert-danger'>Check your information !</div>";
header('Location: ../login.php');
exit(0);
}
}
else
{
$_SESSION['flash']['connect'] = "<div class='alert alert-danger'>Non-existent user !</div>";
header('Location: ../login.php');
exit(0);
}
$stmt->closeCursor();
}
if(empty($_POST['identity']) || empty($_POST['password']))
{
$_SESSION['flash']['empty'] = "<div class='alert alert-danger'>Fill in the fields please !</div>";
header('Location: ../login.php');
exit(0);
}
}
?>
index.php :
<?php
if(!isset($_SESSION))
{
session_start();
}
ob_start();
include('template.php');
require('config/config.php');
if(isset($_SESSION['user']))
{
$currentTime = time();
if($currentTime < $_SESSION['expire'])
{
?>
<html>
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<div class="container header">
<div id="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href=""><img id="imgNavbar" src="../img/candidacy.jpg" alt="Home"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="../index.php">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo $_SESSION['user']['nameUser']?></a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="myProfile.php">My Profile</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="listCandidacies.php">List of candidacies</a>
</li>
<li class="nav-item">
<a class="nav-link" href="addCandidacy.php">Add a candidacy</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Disconnect</a>
</li>
</ul>
</div>
</nav>
</div>
<div class="content">
<div class="col-lg-10">
<h3 class="text-uppercase">
<strong>Welcome to candidacy <br>space !</strong>
</h3>
</div>
</div>
<?php
}
else
{
unset($_SESSION['user']);
session_destroy();
header('Location: ../login.php');
}
}
else
{
unset($_SESSION['user']);
session_destroy();
header('Location: ../login.php');
}
?>
Actually, I believe you don’t just learn how to apply MVC to a specific problem, but either, you learn the concept and theory behind. You can have a look for basics in here.
Moreover, it is usually advised to use a framework so you wouldn’t unintentionally break some of the design pattern concepts.
Concerning your case, I would do it this way:
Models:
User
DatabaseConnector
Controllers:
LoginPageController
IndexController
Views:
Login
Index
Im getting the typical unexpected end of file
i cant see what I'm missing can someone else please help i've gone code blind
<?php
session_start();
include('config.php');
include('funcs.php');
date_default_timezone_set('Europe/London');
if(!in_array($_SESSION['userid'],$adminusr)){
header("Location: index.html");
}
?>
<html><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="css.css" rel="stylesheet" type="text/css">
</head><body>
<div class="navbar navbar-default navbar-static-top" style="height:55px; background-color:#FFFFFF; border-bottom:#FF6600 2px solid;">
<div class="container">
<div class="navbar-header" style="height:60px;">
<button type="button" class="navbar-toggle hidden-xs" data-toggle="collapse" data-target="#navbar-ex-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" style="padding-top:5px;"><img height="45" alt="Brand" src="logo.jpg"></a>
</div>
<div class="collapse navbar-collapse hidden-xs hidden-sm" id="navbar-ex-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
Jobs
</li>
<li>
Servicing
</li>
<li>
Prep
</li>
<li>
Finances
</li>
<li>
+ New Job
</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div class="hidden-lg hidden-md hidden-sm">
<ul class="nav nav-pills nav-justified">
<li>
Jobs
</li>
<li>
Servicing
</li>
<li>
Prep
</li>
<li>
Finances
</li>
<li>
+ New Job
</li>
</ul>
</div>
</div>
<div class="section text-justify" style="padding-bottom:0px;">
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<form role="form">
<div class="form-group">
<div class="input-group input-group-sm">
<input type="text" class="form-control" placeholder="Search for something ...">
<span class="input-group-btn">
<a class="btn btn-link" type="submit"><i class="fa fa-lg fa-search"></i></a>
</span>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="section text-center">
<div class="container">
<div class="row">
<div class="col-md-4 text-left">
<h3>
<i class="fa fa-cogs fa-fw fa-lg text-orange"></i>Current Jobs :</h3>
<?php
// List Jobs With Closest Req. By Date
$curdate = date('Y-m-d');
$listjobsq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE status != 'Completed' AND completed ='0' ORDER BY reqbydate ASC LIMIT 0,10");
$listjobs = mysqli_fetch_assoc($listjobsq);
?>
<ul class="list-group" style="border:none;">
<?php do {
// Job Time Frame Check
$cur = strtotime($curdate);
$req = strtotime($listjobs['reqbydate']);
$diff = floor(abs($req - $cur) / 86400);
$diffdays = ' Days';
$diffbg = 'background-color:#1EBB1B; color:#000000;';
if($req < $cur) { $diff = 'OVERDUE';$diffdays = '';$diffbg = 'background-color:#FF0000; color:#FFFFFF;';}
if($req == $cur) { $diff = 'TODAY';$diffdays = '';$diffbg = 'background-color:#F8C000; color:#000000;';}
if($diff == 1) { $diff = 'TOMORROW';$diffdays = '';$diffbg = 'background-color:#F8C000; color:#000000;';}
if($diff >= 2){ if($diff <= 7){$diffdays = ' Days';$diffbg = 'background-color:#F8C000; color:#000000;';} }
// End
// User Query
$wscid = $listjobs['wscid'];
$userq = mysqli_query($cona,"SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
?>
<li class="list-group-item text-right" style="border:none;"><span class="badge pull-left" style="<?php echo $diffbg;?>"><?php echo $diff. $diffdays;?></span><?php echo $user['forename'].' '.$user['surname'].' - ' .$listjobs['summary'];?></li>
<? } while($listjobs = mysqli_fetch_assoc($listjobsq));?>
<li class="list-group-item text-center" style="border:none;"> -- View All Jobs -- </li>
</ul>
<hr>
</div>
<div class="col-md-4 text-left">
<h3>
<i class="fa fa-fw fa-lg fa-wrench text-orange"></i>Services / Work Due :</h3>
<ul class="list-group">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Morbi leo risus</li>
<li class="list-group-item">Porta ac consectetur ac</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
<hr>
</div>
<!--?php if($_SESSION[ 'stafflvl']=='administrator' || $_SESSION[ 'stafflvl']=='superadministrator'
){ ?-->
<div class="col-md-4 text-left">
<h3>
<i class="fa fa-fw fa-lg fa-money text-orange"></i>Finance & Invoicing :</h3>
<ul class="list-group">
<?php
$uninvoicedq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE completed = '1' AND invoiced = '0' AND wscid !='0' ORDER BY workstartdate ASC");
$uninvoiced = mysqli_fetch_assoc($uninvoicedq);
if($uninvoiced) {
do {
// User Query
$wscid = $uninvoiced['wscid'];
$userq = mysqli_query($cona,"SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
$wtbdq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE wsjid = '$uninvoiced[wsjid]'");
$wtbdr = mysqli_fetch_assoc($wtbdq);
do {
$price = '';
$wtbd = explode(":",$wtbdr['worktobedone']);
foreach($wtbd as $item)
{
$priceq = mysqli_query($con,"SELECT * FROM `workshop-items` WHERE wsiid = '$item'");
$pricer = mysqli_fetch_assoc($priceq);
$price[] = $pricer['incvat'];
$items[] = $pricer['description'];
//echo $item.' - '. $pricer['incvat'].'<br>';
$total = array_sum($price);
}
} while($wtbdr = mysqli_fetch_assoc($wtbdq));
?>
<?
$itemList = implode(":",$items);
$itemview = str_replace(":","\n",$itemList);
?>
<li class="list-group-item text-right" style="border:none;" title="<?php echo $itemview;?>"><span class="badge pull-left" style="background-color:#F00;">Not Invoiced</span><?php echo '£'.$total.' - '; echo $user['forename'].' '.$user['surname'].' - ' .$uninvoiced['summary'];?> </li>
<? } while($uninvoiced = mysqli_fetch_assoc($uninvoicedq));} else { echo "No Jobs Waiting To Invoiced";}?>
</ul>
<hr>
</div>
<?php //echo $_SESSION[ 'admin'];?-->?>
</div>
</div>
</div>
</body></html>
I have looked over all of my code but i am pulling my hair out now..
could really do with a fresh pair of eyes having a gander please
<?php
session_start();
include('config.php');
include('funcs.php');
date_default_timezone_set('Europe/London');
if (!in_array($_SESSION['userid'], $adminusr)) {
header("Location: index.html");
}
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"
type="text/css">
<link href="css.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top"
style="height:55px; background-color:#FFFFFF; border-bottom:#FF6600 2px solid;">
<div class="container">
<div class="navbar-header" style="height:60px;">
<button type="button" class="navbar-toggle hidden-xs" data-toggle="collapse"
data-target="#navbar-ex-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" style="padding-top:5px;"><img height="45" alt="Brand" src="logo.jpg"></a>
</div>
<div class="collapse navbar-collapse hidden-xs hidden-sm" id="navbar-ex-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
Jobs
</li>
<li>
Servicing
</li>
<li>
Prep
</li>
<li>
Finances
</li>
<li>
+ New Job
</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div class="hidden-lg hidden-md hidden-sm">
<ul class="nav nav-pills nav-justified">
<li>
Jobs
</li>
<li>
Servicing
</li>
<li>
Prep
</li>
<li>
Finances
</li>
<li>
+ New Job
</li>
</ul>
</div>
</div>
<div class="section text-justify" style="padding-bottom:0px;">
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<form role="form">
<div class="form-group">
<div class="input-group input-group-sm">
<input type="text" class="form-control" placeholder="Search for something ...">
<span class="input-group-btn">
<a class="btn btn-link" type="submit"><i class="fa fa-lg fa-search"></i></a>
</span>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="section text-center">
<div class="container">
<div class="row">
<div class="col-md-4 text-left">
<h3>
<i class="fa fa-cogs fa-fw fa-lg text-orange"></i>Current Jobs :</h3>
<?php
// List Jobs With Closest Req. By Date
$curdate = date('Y-m-d');
$listjobsq = mysqli_query($con, "SELECT * FROM `workshop-jobs` WHERE status != 'Completed' AND completed ='0' ORDER BY reqbydate ASC LIMIT 0,10");
$listjobs = mysqli_fetch_assoc($listjobsq);
?>
<ul class="list-group" style="border:none;">
<?php do {
// Job Time Frame Check
$cur = strtotime($curdate);
$req = strtotime($listjobs['reqbydate']);
$diff = floor(abs($req - $cur) / 86400);
$diffdays = ' Days';
$diffbg = 'background-color:#1EBB1B; color:#000000;';
if ($req < $cur) {
$diff = 'OVERDUE';
$diffdays = '';
$diffbg = 'background-color:#FF0000; color:#FFFFFF;';
}
if ($req == $cur) {
$diff = 'TODAY';
$diffdays = '';
$diffbg = 'background-color:#F8C000; color:#000000;';
}
if ($diff == 1) {
$diff = 'TOMORROW';
$diffdays = '';
$diffbg = 'background-color:#F8C000; color:#000000;';
}
if ($diff >= 2) {
if ($diff <= 7) {
$diffdays = ' Days';
$diffbg = 'background-color:#F8C000; color:#000000;';
}
}
// End
// User Query
$wscid = $listjobs['wscid'];
$userq = mysqli_query($cona, "SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
?>
<li class="list-group-item text-right" style="border:none;"><span class="badge pull-left"
style="<?php echo $diffbg; ?>"><?php echo $diff . $diffdays; ?></span><a
href="job.php?wsjid=<?php echo $listjobs['wsjid']; ?>"
style="color:#000;"><?php echo $user['forename'] . ' ' . $user['surname'] . ' - ' . $listjobs['summary']; ?></a>
</li>
<?php } while ($listjobs = mysqli_fetch_assoc($listjobsq)); ?>
<li class="list-group-item text-center" style="border:none;"><a href="viewjobs.php"
style="color:#000; font-size:14px; font-weight:bold;">
-- View All Jobs -- </a></li>
</ul>
<hr>
</div>
<div class="col-md-4 text-left">
<h3>
<i class="fa fa-fw fa-lg fa-wrench text-orange"></i>Services / Work Due :</h3>
<ul class="list-group">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Morbi leo risus</li>
<li class="list-group-item">Porta ac consectetur ac</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
<hr>
</div>
<!--?php if($_SESSION[ 'stafflvl']=='administrator' || $_SESSION[ 'stafflvl']=='superadministrator'
){ ?-->
<div class="col-md-4 text-left">
<h3>
<i class="fa fa-fw fa-lg fa-money text-orange"></i>Finance & Invoicing :</h3>
<ul class="list-group">
<?php
$uninvoicedq = mysqli_query($con, "SELECT * FROM `workshop-jobs` WHERE completed = '1' AND invoiced = '0' AND wscid !='0' ORDER BY workstartdate ASC");
$uninvoiced = mysqli_fetch_assoc($uninvoicedq);
if ($uninvoiced) {
do {
// User Query
$wscid = $uninvoiced['wscid'];
$userq = mysqli_query($cona, "SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
$wtbdq = mysqli_query($con, "SELECT * FROM `workshop-jobs` WHERE wsjid = '$uninvoiced[wsjid]'");
$wtbdr = mysqli_fetch_assoc($wtbdq);
do {
$price = '';
$wtbd = explode(":", $wtbdr['worktobedone']);
foreach ($wtbd as $item) {
$priceq = mysqli_query($con, "SELECT * FROM `workshop-items` WHERE wsiid = '$item'");
$pricer = mysqli_fetch_assoc($priceq);
$price[] = $pricer['incvat'];
$items[] = $pricer['description'];
//echo $item.' - '. $pricer['incvat'].'<br>';
$total = array_sum($price);
}
} while ($wtbdr = mysqli_fetch_assoc($wtbdq));
?>
<?php
$itemList = implode(":", $items);
$itemview = str_replace(":", "\n", $itemList);
?>
<li class="list-group-item text-right" style="border:none;"
title="<?php echo $itemview; ?>"><span class="badge pull-left"
style="background-color:#F00;">Not Invoiced</span><?php echo '£' . $total . ' - ';
echo $user['forename'] . ' ' . $user['surname'] . ' - ' . $uninvoiced['summary']; ?>
</li>
<?php } while ($uninvoiced = mysqli_fetch_assoc($uninvoicedq));
} else {
echo "No Jobs Waiting To Invoiced";
} ?>
</ul>
<hr>
</div>
<?php //echo $_SESSION[ 'admin'];?-->?>
</div>
</div>
</div>
</body>
</html>
try this once
tou are missing a } at end. make sure that you ended if($uninvoiced) { on line 152.
I am developing an application in which i need to send notification to all connected clients in real time. I have used nodejs for real time notification send to connected clients.
Problem is this is working fine on server, While connected clients are not able to display popup notification. While total numbers of notification is increasing as per notification inserted to database on server and clients also. Only problem no popup notification.
code : index.php
<?php
include_once( dirname( __FILE__ ) . '/class/Database.class.php' );
include_once( dirname( __FILE__ ) . '/class/HelperClass.php' );
$pdo = Database::getInstance()->getPdoObject();
$helper = new HelperClass();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>VegFru | Notification</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="fonts/css/font-awesome.min.css" rel="stylesheet">
<link href="css/animate.min.css" rel="stylesheet">
<link href="css/pnotify.custom.min.css" rel="stylesheet">
<!-- Custom styling plus plugins -->
<link href="css/custom.css" rel="stylesheet">
<script src="js/jquery.min.js"></script>
</head>
<body class="nav-md">
<div class="container body">
<div class="main_container">
<div class="col-md-3 left_col">
<div class="left_col scroll-view">
<div class="navbar nav_title" style="border: 0;">
<i class="fa fa-paw"></i> <span>VegFru</span>
</div>
<div class="clearfix"></div>
<!-- menu prile quick info -->
<div class="profile">
<div class="profile_pic">
<img src="images/img.jpg" alt="..." class="img-circle profile_img">
</div>
<div class="profile_info">
<span>Welcome,</span>
<h2>Ashish Singh</h2>
</div>
</div>
<!-- /menu prile quick info -->
<br />
<!-- sidebar menu -->
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<ul class="nav side-menu">
<li>Push Notification <i class="fa fa-dashboard pull-right"></i></li>
<li>
<form action="ajax/insertNewMessage.php" id="notification">
<div class="form-group" style="margin-left: 16px; width: 83%">
<input type="checkbox" onclick="client.gerateRandomNotification(this)"/>
<label class="control-label" for="notification-input">Generate Random</label>
</div>
<div class="form-group" style="margin-left: 16px; width: 83%">
<label class="control-label" for="notification-input">Notification Message</label>
<input type="text" id="notification-input" class="form-control" placeholder="Notification Message"><br/>
</div>
<button type="submit" class="btn btn-success" style="margin-left: 16px;">Send Push Notification</button>
</form>
</li>
</ul>
</div>
</div>
<!-- /sidebar menu -->
</div>
</div>
<!-- top navigation -->
<div class="top_nav">
<div class="nav_menu">
<nav class="" role="navigation">
<!-- <div class="nav toggle">
<a id="menu_toggle"><i class="fa fa-bars"></i></a>
</div>
-->
<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<img src="images/img.jpg" alt="">Ashish Singh
</a>
</li>
<li role="presentation" class="dropdown">
<a href="javascript:;" class="dropdown-toggle info-number" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-envelope-o"></i>
<span class="badge bg-green"></span>
</a>
</li>
<li role="presentation" class="dropdown">
<?php
$query = $pdo->prepare( 'SELECT * FROM notification WHERE status = 0 ORDER BY id DESC' );
$query->execute();
$notifications = $query->fetchAll( PDO::FETCH_OBJ );
?>
<a href="javascript:;" class="dropdown-toggle info-number" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-bell-o"></i>
<span class="badge bg-green"><?= count($notifications) > 0 ? count($notifications): '' ?></span>
</a>
<ul class="dropdown-menu list-unstyled msg_list animated fadeInDown" style="right: -120px !important; border-top-left-radius: 7px; border-top-right-radius: 7px; margin-top: 10px;" role="menu">
<li style="background-color: white">
<div class="text-center">
<a>
<strong><a href="inbox.html">Notification (<?= count($notifications) ?>)</strong>
</a>
</div>
</li>
<div id="notifications-container">
<?php foreach( $notifications as $notification):?>
<li data-id="<?= $notification->id ?>" onclick="client.openNotification(this)">
<a>
<span class="image">
<img src="images/img.jpg" alt="Profile Image" />
</span>
<span>
<span>Ashish Singh</span>
<span class="time"><?php echo $helper->time_elapsed_string($notification->created_at) ?></span>
</span>
<span class="message">
<?php echo $notification->notification ?>
</span>
</a>
</li>
<?php endforeach; ?>
</div>
<li>
<div class="text-center">
<a>
<strong><a href="inbox.html">See All Alerts</strong>
<i class="fa fa-angle-right"></i>
</a>
</div>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
<!-- /top navigation -->
</div>
</div>
<script src="js/bootstrap.min.js"></script>
<script src="js/pnotify.custom.min.js"></script>
<script src="js/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
<script src="js/nodeClient.js"></script>
</body>
</html>
nodeserver.js
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
console.log( "New client" );
client.on( 'notification', function( data ) {
console.log( 'Notification received ' + data.notification);
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.sockets.emit( 'notification', { notification: data.notification, serverData: data.serverData } );
});
});
server.listen( 8082 );
nodeclient.js
var client = (function () {
var socket = io.connect('http://192.168.2.8:8082');
var interval; //for random notification generation
function init() {
buttonClicked();
socketNotificationUpdate();
}
function buttonClicked() {
$("#notification").on('submit', function (e) {
e.preventDefault(); //Preventing default submit action
var $cacheDom = $(this); //Caching the dom so that jQuery doesn't always need to search in the page
var url = $cacheDom.attr('action'); //Grabbing url of the form
var notification = $cacheDom.find('div:nth-child(2) input').val();
$cacheDom.find('div').removeClass('has-error').addClass('has-success').val('');
if (notification.length == 0) {
$cacheDom.find('div').removeClass('has-success').addClass('has-error');
return false;
}
//Ajax call to save data
saveAjaxNotification(notification, url);
});
}
function socketNotificationUpdate() {
socket.on('notification', function (data) {
var $cacheDom = $('#notifications-container');
var actualContent = $cacheDom.html(); //All notification
var $notificationCount = $cacheDom.parent().prev().find('span'); //Header notification count selector
var notificationCount = $notificationCount.text(); //Header Notification count
$notificationCount.text(Number(notificationCount) + 1); //Incrementing after one notification
var newMsgContent = '<li data-id="' + data.serverData.lastInsertId + '" onclick="client.openNotification(this)"><a><span class="image"><img src="images/img.jpg" alt="Profile Image" /></span><span><span>Ashish Singh</span><span class="time">0 seconds ago</span></span><span class="message">' + data.notification + '</span></a></li>';
$cacheDom.html(newMsgContent + actualContent);
});
}
function openNotification(that) {
var notificationId = $(that).attr('data-id');
$.post('./ajax/readNotification.php', {notification_id: notificationId}, function () {
$(that).remove(); //removing the clicked notification (optional as it should take the user to new page)
var $notificationCount = $('#notifications-container').parent().prev().find('span');
var notificationCount = $notificationCount.text();
$notificationCount.text(Number(notificationCount) - 1);
alert('Notification Link will be opened'); //Just for message
location.href = ""; //Reloading the page as on clicking the notification will take the user to notifications page where all the notification will be shown (read or unread)
});
}
function saveAjaxNotification(notification, url) {
$.ajax({
url: url,
type: "POST",
data: {notification: notification},
success: function (data) {
var serverData = JSON.parse(data);
socket.emit('notification', {notification: notification, serverData: serverData});
PNotify.desktop.permission();
(new PNotify({
title: 'Ashish Singh',
text: notification,
desktop: {
desktop: true,
icon: 'images/img.jpg'
}
})).get().click(function (e) {
if ($('.ui-pnotify-closer, .ui-pnotify-sticker, .ui-pnotify-closer *, .ui-pnotify-sticker *').is(e.target))
return;
alert('http://vegfru.com/imrealashu/notification/' + serverData.link);
});
}
});
}
function gerateRandomNotification(that) {
var url = $(that).parent().parent().attr('action');
var count = 1;
if ($(that).is(':checked')) {
interval = setInterval(function () {
saveAjaxNotification('notification' + count, url);
count++
}, 3000);
} else {
clearInterval(interval);
}
}
init();
return {
openNotification: openNotification, //Revealing the openNotification function as it will be used in DOM
gerateRandomNotification: gerateRandomNotification
}
})();
How do I send to multiple recipients in php?
I only have one input field for the user to input all the recipients. The code already has a javascript which creates tags and autofill. The code only sends to the first input.
Here is the code:
<?php
error_reporting(0);
session_start();
include_once 'config.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM user WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$deptm = mysql_query("SELECT * FROM departments WHERE dep_ID = ".$userRow['department_id']."");
$deptment = mysql_fetch_array($deptm);
if(isset($_POST['send-btn']))
{
$recipient = $_POST['TO'];
$check = mysql_query("SELECT * FROM user WHERE faculty_number='$recipient'");
$rec = mysql_fetch_array($check);
if($userRow['position'] == 'Department Head')
{
if($rec['position'] != 'Dean' && $rec['position'] != 'Faculty Secretary')
{
$subject = $_POST['subject'];
$message = $_POST['message'];
$indicator = $_POST['indicator'];
$counter = $_POST['typ_cnt'];
$date = date('M-j');
/*$cnt=array();
$cnt=count($_POST['department']);
for($i=0;$i<$cnt;$i++)
{
$recipient=$_POST['department'][$i];*/
if($del=mysql_query("INSERT INTO memo(memo_date, iso, memo_subject, content, memo_sender) VALUES ('$date', 'UST:".$indicator." - ".$counter."/AY 2015-2016', '$subject','$message','".$userRow['faculty_number']."')"))
{
$getID = mysql_query("SELECT * FROM memo ORDER BY memo_ID DESC LIMIT 1");
$id = mysql_fetch_array($getID);
if($recipient=mysql_query("INSERT INTO recipients(id,type,iso,memo_sender,recipient_num,read_status,read_timestamp,acknowledgement,ack_timestamp,delete_status) VALUES ('".$id['memo_ID']."','memo','UST:".$indicator." - ".$counter."/AY 2015-2016','".$userRow['faculty_number']."', '$recipient', 'unread', '---', 'Not Acknowledged', '---', 'active')"))
{
?>
<script>alert('Memo Sent');</script>
<?php
}
else{
?>
<script>alert('Failed');</script>
<?php
}
}
else{
?>
<script>alert('Failed');</script>
<?php
}
//}
}else{
?>
<script>alert('Your recipient is not allowed');</script>
<?php
}
}else if($userRow['position'] == 'Dean')
{
$subject = $_POST['subject'];
$message = $_POST['message'];
$indicator = $_POST['indicator'];
$counter = $_POST['typ_cnt'];
$date = date('M-j');
/*$cnt=array();
$cnt=count($_POST['department']);
for($i=0;$i<$cnt;$i++)
{
$recipient=$_POST['department'][$i];*/
if($del=mysql_query("INSERT INTO memo(memo_date, iso, memo_subject, content, memo_sender) VALUES ('$date', 'UST:".$indicator." - ".$counter."/AY 2015-2016', '$subject','$message','".$userRow['faculty_number']."')"))
{
$getID = mysql_query("SELECT * FROM memo ORDER BY memo_ID DESC LIMIT 1");
$id = mysql_fetch_array($getID);
if($recipient=mysql_query("INSERT INTO recipients(id,type,iso,memo_sender,recipient_num,read_status,read_timestamp,acknowledgement,ack_timestamp,delete_status) VALUES ('".$id['memo_ID']."','memo','UST:".$indicator." - ".$counter."/AY 2015-2016','".$userRow['faculty_number']."', '$recipient', 'unread', '---', 'Not Acknowledged', '---', 'active')"))
{
?>
<script>alert('Memo Sent');</script>
<?php
}
else{
?>
<script>alert('Failed');</script>
<?php
}
}
else{
?>
<script>alert('Failed');</script>
<?php
}
//}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="" name="description">
<meta content="" name="author">
<title>Welcome - <?php echo $userRow['position']; ?></title>
<!-- Bootstrap Core CSS -->
<link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel=
"stylesheet"><!-- MetisMenu CSS -->
<link href="bower_components/metisMenu/dist/metisMenu.min.css" rel=
"stylesheet"><!-- Timeline CSS -->
<link href="dist/css/timeline.css" rel="stylesheet"><!-- Custom CSS -->
<link href="dist/css/sb-admin-2.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="bower_components/morrisjs/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="bower_components/font-awesome/css/font-awesome.min.css" rel=
"stylesheet" type="text/css">
<!-- Bootstrap styling for Typeahead -->
<link href="dist/css/tokenfield-typeahead.css" type="text/css" rel="stylesheet">
<!-- Tokenfield CSS -->
<link href="dist/css/bootstrap-tokenfield.css" type="text/css" rel="stylesheet">
<!-- Docs CSS -->
<link href="docs-assets/css/pygments-manni.css" type="text/css" rel="stylesheet">
<link href="docs-assets/css/docs.css" type="text/css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="sendmemo.js" type="text/javascript"></script>
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('department[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
</head>
<body>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" style=
"margin-bottom: 0">
<div class="navbar-header">
<button class="navbar-toggle" data-target=".navbar-collapse"
data-toggle="collapse" type="button"><span class=
"sr-only">Toggle navigation</span> <span class=
"icon-bar"></span> <span class="icon-bar"></span> <span class=
"icon-bar"></span></button> <a class="navbar-brand" href=
"index.html">UNIVERSITY OF SANTO TOMAS - FACULTY OF MEDICINE
DOCUMENT MANAGEMENT SYSTEM</a>
</div>
<!-- /.navbar-header -->
<ul class="nav navbar-top-links navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href=
"#"><i class="fa fa-bell fa-fw"></i> <i class=
"fa fa-caret-down"></i></a>
<ul class="dropdown-menu dropdown-alerts">
<li>
<a href="#">
<div>
<i class="fa fa-envelope-o fa-fw"></i> New
Message <span class=
"pull-right text-muted small">4 minutes
ago</span>
</div></a>
</li>
<li class="divider">
</li>
<li>
<a href="#">
<div>
<i class="fa fa-envelope fa-fw"></i> New
Message <span class=
"pull-right text-muted small">12 minutes
ago</span>
</div></a>
</li>
<li class="divider">
</li>
<li>
<a href="#">
<div>
<i class="fa fa-envelope fa-fw"></i> New
Message <span class=
"pull-right text-muted small">4 minutes
ago</span>
</div></a>
</li>
<li class="divider">
</li>
<li>
<a href="#">
<div>
<i class="fa fa-tasks fa-fw"></i> New Message
<span class="pull-right text-muted small">4
minutes ago</span>
</div></a>
</li>
<li class="divider">
</li>
<li>
<a href="#">
<div>
<i class="fa fa-upload fa-fw"></i> Server
Rebooted <span class=
"pull-right text-muted small">4 minutes
ago</span>
</div></a>
</li>
<li class="divider">
</li>
<li>
<a class="text-center" href="#"><strong>See All
Notifications</strong> <i class=
"fa fa-angle-right"></i></a>
</li>
</ul>
<!-- /.dropdown-alerts -->
</li>
<!-- /.dropdown -->
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href=
""><i class="fa fa-user"></i>
<?php echo $userRow['first_name'];?> <?php echo $userRow['middle_name'];?> <?php echo $userRow['last_name'];?>
<b class="caret"></b></a>
<ul class="dropdown-menu">
<li>
<a><i class="fa fa-fw fa-user"></i>
<?php echo $userRow['position'];?></a>
</li>
<li class="divider">
</li>
<li>
<a href="changepass.php"><i class=
"fa fa-fw fa-wrench"></i> Change Password</a>
</li>
<li class="divider">
</li>
<li>
<a href="logout.php?logout"><i class=
"fa fa-fw fa-power-off"></i> Log Out</a>
</li>
</ul>
</li>
<!-- /.dropdown -->
</ul>
<div class="navbar-default sidebar">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li>
<a href="<?php echo $_SESSION['home']?>"><i class=
"fa fa-home fa-fw"></i> Home</a>
</li>
<li>
<a href="sendmemo.php"><i class=
"fa fa-envelope-o fa-fw"></i> Send Memo</a>
</li>
<li>
<a href="index.html"><i class=
"fa fa-bar-chart-o fa-fw"></i> Reports<span class=
"fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="reports.php">List of
Acknowledgement</a>
</li>
<li>
<a href="index.html">Semestral Compliance
Report</a>
</li>
</ul>
<!-- /.nav-second-level -->
</li>
<li>
<a href="calendar.php"><i class=
"fa fa-table fa-fw"></i> Calendar</a>
</li>
<li>
<a href="inbox.php"><i class=
"fa fa-inbox fa-fw"></i> Inbox</a>
</li>
<li>
<a href="sent.php"><i class=
"fa fa-send-o fa-fw"></i> Sent</a>
</li>
<li>
<a href="allfiles.php"><i class=
"fa fa-files-o fa-fw"></i> All Files</a>
</li>
<?php
if($userRow['position'] = 'Dean' && $userRow['position'] = 'Faculty Secretary')
{ ?>
<li>
<i class="fa fa-folder fa-fw"></i> Folders
</li>
<?php
}else if($userRow['position'] = 'Department Head')
{ ?>
<li>
<i class="fa fa-folder fa-fw"></i> Folders
</li>
<?php
}else if($userRow['position'] = 'Faculty')
{ ?>
<li>
<i class="fa fa-folder fa-fw"></i> Folders
</li>
<?php
}
?>
<li>
<a href="upload.php"><i class=
"fa fa-upload fa-fw"></i> Upload Document</a>
</li>
</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</nav>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h2 class="text-center">Send Memo</h2>
<ul class="nav nav-tabs nav-justified">
<li class="active"><a data-toggle="tab" href="#memo">Memo</a></li>
<li><a data-toggle="tab" href="#letter">Letter</a></li>
</ul>
<?php
switch($userRow['position'])
{
case 'Dean': $ind="A001-001";
break;
case 'Faculty Secretary': $ind="A002-002";
break;
case 'Department Head': $ind="A003-003";
break;
}
$result=mysql_query("SELECT count(*) as total from recipients WHERE memo_sender=".$userRow['faculty_number']);
$cntr=mysql_fetch_assoc($result);
$cnt = $cntr['total']+1;
$curYr = date('Y');
?>
<div class="tab-content">
<div id="memo" class="tab-pane fade in active">
<form method="post">
<div class="form-inline" role="form" ><br>
<label>ISO:</label>
<div class="form-group">
<label>UST:</label>
<input class="form-control" name="indicator" value="<?php echo $ind;?>" style="width:90px;"
type="text">
</div>
<div class="form-group">
<label>-</label>
<input type="text" class="form-control" name="typ_cnt" value="ME<?php echo $cnt;?>" style="width:60px;">
<label>/AY <?php echo $curYr;?> - <?php echo date('Y',strtotime('+1 year'));?> </label>
</div>
</div><br>
<div class="form-group">
<label>TO:</label>
<div class="input-group">
<input name="TO" type="textbox" class="form-control" id="tokenfield-typeahead" value="
<?php
if(isset($_POST['select-btn']))
{
$cnt=array();
$cnt=count($_POST['department']);
for($i=0;$i<$cnt;$i++)
{
$del_id=$_POST['department'][$i];
echo $del_id.",";
}
}
?> "/>
<span class="input-group-btn">
<button class="btn btn-warning" type="button" value="Departments" data-toggle="modal" data-target="#myModal">Department</button>
</span>
</div>
</div>
<div class="form-group">
<label for="subj">SUBJECT:</label>
<input class="form-control" name="subject"
type="text">
</div>
<br>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" rows="10"></textarea>
</div>
<button type="submit" name="send-btn" class="btn btn-warning">Send</button>
</form>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Departments</h4>
</div>
<div class="modal-body">
<form method="post" action="sendmemo.php">
<div class="form-group">
<?php
$deptlist = mysql_query("SELECT * FROM departments");
while($dept = mysql_fetch_array($deptlist))
{
echo "<div class='checkbox'>";
echo "<label>";
echo "<input type='checkbox' name='department[]' value='".$dept['department_name']."' >" . $dept['department_name'];
echo "</label>";
echo "</div>";
}
?>
</div>
</div>
<div class="modal-footer">
<button type="submit" name="select-btn" class="btn btn-warning">Select</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div id="letter" class="tab-pane fade">
<form method="post">
<div class="form-inline" role="form" ><br>
<label>ISO:</label>
<div class="form-group">
<label>UST:</label>
<input class="form-control" name="indicator" value="<?php echo $ind;?>" style="width:90px;"
type="text">
</div>
<div class="form-group">
<label>-</label>
<input type="text" class="form-control" name="typ_cnt" value="ME<?php echo $cnt;?>" style="width:60px;">
<label>/AY <?php echo $curYr;?> - <?php echo date('Y',strtotime('+1 year'));?> </label>
</div>
</div><br>
<div class="form-group">
<label>TO:</label>
<div class="input-group">
<input name="TO" type="textbox" class="form-control">
<span class="input-group-btn">
<button class="btn btn-warning" type="button" value="Departments" data-toggle="modal" data-target="#myModal">Department</button>
</span>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Departments</h4>
</div>
<div class="modal-body">
<div class="form-group">
<?php
$deptlist = mysql_query("SELECT * FROM departments");
while($dept = mysql_fetch_array($deptlist))
{
$dept_head = mysql_query("SELECT faculty_number FROM user WHERE position='Department Head' AND department_id='".$dept['dep_ID']."'");
echo "<div class='checkbox'>";
echo "<label>";
echo "<input type='checkbox' name='department[]' value='$dept_head' >" . $dept['department_name'];
echo "</label>";
echo "</div>";
}
?>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-warning" data-dismiss="modal">Select</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="subj">SUBJECT:</label>
<input class="form-control" name="subject" type="text">
</div>
<br>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" rows="10"></textarea>
</div>
<button type="submit" name="send-btn" class="btn btn-danger">Send</button>
</form>
</div>
</div>
</div>
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="bower_components/jquery/dist/jquery.min.js"></script> <!-- Bootstrap Core JavaScript -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script> <!-- Metis Menu Plugin JavaScript -->
<script src="bower_components/metisMenu/dist/metisMenu.min.js"></script> <!-- Custom Theme JavaScript -->
<script src="dist/js/sb-admin-2.js"></script>
<script type="text/javascript" src="jquery-ui/jquery-1.9.1.js"></script>
<script type="text/javascript" src="jquery-ui/jquery-ui.js"></script>
<script type="text/javascript" src="dist/bootstrap-tokenfield.js" charset="UTF-8"></script>
<script type="text/javascript" src="docs-assets/js/scrollspy.js" charset="UTF-8"></script>
<script type="text/javascript" src="docs-assets/js/affix.js" charset="UTF-8"></script>
<script type="text/javascript" src="docs-assets/js/typeahead.bundle.min.js" charset="UTF-8"></script>
<script>
var engine = new Bloodhound({
<!--local: [{value: 'red'}, {value: 'blue'}, {value: 'green'} , {value: 'yellow'}, {value: 'violet'}, {value: 'brown'}, {value: 'purple'}, {value: 'black'}, {value: 'white'}, {value: 'jerome'}],
local: [<?php $suggest = mysql_query("SELECT * FROM user");
while($info = mysql_fetch_array($suggest))
{
echo "{value: '".$info['faculty_number']." (".$info['last_name'].", ".$info['first_name'].")'},";
}
?>],
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace
});
engine.initialize();
$('#tokenfield-typeahead').tokenfield({
typeahead: [null, { source: engine.ttAdapter() }]
});
</script>
</body>
</html>
Basically, what you should do to send to a couple recipients is:
$message = "Email Body goes here";
$to = $recepient1.",".$recepient2.",".$recepient3;
$subject = 'Some Subject';
$from = $sender_email;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
mail($to, $subject, $message, $headers);
You didn't post your (full) email code, only a part that puts some data in a database.
Look at this post:
PHP form send email to multiple recipients
If you can't implement it yourself, update your question with your full email code so i can do it for you.
Edit:
I implemented this snippet; you have to enter the recipients with a , (comma + space) between them in the old html form field;
<?php
$recipient = "recipienta, recipientb";
$recipients = explode(", ",$recipient);
foreach ($recipients as $recipient) {
echo $recipient;
}
?>
Here is the relevant part of your code with the snippet implemented, i don't have all your dependencies so i couldn't test it but it should work, if it doesn't just let me know your problem and i'll fix it <3;
<?php
error_reporting(0);
session_start();
include_once 'config.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM user WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$deptm = mysql_query("SELECT * FROM departments WHERE dep_ID = ".$userRow['department_id']."");
$deptment = mysql_fetch_array($deptm);
if(isset($_POST['send-btn']))
{
$recipient = $_POST['TO'];
$recipients = explode(", ",$recipient);
foreach ($recipients as $recipient) {
$check = mysql_query("SELECT * FROM user WHERE faculty_number='$recipient'");
$rec = mysql_fetch_array($check);
if($userRow['position'] == 'Department Head')
{
if($rec['position'] != 'Dean' && $rec['position'] != 'Faculty Secretary')
{
$subject = $_POST['subject'];
$message = $_POST['message'];
$indicator = $_POST['indicator'];
$counter = $_POST['typ_cnt'];
$date = date('M-j');
/*$cnt=array();
$cnt=count($_POST['department']);
for($i=0;$i<$cnt;$i++)
{
$recipient=$_POST['department'][$i];*/
if($del=mysql_query("INSERT INTO memo(memo_date, iso, memo_subject, content, memo_sender) VALUES ('$date', 'UST:".$indicator." - ".$counter."/AY 2015-2016', '$subject','$message','".$userRow['faculty_number']."')"))
{
$getID = mysql_query("SELECT * FROM memo ORDER BY memo_ID DESC LIMIT 1");
$id = mysql_fetch_array($getID);
if($recipient=mysql_query("INSERT INTO recipients(id,type,iso,memo_sender,recipient_num,read_status,read_timestamp,acknowledgement,ack_timestamp,delete_status) VALUES ('".$id['memo_ID']."','memo','UST:".$indicator." - ".$counter."/AY 2015-2016','".$userRow['faculty_number']."', '$recipient', 'unread', '---', 'Not Acknowledged', '---', 'active')"))
{
?>
<script>alert('Memo Sent');</script>
<?php
}
else{
?>
<script>alert('Failed');</script>
<?php
}
}
else{
?>
<script>alert('Failed');</script>
<?php
}
//}
}else{
?>
<script>alert('Your recipient is not allowed');</script>
<?php
}
}else if($userRow['position'] == 'Dean')
{
$subject = $_POST['subject'];
$message = $_POST['message'];
$indicator = $_POST['indicator'];
$counter = $_POST['typ_cnt'];
$date = date('M-j');
/*$cnt=array();
$cnt=count($_POST['department']);
for($i=0;$i<$cnt;$i++)
{
$recipient=$_POST['department'][$i];*/
if($del=mysql_query("INSERT INTO memo(memo_date, iso, memo_subject, content, memo_sender) VALUES ('$date', 'UST:".$indicator." - ".$counter."/AY 2015-2016', '$subject','$message','".$userRow['faculty_number']."')"))
{
$getID = mysql_query("SELECT * FROM memo ORDER BY memo_ID DESC LIMIT 1");
$id = mysql_fetch_array($getID);
if($recipient=mysql_query("INSERT INTO recipients(id,type,iso,memo_sender,recipient_num,read_status,read_timestamp,acknowledgement,ack_timestamp,delete_status) VALUES ('".$id['memo_ID']."','memo','UST:".$indicator." - ".$counter."/AY 2015-2016','".$userRow['faculty_number']."', '$recipient', 'unread', '---', 'Not Acknowledged', '---', 'active')"))
{
?>
<script>alert('Memo Sent');</script>
<?php
}
else{
?>
<script>alert('Failed');</script>
<?php
}
}
else{
?>
<script>alert('Failed');</script>
<?php
}
//}
}
}
}
?>