Where to put $_Session[variable] in the code - php

Im trying to save a variable in the $_Session[deptselect] and call it in a different page but confused to where to declare it. when i echo $_Session[deptselect] it gives blank.
I have already tried in multiple places like inside the form or below it but nothing seems to work. i want to send it from appointment to appointment2 but appointment2 seems to not get the variable.
I have already tried to use $_post[deptselect] to get the value in second page and i get it there but it disappears as soon as i press submit in the second page.
appointment.php
<?php
include("dbconnection.php");
session_start();
if(!isset($_SESSION['username'])) {
die("Please login");
}
$_SESSION['deptselect']=$_POST['deptselect'];
?>
<form class="" action="appointment2.php" method="post">
<fieldset>
<legend>Appointment</legend>
<label>Select Department</label>
<select class="" name="deptselect" required>
<option ></option>
<?php
$deptsql=mysqli_query($con,"select deptname,deptid from department where status='1'");
while ($row = mysqli_fetch_assoc($deptsql))
{
echo "<option id='deptselect' value='" . $row['deptid'] ."'>" . $row['deptname'] ."</option>";
}
?>
</select>
</fieldset>
<input type="submit" name="submit" value="Submit">
</form>
<?php
include("footer.php");
?>
appointment2.php
<?php
session_start();
if(!isset($_SESSION['username']))
{
include('footer.php');
die("Please login");
}
if(!empty($_POST)){
include("dbconnection.php");
$dept=$_SESSION['deptselect'];
echo $dept;
$daate=$_POST['date'];
$doc = $_POST['doc'];
$tiime=$_POST['time'];
$user=$_SESSION['username'];
$pd="SELECT * from patient where name='$user'";
$pid=mysqli_query($con,$pd);
while($row = mysqli_fetch_assoc($pid)){
$piid=$row['pid'];
}
$query="insert into appointment (deptid, docempid,pid,apptime,appdate) VALUES ('$dept','$doc','$piid','$tiime','$daate') " ;
//$res=mysqli_query($con,$query);
// echo "$query";
//echo "$dept";
}
?>
<body>
<form class="" action="appointment2.php" method="post">
<label>Select doctor</label>
<select class="" name="doc" >
<option ></option>
<?php
$dee=$_POST['deptselect'];
$_SESSION['id']=$dee;
$sql="SELECT * FROM doctor where deptid='$dee'";
$docsql= mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($docsql))
{
echo "<option value='" . $row['docempid'] ."'>" . $row['name'] ."</option>";
}
?>
</select>
<br><br><br>
<label>Enter Time</label>
<input type="time" name="time" placeholder="enter time" >
<br><br><br>
<label>Enter date</label>
<input type="date" name="date" placeholder="enter date" >
<br> <br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
<?php include("footer.php");
?>
I want the $_session[deptselect] in appointment2.php so i can run the command the insert, i have zero knowledge of js so doing the dependent list thing is not possible. TIA

In the beginning of the first script you have:
if(!isset($_SESSION['username'])) {
die("Please login");
}
And in the beginning of the second:
if(!isset($_SESSION['username']))
{
include('footer.php');
die("Please login");
}
Both stop immediately, if $_SESSION['username'] is not set.
So how should $_SESSION['username'] ever be set, if your body code is never executed?

Related

How to store and get two values in PHP?

This is process_upcategory.php
I want to update the category name or the category id with another category name/id by its category id or by its category name.
I'm new to php
<?php
require('includes/config.php');
if(!empty($_POST))
{
$msg=array();
if(empty($_POST['cat']))
{
$msg[]="Please full fill all requirement";
}
if(!empty($msg))
{
echo '<b>Error:-</b><br>';
foreach($msg as $k)
{
echo '<li>'.$k;
}
}
else
{
$cat_nm=$_POST['cat[0]'];
$cat_id=$_POST['cat[1]'];
$query= "UPDATE `category` SET cat_nm='$cat_nm' WHERE cat_id='$cat_id'";
mysqli_query($conn,$query) or die("can't Execute...");
mysql_close($link);
header("location:category.php");
}
}
else
{
header("location:index.php");
}
?>
Now this is category.php, just a snippet of code. Not whole code
<form action='process_upcategory.php' method='POST'>
<b style="color:darkgreen">UPDATE CATEGORY </b> <br>
<b style="color:darkgreen">Old Category</b>
<br>
<select name="cat[]" multiple>
<?php
$query="select * from category ";
$res=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($res))
{
echo "<option>".$row['cat_nm'];
echo "<option>".$row['cat_id'];
}
?>
</select>
<br>
<b style="color:darkgreen">New Category</b><br>
<input type='text' name='cat[0]'></input><br>
<input type='text' name='cat[1]'></input>
<input type='submit' value=' UPDATE '>
</form>
I want to update the category name with another category name by its category id or by its category name. I get undefined index cat[0] and cat[1]
When you end an input name with [] it wil be converted to an array by php. The correct way to get the values in this case would be something like this:
$cat=$_POST['cat'];
$cat_nm=$cat[0];
$cat_id=$cat[1];
I combined the two routines into one script.
I added 'sub' to the form to distinguish from when the form was submitted or not.
I used list() in the query result loop.Used mysqli_fetch_array($result, MYSQLI_NUM) rather than mysqli_fetch_assoc($res)
used foreach() to loop through the $_POST['cat']
Added 'value' to the <option value=""> to hold the id
Eliminated the switching from HTML mode to PHP mode by using HEREDOC.
<?php
if (intval($_POST['sub']) == 1){
$newcat = $_POST['new'];
foreach($_POST['cat'] as $key=>$value){
if(strlen($newcat[$key]) > 0){
mysqli_query($conn,"UPDATE `category` SET `cat_nm`='$newcat[$key]' WHERE `cat_id`='$value'");
}
}
}
echo <<<EOT
<html><head><style>h4,h3{color:darkgreen;margin:.2em;}</style></head><body>
<form action="#" method='POST'>
<h3>UPDATE CATEGORY</h3>
<h4>Old Category</h3>
<select name="cat[]" multiple>
EOT;
$sql="SELECT `cat_nm`, `cat_id` FROM `category` ";
$result=mysqli_query($conn,$sql);
while(list($cat_nm,$cat_id) = mysqli_fetch_array($result, MYSQLI_NUM)){
echo " <option value=\"$cat_id\">$cat_nm</option>\n";
}
echo <<<EOT
</select>
<h3>New Category</h3>
<input type="text" name="new[0]" /><br/>
<input type="text" name="new[1]" /><br/>
<input type="hidden" name="sub" value="1" /><br/>
<input type="submit" value=" UPDATE />
</form>
</body></html>
EOT;
?>

Adding radio button to MySQL fetch result and use result value to fetch another record...?

I want a first radio button to be checked by default. When I click get schedule I want to fetch the schedule of that radio selected train number using a WHERE clause.
<?php
while ($res = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type = radio />" . $res['Train_no'] . "</td>";
?>
<form action="schedule.php" method="POST" class="form-inline">
<input type="submit" value="Get Schedule"/>
</form>
<?php
}
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('traindb', $con);
} else {
die('Could not connect: ' . mysql_error());
}
$selected_val = $_POST['Train_no']; // Storing Selected Value In Variable
echo "You have selected :" . $selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE Train_No='$selected_val'");
mysql_close($con);
?>
You can try this:
if(isset($_POST['Submit'])){
$selected_val = $_POST['Train_no']; // Storing Selected Value In
echo "You have selected :" .$selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE
Train_No='$selected_val'");
}
Here is one example of "select":
<form action="" method="post">
<select type="age" class="form-control" id="age" name="age">
<!-- <option value="disable" selected="">Please Select</option> -->
<option value="">Please select</option>
<option value="Under 35">Under 35</option>
<option value=">35 - 44">35 - 44</option>
<option value=">45 - 54">45 - 54</option>
<option value=">55 - 59">55 - 59</option>
<option value=">60 - 64">60 - 64</option>
<option value=">65 - 69">65 - 69</option>
<option value=">70 - 74">70 - 74</option>
<option value=">75 - 79">75 - 79</option>
<option value="80 +">80 +</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
$age = $_POST['age'];
if (isset($_POST['age']) && $_POST['age'] == "")
echo "You did not choose any options. Pls try again.";
else {
echo $age;
}
?>
If you want embed PHP in select options, do it like this:
<option value="<?php echo $res['Train_no'] ?>"><?php echo $res['some_other'] ?></option>
For radio button it is like this:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<input type="radio" name="gender" value="<?php echo $res['Train_no'] ?>" checked><?php echo $res['some_other'] ?><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br><br>
<input type="submit">
</form>
</body>
</html>

How to load a view in codeigniter where multiple forms are used

This is my view:
<form action="HomeController.php" method="post">
Available Batches:<select name="batch">
<?php mysql_connect('localhost','root','');
mysql_select_db('login');
$sql = "SELECT b_id FROM batch where type='C#'and seats_left!=0";
$result= mysql_query($sql);
while ($row = mysql_fetch_array($result)){
?>
<option value="" ><?php echo $row["b_id"];?></option>
<?php
}
?>
</select><br>
<input type="submit" name="new" value="new"><br>
</form>
<?php echo form_open('HomeController/Enteringdata') ?>
Enter batch id:<input type="text" name="b"><br>
<input type="submit" name="enter" value="enter"><br>
This is my controller:
public function Enteringdata() {
if($this->input->post('enter')=="enter"){
$b= $this->input->post('b');
$this->load->Model('LoginModel');
$seats=$this->LoginModel->batch($b);
$seats--;
$this->LoginModel->ins($seats,$b);
}
elseif($this->input->post('new')=="new")
{
$this->load->view('batchc');
}
}

php form and session assignment in the same file

I am trying to use post in a form to save form data from a dropdown as a session in the same file as the form
<?php
session_start();
if(isset($_SESSION['post-data']['surnameid']))
unset($_SESSION['post-data']['surnameid']);
?>
Then in the body of html
<form action="" method="post">
<?php
include 'connect.inc';
$sql = "SELECT surnameid FROM customer";
$result = mysql_query($sql);
echo "<select name='surnameid'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['surnameid'] . "'>" . $row['surnameid'] . </option>";
}
include 'close.inc';
?>
<br/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
$_SESSION['post-data'] = $_POST;
echo $_SESSION['post-data']['surnameid'];
?>
The assiginment to $_SESSION does not work
Try this :
You forgot a double quote when you are putting echoing
make sure that you have the rows in the database ( we can't test that ) but i tested with predefined data and this version works
form.php
<?php
session_start();
?>
<form action="" method="post">
<?php
echo "<select name='surnameid'>";
echo "<option value='1'>test</option>";
echo "<option value='2'>test2</option>";
?>
<br/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if( !empty($_POST)){
$_SESSION['post-data'] = $_POST;
}
if(isset($_SESSION['post-data']['surnameid']))
echo $_SESSION['post-data']['surnameid'];
?>
test_session.php
<?php
session_start();
print_r($_SESSION['post-data']['surnameid']);
?>

php script with multiple buttons

The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>

Categories