restricting access to specific users in php error - javascript

I have a made a simple website with login. My users sql table is as shown below:
my login form is like below:
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Sign In</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="login.php">
<fieldset>
<div class="form-group" >
<input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="pass" type="password" value="">
</div>
<input type="submit" value="login" name="login" >
i want to restrict some users from entering some pages. i have made the access column for this. I have added the following code to the protected page
session_start();
if($_SESSION["access"]!=0)
{
header('Location: login.php');
}
there are some problems with this coode i guess. when the access=0,the page is shown even if the user is not logged in and shows like Undefined index: access in C:\xampp\htdocs\nurse\index.php on line 12
when the access is set to 1 ,its redirected to login page. can anyone help me?

You need to check session is set or not like this
if(isset($_SESSION["access"])) {
if($_SESSION["access"] !=0) {
header('Location: login.php');
}
}

Related

Creating a request button to save the word in the database which is typed in search box along with the logged in user name in codeigniter

I have a program in codeigniter where in, after logging in there will be a search box to search(ajax search) the data from database,it will be in the form of link.
But if i have any word which is not in the database, it should display "No data found" and there should be a "Request button".
On clicking the request button, the typed word in the search box should save in the database along with the logged in user name, and it should display request sent on screen.
This is my controller for ajaxsearch:
if(is_null($this->input->get('id')))
{
$this->load->view('data');
}
else
{
$this->load->model('Info_model');
$data['Infotable']=$this->Info_model->Infotable($this->input->get('id'));
$this->load->view('view_result',$data);
}
This is my view:
1.Data page
<form action="" method="get">
<div class="row">
<div class="col-lg-10 col-lg-offset-1">
<div class="input-group">
<span class="input-group-addon" >SEARCH</span>
<input autocomplete="off" id="search" type="text" class="form-control input-lg" placeholder="Search " >
</div>
</div>
</div>
<div class="space"></div>
</form>
Thanks in advance
<form action="<?php echo base_url();?>Test/ajaxsearch" method="post">
<div class="row">
<div class="col-lg-10 col-lg-offset-1">
<div class="input-group">
<span class="input-group-addon" >SEARCH</span>
<input autocomplete="off" id="search" name="search" type="text" class="form-control input-lg" placeholder="Search " >
<button type="submit">Search</button>
</div>
</div>
</div>
<div class="space"></div>
</form>
then in your ajaxsearch()
public function ajaxsearch() {
if(#$this->input->post('search') != "") {
$data['Infotable']=$this->Info_model->Infotable($this->input->post('search'));
if(count($data['Infotable']) > 0) {
$this->load->view('view_result',$data);
}else {
$data['dataNotFound'] = "No data Found";
$this->load->view('view_result',$data);
}
}
}
then in view_result
<?php
if(isset($dataNotFound)) {
echo $dataNotFound;
}else {
foreach($Infotable as $objects) {
echo $objects;
}
}
?>

contact us web page using Ajax and php

I would like to create contact us web page without refreshing the page when click send button. My code bellow, but there are some problems:
1- The page is refreshed after submitting the form.
2- The value of the Contact form inputs appear in the URL after submitting the form. check this image
3- The confirmation message appears for 0.25 seconds then disappear or does not appear.
4- The email is not received to my email address when I use Firefox browser.
Please help me to solve those problems.
HTML
<div class="col-sm-8 col-sm-offset-2" id="mail-status">
</div>
<div class="container-fluid">
<div class="row">
<div class="col-sm-8 col-xs-12 col-sm-offset-2">
<div class="well">
<form class="form-horizontal">
<fieldset>
<legend class="legend-title">I'd <span class="heart"><i class="fa fa-heart"></i></span> to contact you!</legend>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required="required">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" required="required">
</div>
</div>
<div class="form-group">
<label for="inputSubject" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputSubject" name="subject" placeholder="Subject" required="required">
</div>
</div>
<div class="form-group">
<label for="textAreaMessage" class="col-sm-2 control-label">Message</label>
<div class="col-sm-9">
<textarea class="form-control" rows="5" id="textAreaMessage" name="message" placeholder="Message" required="required" minlength=5 ></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-sm-offset-8">
<button type="submit" class="btn btn-primary right-button" id="send" onClick="sendContact()">Send</button>
</div>
</div>
</fieldset>
</form>
Javascript
function sendContact() {
jQuery.ajax({
url: "send_email.php",
data:'inputName='+$("#inputName").val()+'&inputEmail='+$("#inputEmail").val()+'&inputSubject='+$("#inputSubject").val()+'&textAreaMessage='+$(textAreaMessage).val(),
type: "POST",
success:function(data){
$("#mail-status").html(data);
},
error:function (){}
});
}
php
<?php
$toEmail = "*****#***.com";
$mailHeaders = "From: " . $_POST["inputName"] . "<". $_POST["inputEmail"] .">\r\n";
if(mail($toEmail, $_POST["inputSubject"], $_POST["textAreaMessage"], $mailHeaders)) {
print "<div class='alert alert-success'> <button type='button' class='close' data-dismiss='alert'>×</button> Thank you for your email! I will be in touch.</div>";
} else {
print "<div class='alert alert-danger'> <button type='button' class='close' data-dismiss='alert'>×</button> We are sorry, but there is a problem. Please Make sure all fields are filled!</div>";
}
?>`
Get rid of the inline onclick() event in your HTML, and let your JavaScript handle the click:
$('#send').click(function(e){
e.preventDefault();
sendContact();
});
preventDefault() stops the click event from acting normally, so your form data is submitted without reloading the page.

Unable to insert data in a table using php

I have this following code->
html5 code block:
<div class="col-md-8 blogger-right-container">
<form action="blogger_account.php" method="post" role="form" enctype="multipart/form-data">
<div class="form-group">
<h2>Blog Title</h2>
<input type="text" class="form-control" name="blog_title" placeholder="Enter title for your blog.">
</div>
<div class="form-group">
<h2>Blog Content</h2>
<textarea name="blog_content" class="form-control" cols="30" rows="10" placeholder="Enter the content here."></textarea>
</div>
<div class="form-group" style="text-align: center;">
<input type="submit" class="btn btn-success btn-lg" name="add" value="Insert">
</div>
</form>
</div> <!-- blogger right container -->
php code block:
`
if(isset($_POST['add'])){
$blogger = $_SESSION['blogger_email'];
$blog_title = $_POST['blog_title'];
$blog_content = $_POST['blog_content'];
$insert_blog = "insert into blogs (blogger_email,blog_title,blog_content) values ('$blogger',$blog_title','$blog_content')";
$run_blog = mysqli_query($con,$insert_blog);
if($run_blog){
echo "<script>alert('Blog inserted.')</script>";
echo "<script>window.open('blogger_account.php','_self')</script>";
}
}`
The php code block is not inserting the data in blogs table. The columns are in proper order, but still i tried to change the sequence and it didn't worked.
Can anyone tell me whats wrong with my code.
Please mention Error you are receiving.
In order to show detailed php errors, navigate to " How to get useful error messages in PHP? "
MySQL errors in php can be thrown this way --> PHP MySQL error handling

to redirect the same page after form submit

I have a form in my webpage Index.html. When I submit my form it redirects to the action page.
I want the page to redirect to itself (index.html) after completing the action.
<div class="container">
<div class="row marginbot-80">
<div class="col-md-8 col-md-offset-2">
<form id="contact-form" method="post" action="mailfunc.php" class="wow bounceInUp" data-wow-offset="10" data-wow-delay="0.2s">
<div class="row marginbot-20">
<div class="col-md-6 xs-marginbot-20">
<input type="text" class="form-control input-lg" id="name" placeholder="Enter name" required="required" />
</div>
<div class="col-md-6">
<input type="email" class="form-control input-lg" id="email" placeholder="Enter email" required="required" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control input-lg" id="subject" placeholder="Subject" required="required" />
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" cols="25" required="required" placeholder="Message"></textarea>
</div>
<button "style=width:40 height:40" class="btn btn-skin" type="submit" id="btnContactUs">Send Message</button>
</div>
</div>
</form>
</div>
</div>
</div>
You could redirect after the script in mailfunc.php to your index.html like this:
header("Location: index.html");
exit();
Currently your submit will open a POST request to mailfunc.php where your form data (most probably) will be processed.
You can quite easily forward from mailfunc.php to index.html using some Javascript code like:
window.location.href = 'index.html';
Try this.
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
This helps you to redirect to the same page where you are submitting the form
Refer this link to know more about HTTP_REFERER

RSVP Form submitting after each response- Need All responses submitted together for family

This is for a private wedding website. Invitations are sent to guests, which could be individuals or entire families. Those invitations include a special Passcode that lets them access the website. Once inside, in the RSVP section we have returned the names of the guests invited.
The goal is to allow whichever guest that has logged in to respond to the RSVP by clicking a box that says "Yes" I'll be in attendance or "No" I won't be attending.
The question is available to all of the people in the group. Then the person can enter other info + offer a song suggestion for the wedding. I'd like at that point for the entire form to be submitted altogether.
PROBLEM: As soon as a guest clicks on (Yes) or (No) the form automatically submits, and then refreshes the page. At that point you can't see what selection was made any longer. How do I change this code so that my guests can click all of the yes's and no's and then entire the other information, where then they can see all of their responses before clicking Submit which submits() the whole form?
Ex:
John (Yes)
(No)
Emily (Yes)
(No)
Louis (Yes)
(No)
Mailing Address (form field)
Email Address (form field)
Phone number (form field)
Song Title (form field)
Artist (form field)
Button (Submit)
<!-- FORM -->
<div class="row">
<div id="result" class="col-md-12"> <!-- Show Message --> </div>
<div class="col-md-6">
<div id="events" class="form-group">
<?php
global $wpdb;
$guestname = $wpdb->get_results("SELECT * FROM `wp_password_a` where pwd_a_id='" . $_SESSION['admin_id'] . "' ");
$guest = $guestname[0]->name_guest;
$id_res = $guestname[0]->wp_response_id;
$myrows = $wpdb->get_results("SELECT * FROM `wp_pwd_a_response` where guestname='" . $guest . "' ");
//echo "<pre>";print_r($myrows);
$i = 1;
foreach ($myrows as $pro_data) {
?>
<form method="post" id="formName<?php echo $pro_data->wp_response_id; ?>" >
<div class="col-md-12">
<div class="col-md-4" style="margin:10px 0px;">
<?php echo $pro_data->member; ?>
</div>
<div class="col-md-4">
<div class="checkbox">
<label><input type="checkbox" class="checkbox" onchange="document.getElementById('formName<?php echo $pro_data->wp_response_id; ?>').submit()" name="response" value="YES">Will be in Attendance</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label><input type="checkbox" class="checkbox" onchange="document.getElementById('formName<?php echo $pro_data->wp_response_id; ?>').submit()" name="response" value="NO">Regretfully Declines</label>
<input type="hidden" name="res_id" value="<?php echo $pro_data->wp_response_id; ?>">
</div>
</div>
</div>
</form>
<?php } ?>
<form method="post">
<div id="fullname" class="form-group">
<label for="inputname"><i><b>Mailing Address</b></i></label>
<input type="text" name="address" class="form-control" id="inputname" placeholder="">
</div>
<div class="form-group">
<label for="inputname"><i><b>E-mail Address</b></i></label>
<input type="text" name="phone" class="form-control" id="inputname" placeholder="">
</div>
<div class="form-group">
<label for="inputname"><i><b>Phone Number</b></i></label>
<input type="text" name="phone" class="form-control" id="inputname" placeholder="">
</div>
</div>
</div>
<div class="col-md-6">
<div class="col-md-12">
<i></br></br>In celebration of the bride and groom’s special day, I would like to dedicate the following song:</i>
</div>
<div id="fullname" class="form-group">
<label for="inputname"></br></br><i><b>Song Title</b></i></label>
<input type="text" name="song" class="form-control" id="inputname" placeholder="">
</div>
<div class="form-group">
<label for="inputname"><i><b>Artist</b></i></label>
<input type="text" name="artist" class="form-control" id="inputname" placeholder="">
</div>
</div>
<div class="col-md-12 text-center text-danger"></br></br>
<div><b><i>Please provide a response by August 1, 2015.</i></b></div>
</div>
<div class="col-md-12 text-center">
<div class="form-group">
<input type="submit" id="submitButton" name="submitresponse" class="btn btn-default btn-lg" value="Submit">
</div>
</div>
<form>
</div>
</div>
</section><!--END of RSVP SECTION-->
Based on the code sample you provided, it appears you are creating a new form for each checkbox, which is separate from the form containing the other information. Furthermore, in the generated checkbox forms, you have the code:
onchange="document.getElementById('formNamewp_response_id; ?>').submit()"
This explains the behavior you are seeing when the checkbox is clicked and the page refreshes. The onchange event fires, and the form submits.
To achieve the behavior you are looking for, render each your checkbox inputs into the lower form, and remove the 'onchange' handler on each checkbox input. Then when you submit the form, all fields will be submitted at once. Just make sure that the endpoint you are POSTing to properly handles the new fields.

Categories

Resources