php script with multiple buttons - php

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>

Related

<option> is having each option its own dropdown

I tried putting the loop inside but then when I pick a category it won't print in the echo and if the loop is outside like right now each category is printed alone I want them all in one tab and one go button
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<form action="" method="post">
<select name="work_place">
<option value="<?php echo $category;?>"><?php echo $category;?></option>
</select>
<input type="submit" value="go" />
</form>
<?php
}
<?php
$post = (isset($_POST['work_place'])) ? $_POST['work_place'] : '';
echo $post;
?>
Image of current issue:
<form action="" method="post">
<select name="work_place">
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<option value="<?php echo $category;?>"><?php echo $category;?>
</option>
<?php
} ?>
</select>
<input type="submit" value="go" />
</form>
You want to loop over 'option', only.
You do not want multiple forms and 'select' inputs.

Where to put $_Session[variable] in the code

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?

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;
?>

dropdown selected item not printing

I am trying to print the dropdown selected item. I have well displayed the dropdwon list menu. but when i select an option it doesn't print the option. i have tried in many ways. But not yet got! Please help me, this is my following code.
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
You have issues in your code, try this one instead :
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
$_GET['category']
should be
$_POST['category']
Example for javascript:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('cat');
eSelect.onchange = function() {
document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
document.getElementById("displaytext").style.display= 'block';
}
}
</script>
</head>
<body>
<select id="cat" name="cat">
<option value="x">X</option>
<option value="y">Y</option>
<option value="other">Other</option>
</select>
<div id="displaytext" style="display: none;" ></div>
</body>
</html>
​

handle info from select box

I have the following code:
<form name="votos" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php $categorias=mysql_query("SELECT * FROM categoria") or die (mysql_error()); ?>
<?php while($cat=mysql_fetch_array($categorias)){
echo "<h5>".$cat[0]." - ".$cat[1]."</h5>";
$nomeados=mysql_query("SELECT * FROM nomeados WHERE cod_categoria='$cat[0]'") or die(mysql_error());
?>
<div class="styled-select">
<select name="voto">
<option value=""></option>
<?php
while($nom=mysql_fetch_array($nomeados)){
$nomes=mysql_fetch_array(mysql_query("SELECT nome FROM logins WHERE cod_login='$nom[0]'")) or die (mysql_error());
?>
<option value="<?php echo $nom[0]; ?>"><?php echo $nomes[0]; ?></option>
<?php
}
?></select></div>
<?php }
?>
<input type="submit" class="botao" value="" name="submit" />
</form>
The code shows all the categories with the nomenies in a select box.
The thing is that the select box goes to post with the same name. I don't know how to get the information from all of the select boxes.
If you have more elements with the same name you can post them as an array
<select name="voto[]">
and than you get it in php
$votos = $_POST['voto']
foreach($votos as $voto){
//do what you need to do
}
and it's an array with all the values

Categories