What's wrong with my sessions? - php

When i try to go to the home page, the page becomes blank and it is because of the session tags because when i remove them the page shows.
Home page:
<?php
session_start();
include 'includes/db_connect.php';
if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
header('Location:home.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="styles/home.css" type="text/css" >
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript" src="js/nav.js" ></script>
<script type="text/javascript">
<!--
function toggle_visibility() {
var e = document.getElementById("nav");
if(e.style.display == 'table')
e.style.display = 'none';
else
e.style.display = 'table';
}
//-->
</script>
</head>
<body>
<div class="Menu" >
<div class="middle" >
<div class="profilepic" >
<a href="profile.php" >
<img src="" ></img>
</a>
</div>
<div class="search" >
<form method="POST" action="search.php" >
<input type="search" id="search-input" class="search-input" placeholder="Please enter a search term!" minlength="1" >
</form>
</div>
<p><?php echo '$_SESSION["Username"]'; ?></p>
</div>
<div class="navigation">
<div class="openMenu" id="openMenu" ><button onclick="toggle_visibility();">Menu</button></div>
<ul id="nav" >
<li><a href="profile.php" ><i id="navicon" class="fa fa-user fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-envelope-o fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-bars fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-group fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-rss fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-clock-o fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-edit fa-2x" ></i></a></li>
<li><a href="" ><i id="navicon" class="fa fa-gear fa-spin fa-2x" ></i></a></li>
</ul>
</div>
</div>
<div class="status" >
<form action="" method="GET" >
<div class="upload" >
<input type="file" name="videofilename" accept="video/*" class="upload" />
</div>
<div class="upload">
<input type="file" name="audiofilename" accept="audio/*" class="upload" />
</div>
<div class="upload" >
<input type="file" name="imagefilename" accept="image/*" />
</div>
<textarea class="statusText" id="statusText" rows="1" cols="60" placeholder="Update Your Status..." ></textarea>
<input type="submit" value="Post!" >
</form>
</div>
</body>
</html>
This is my login page where the session is stored:
<?php
session_start();
include 'includes/db_connect.php';
?>
<html>
<head>
<title>Howlers | Login</title>
</head>
<body>
<div class="login" >
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
header('Location=home.php');
?>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysqli::real_escape_string($_POST['username']);
$password = md5(mysqli::real_escape_string($_POST['password']));
$checklogin = mysqli::query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysqli::num_rows($checklogin) == 1)
{
$row = mysqli::fetch_array($checklogin);
$email = $row['Email'];
$_SESSION['Username'] = $username;
$_SESSION['Email'] = $email;
$_SESSION['LoggedIn'] = 1;
header('Location=home.php');
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href='login.php'>click here to try again</a>.</p>";
}
}
else
{
?>
<h1>Login</h1>
<p>Thanks for visiting! Please either login below, or click here to register.</p>
<form method="post" action="login.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="submit" id="submit" value="Login" />
</fieldset>
</form>
<?php
}
?>
</div>
</body>
</html>
here is a picture of the outcome:
http://prntscr.com/5580lh
I am not sure what i am doing wrong here can anyone please help me :(?

one of error is the redirect in the login-page:
you write header('Location=home.php');
The equal is wrong, use the double-point: header('Location:home.php');

You didn't close the if condition proper in home page:
if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
header('Location:home.php');
}
you forgot to close the if condition which was fatal error!

Make following changes in your pages
Home Page
<?php
session_start();
include 'includes/db_connect.php';
if(!isset($_SESSION['LoggedIn']) && !isset($_SESSION['Username']))
{
header('Location:home.php');
}
?> ...
Login Page
...
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
header('Location:home.php');
?>
<?php
}
?>
...
and
...
if(mysqli::num_rows($checklogin) == 1)
{
$row = mysqli::fetch_array($checklogin);
$email = $row['Email'];
$_SESSION['Username'] = $username;
$_SESSION['Email'] = $email;
$_SESSION['LoggedIn'] = 1;
header('Location:home.php');
}
...

Related

Data no inserted to database

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']?>">

localhost redirected you too many times. ERR_TOO_MANY_REDIRECTS

I keep getting this error since last night for my index.php page only. i have cleared all my cookies but still i cant access index.php on local host. i have looked at other solutions but couldn't find an answer.
I think it is to do with my index.php code which redirects me to the same page to login. but i am new to web development i am not sure how to tackle the issue. below i have provided snippet of my index.php and login.php. any help is highly appreciated.
login.php
<?php
session_start();
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'ecom';
// Try and connect using the info above.
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
die ('Please fill both the username and password field!');
}
if ($stmt = $con->prepare('SELECT id, password FROM users WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
}
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// password_hash function in is used in registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
// Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
/*echo 'Welcome ' . $_SESSION['name'] . '!';*/
header('Location: index.php');
} else {
echo 'Incorrect password!';
}
} else {
echo 'Incorrect username!';
}
$stmt->close();
?>
index.php
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location:index.php');
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<title>City Outlet</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link href="css/style.css?10" rel="stylesheet" type="text/css">
<!-- awesome-font CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<!--========== Top bar Area ============-->
<header class="topbar">
<nav>
<a style="color:white; font-size:18" href="logout.php"><i class="fa fa-sign-out style="font-size:24px; color:white;""></i>Logout</a>
<button href="" class="btn login btn-default d-inline-flex" data-toggle="modal" data-target="#loginModal" ><i class="fa fa-sign-in" style="font-size:24px" value="login" onClick="showModal()" ></i> Login</button>
<button href="" class="btn login btn-default d-inline-flex" data-toggle="modal" data-target="#signupModal"><i class="fa fa-user" style="font-size:24px" ></i> Sign Up</button>
</nav>
<p style="color:white; font-weight:bold; text-align:right; line-height:-10px; margin-top: 7px;"> Welcome: <strong style="color:red; font-weight:bold;"><?=$_SESSION['name']?></strong>! <img src="images/ava.png" style=width:25px; height="25px;"> </p>
</header>
<!-- Login Modal -->
<div class="loginbox" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true"> <img class="avatar" src="images/redavatar.png">
<h1>Sign in</h1>
<form action="login.php" method="post">
<p>Username</p>
<input type="text" name="username" placeholder="Enter username" required>
<p>Password</p>
<input type="password" name="password" placeholder="Enter password" required>
<input type="submit" name="submit" value="submit">
Not a member? <br>
<br>
</form>
</div>
<!----- Sign up Modal ----->
<div class="container">
<h1 class="text-white bg-dark text-center"></h1>
<div class="modal" id="signupModal">
<div class="modal-dialog ">
<div class= "modal-content">
<div class="modal-header">
<h3 >Sign Up</h3>
<button style="color:red" type="button" class="close" data-dismiss="modal" > ×</button>
</div>
<div class="modal-body">
<form action="signup.php" method="post" >
<div class="form-group">
<label><i class="fa fa-user fa-2x"></i> First name:
</label>
<input type="text" name="username" class="form-control" placeholder="your name" required>
</div>
<div class="form-group">
<label><i class="fa fa-lock fa-2x"></i> Password:
</label>
<input type="password" name="password" class="form-control" placeholder="choose password" required>
</div>
<div class="form-group">
<label><i class="fa fa-envelope fa-2x"></i> Email:
</label>
<input type="text" name="email" class="form-control" placeholder="Your email" required>
</div>
<input type="submit">
</form>
</div>
</div>
</div>
</div>
</div>
<!----- Sign up Modal ----->
<!--========== End Top bar Area ============-->
<nav class="navbar fixed-top navbar-expand-md navbar-light ">
<div class="container-fluid"> <a class="navbar-brand " href="index.php"> <img src="images/citylogo.png" alt=""> </a>
<button class="navbar-toggler third-button darken-3" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<div class="animated-icon3"><span></span><span></span><span></span></div>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<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"> <a class="nav-link" href="#">Services</a> </li>
<li class="nav-item"> <a class="nav-link" href="shop.php">Shop</a> </li>
<li class="nav-item"> <a class="nav-link" href="booking.php">Booking</a> </li>
<li class="nav-item"> <a class="nav-link" href="gallery.html">Gallery</a> </li>
<li class="nav-item"> <a class="nav-link" href="contact.html">Contact</a> </li>
</ul>
<form class="searchbar" action="/action_page.php" style="margin:auto;max-width:300px">
<button type="submit"><i class="fa fa-search"></i></button>
<input type="text" placeholder="Search.." name="search2">
</form>
</div>
</div>
</nav>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script>
function showModal()
{
document.getElementById("loginModal").style.display = 'block';
}
</script>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
On index.php
This part of the code.
if (!isset($_SESSION['loggedin'])) {
header('Location:index.php'); // redirecting to same page.
exit();
}
You are redirecting to the same page if the user is not log in, thats why you have the infinite redirects... You must redirect to other page like a login page.

how to Fix undefined variable rows in my code

hi i am write code for edit data in php but some error are find at this time please fix.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chemistry_web";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_GET['ID'])){
$id=$_GET['ID'];
$sql="SELECT * FROM research_tab WHERE ID='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);}
//include_once 'connection.php';
// if (isset($_GET['ID'])){
// $id=$_GET['ID'];
// $query = "SELECT * FROM $tbl_name WHERE id='$id'";
// $result = mysql_query($query);
// $row = mysql_fetch_array($result);
// }
?>
<!DOCTYPE html>
<html>
Why give warning Undefined variable rows when i am defined variable in top of code.
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Education</title>
<link href="assets/plugins/bootstrap/bootstrap.css" rel="stylesheet" />
<link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
<link href="assets/css/style.css" rel="stylesheet" />
<link href="assets/css/main-style.css" rel="stylesheet" />
<link href="assets/plugins/morris/morris-0.4.3.min.css" rel="stylesheet" />
</head>
<body>
<div id="wrapper">
<nav class="navbar navbar-default navbar-fixed-top" role="navigation" id="navbar">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-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" href="dashboard.html">
<img src="assets/img/logo.png" alt="Chemistry"/>
</a>
</div>
<ul class="nav navbar-top-links navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-3x"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><i class="fa fa-sign-out fa-fw"></i>Logout
</li>
</ul>
</li>
</ul>
</nav>
<nav class="navbar-default navbar-static-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="side-menu">
<li class="sidebar-search">
</li>
<li class="selected">
<i class="fa fa-dashboard fa-fw"></i>Dashboard
</li>
<li>
<i class="fa fa-files-o fa-fw"></i>Home Page Settings<span class="fa arrow"></span>
<ul class="nav nav-second-level">
<li>
Research Settings
</li>
</ul>
</li>
</ul>
</div>
</nav>
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Research Settings</h1>
</div>
</div>
<form action="#" method="post" >
<div class="form-group">
<label>Research Title</label>
<input type="text" id="title" name="newTitle" class="form-control" placeholder="Enter title" value="<?php echo $rows[1]; ?>"/>
<input type="hidden" id="id" name="id" class="form-control" placeholder="Enter title" value="<? echo $rows[0]; ?>"/>
</div>
<div class="form-group">
<label>Description</label>
<textarea id="description" name="description" class="form-control" rows="3"></textarea>
</div>
<button id="submit" name="submit" type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
<script src="assets/plugins/jquery-1.10.2.js"></script>
<script src="assets/plugins/bootstrap/bootstrap.min.js"></script>
<script src="assets/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="assets/plugins/pace/pace.js"></script>
<script src="assets/scripts/siminta.js"></script>
<script src="assets/plugins/morris/raphael-2.1.0.min.js"></script>
<script src="assets/plugins/morris/morris.js"></script>
<script src="assets/scripts/dashboard-demo.js"></script>
</body>
</html>
Why give warning Undefined variable rows when i am defined variable in top of code
Why give warning Undefined variable rows when i am defined variable in top of code
Why give warning Undefined variable rows when i am defined variable in top of code
There are some issues with your code.
mysql_* functions are deprecated and insecure. Please use PDO.
Read the following topic about MySQL injections: How can I prevent SQL injection in PHP?
Don't mix HTML5 and XHTML.
Don't use PHP short open tags <?, use the full tag: <?php
<input type="text" id="title" name="newTitle" class="form-control" placeholder="Enter title" value="<?php echo $rows[1]; ?>"/>
<input type="hidden" id="id" name="id" class="form-control" placeholder="Enter title" value="<? echo $rows[0]; ?>"/>
Instead of <?php echo $rows[0]; ?> use:
<?php echo ( isset ( $rows[0] ) ? $rows[0] : '' ); ?>
Same goes for <?php echo $rows[1]; ?>:
<?php echo ( isset ( $rows[1] ) ? $rows[1] : '' ); ?>
So the complete code would be:
<input type="text" id="title" name="newTitle" class="form-control" placeholder="Enter title" value="<?php echo ( isset ( $rows ) ? $rows[1] : '' ); ?>">
<input type="hidden" id="id" name="id" class="form-control" placeholder="Enter title" value="<?php echo ( isset ( $rows ) ? $rows[0] : '' ); ?>">
you are defining variable $rows in "if" block and because of that $rows is not available in whole document.
Try the below code.
// declare $rows at global level
$rows=null;
if(isset($_GET['ID'])){
$id=$_GET['ID'];
$sql="SELECT * FROM research_tab WHERE ID='$id'";
$result=mysql_query($sql);
// assign value
$rows=mysql_fetch_array($result);
}
Try Below Code:
if(isset($_GET['ID'])){
$id = $_GET['ID'];
$result = $conn->query("SELECT * FROM leads WHERE LeadId='$id'");
$rows = $result->fetch_row();
}
OK catch some problems and i fixit for you in PHP tags:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chemistry_web";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//Test if your ID IS a Int if is that you put in your DB
if(isset($_GET['ID']) AND is_numeric($_GET['ID'])){
$id=$_GET['ID'];
$sql="SELECT * FROM research_tab WHERE ID='".$id."'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);}
//include_once 'connection.php';
// if (isset($_GET['ID'])){
// $id=$_GET['ID'];
// $query = "SELECT * FROM $tbl_name WHERE id='$id'";
// $result = mysql_query($query);
// $row = mysql_fetch_array($result);
// }
?>
Just replace your Php tags. May this help you!!

Header.php file not showing anything, and no errors/warnings

I'm having a problem I've been trying to solve for 3 weeks, and have gotten almost nowhere.
My web app was working perfectly fine, until one day it stopped working entirely because my header.php file is royally messed up.
Basically, the problem is:
My header.php file is displaying NOTHING (View Source displays nothing) and the page is completely white. I have turned on all error reporting, and tried echoing things at the top of the page, but NOTHING works until I comment out a few variables in an if statement (see below)
The following are the first few lines of code of my header.php file.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
echo("TEST");
session_start();
if(isset($_SESSION['username'])){ //VARIABLES WITHIN THIS STATEMENT CAUSE ERROR
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$title = $_SESSION['title'];
$id = $_SESSION['id'];
$role = $_SESSION['role'];
$fullname = $_SESSION['fullname'];
$labname = $_SESSION['labname'];
$date_official = $_SESSION['date'];
$permission = $_SESSION['permission'];
$avatar = $_SESSION['avatar'];
$usr_ip = $_SERVER['REMOTE_ADDR'];
$isLogged = true;
}
else
{
$isLogged = false;
echo("Unlogged.");
}
if($isLogged)
{
if($permission == 7 || $permission == 1 || $permission == 2){
echo <<<_END
<!DOCTYPE html>
<html lang="en">
<head>
...file truncated for simplicity.
If I comment out $permission and $role, the echo "TEST" runs, but the rest of the page doesn't. I'm kind of a PHP noob, but honestly I've used PHP for almost 5 years now and have never encountered anything like this.
I greatly appreciate ANY help. Thank you so much.
Cheers
EDIT** I have been asked to paste my entire code:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
echo("TEST");
if(isset($_SESSION['username'])){
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$title = $_SESSION['title'];
$id = $_SESSION['id'];
$role = $_SESSION['role'];
$fullname = $_SESSION['fullname'];
$labname = $_SESSION['labname'];
$date_official = $_SESSION['date'];
$permission = $_SESSION['permission'];
$avatar = $_SESSION['avatar'];
$usr_ip = $_SERVER['REMOTE_ADDR'];
$isLogged = true;
}
else
{
$isLogged = true;
echo("Unlogged.");
}
if($isLogged)
{
if($permission == 7 || $permission == 1 || $permission == 2){
echo <<<_END
<!DOCTYPE html>
<html lang="en">
<head>
<!--CHARSET-->
<meta charset="UTF-8">
<!--FAVICON-->
<link rel='shortcut icon' type='image/x-icon' href='/favicon.ico' />
<!--INTERNAL STYLING-->
<link href="css/style.css" rel="stylesheet">
<link href="css/core_styles.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/core_styles.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="css/calendar.css" />
<link rel="stylesheet" type="text/css" href="css/font-awesome.css">
<link rel="stylesheet" href="css/dropzone.css">
<!--JQUERY AND JQUERY UI-->
<script src="js/jquery-2.2.3.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/smoothness/jquery-ui.css" />
<!--INTERNAL SCRIPTS-->
<script src="js/functions.js"></script>
<script src="js/account.js"></script>
<script src="js/storage.js"></script>
<script src="js/notebook.js"></script>
<script src="js/protocol.js"></script>
<script src="js/calendar.js"></script>
<script src="js/members.js"></script>
<script src="js/administration.js"></script>
<script src="js/dash.js"></script>
<!--CHARTS-->
<script src="js/Chart.min.js"></script>
<!--DROPZONE-->
<script src="js/dropzone.js"></script>
<!--BOOTSTRAP-->
<link type="text/css" href="css/bootstrap.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<!--SUMMERNOTE-->
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.1/summernote.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.1/summernote.css" rel="stylesheet">
<!--TOOLTIPS-->
<link rel="stylesheet" type="text/css" href="css/tooltipster.css" />
<script type="text/javascript" src="js/jquery.tooltipster.min.js"></script>
<title>FMAX ($labname)</title>
</head>
<body>
<div id="appWrapper">
<div id="point_prompt"></div>
<div id="appPanelLeft">
<!--LOGO CONTAINER-->
<div id="appLogo" class="subPanel">
<span class="logoF logoFont textDeny">F </span><span class="logoMax logoFont textDeny">M A X</span>
</div>
<!--PROFILE PICTURE CONTAINER-->
<div id="subPanel_0" class="subPanel">
<div class="profilePicture" style="background-image: url($avatar)"></div>
<div class="ta_center">
<span id="pnl_username" class="regTextPanelB">$fullname</span>
<br/>
<span id="pnl_permis" class="hintTextA">$role</span>
<br/>
<span id="pnl_degree" class="hintTextA">$title</span>
<br/>
</div>
</div>
<!--MAIN NAV CONTAINER-->
<div id="subPanel_1" class="subPanel">
<table id="navTable">
<tr>
<td>
<div data-value="0" class="pnlNavBtn">
<span class="fa fa-home fa-3x fa-fw myBlue"></span><br/>
<span class="regTextPanelA">Dashboard</span>
</div>
</td>
<td>
<div data-value="1" class="pnlNavBtn">
<span class="fa fa-book fa-3x fa-fw myPurple"></span>
<span class="regTextPanelA">Notebook</span>
</div></td>
</tr>
<tr>
<td>
<div data-value="2" class="pnlNavBtn">
<span class="fa fa-shopping-cart fa-3x fa-fw myYellow"></span>
<span class="regTextPanelA">Orders</span>
</div>
</td>
<td>
<div data-value="3" class="pnlNavBtn">
<span class="fa fa-list fa-3x fa-fw myOrange"></span>
<span class="regTextPanelA">Protocols</span>
</div>
</td>
</tr>
<tr>
<td>
<div data-value="4" class="pnlNavBtn">
<span class="fa fa-database fa-3x fa-fw myGreen"></span>
<span class="regTextPanelA">Storage</span>
</div>
</td>
<td>
<div data-value="5" class="pnlNavBtn">
<span class="fa fa-users fa-3x fa-fw myDarkBlue"></span>
<span class="regTextPanelA">Members</span>
</div>
</td>
</tr>
<tr>
<td>
<div data-value="6" class="pnlNavBtn">
<span class="fa fa-calendar fa-3x fa-fw myYellow"></span>
<span class="regTextPanelA">Calendar</span>
</div>
</td>
<td>
<div data-value="8" class="pnlNavBtn">
<span class="fa fa-lock fa-3x fa-fw myRose"></span>
<span class="regTextPanelA">Administration</span>
</div>
</td>
</tr>
</table>
</div>
<!--USER NAV CONTAINER-->
<div id="subPanel_2" class="subPanel">
<span class="hintTextA">User</span>
<br/>
<br/>
<div data-value="9" class="pnlNavBtn list">
<span class="fa fa-user fa-fw myBlue"></span>
<span class="regTextPanelA">Account</span>
</div>
<a href="logout.php"><div class="pnlNavBtn list">
<span class="fa fa-sign-out fa-fw myBlue"></span>
<span class="regTextPanelA">Logout</span>
</div>
</a>
</div>
<!--LAB MODULES CONTAINER-->
<div id="subPanel_3" class="subPanel">
<span class="hintTextA">Public Lab Page</span>
<br/>
<br/>
<div data-value="10" class="pnlNavBtn list">
<span class="fa fa-edit fa-fw myBlue"></span>
<span class="regTextPanelA">Edit Modules</span>
</div>
</div>
</div>
<div id="appWindow">
<!--TOOLBAR -->
<div id="appToolbar" class="row">
<nav class="navToolbar">
<ul>
<li id="app_msgs_btn"><span class="fa fa-lg fa-envelope-o myDarkBlue"></span> <span class="notificationHint small" id="inboxHint">0</span></li>
<li id="app_notif_btn"><span class="fa fa-lg fa-exclamation-circle myRose"></span> <span class="notificationHint small" id="notifHint">0</span></li>
<ul>
</nav>
</div>
<div id="notificationPane">
<div class="notification">
<span class="fa fa-fw fa-exclamation myRed"></span> Lorem ipsum dolor sit amet.
</div>
<div class="notification">
<span class="fa fa-fw fa-exclamation myRed"></span> If you type for long enough, your text might just extend two lines.
</div>
<div class="notification">
<span class="fa fa-fw fa-exclamation myRed"></span> Some Content.
</div>
<div class="notification">
<span class="fa fa-fw fa-exclamation myRed"></span> Some Content.
</div>
<div class="notification read">
<span class="fa fa-fw fa-exclamation myRed"></span> Some Content.
</div>
</div>
_END;
}
}
else{
echo("Error.");
}

Multiple recipients email php

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
}
//}
}
}
}
?>

Categories