how to add confirmation message before deleting an item using php - php

i wanna add a confirmation message when i wanna delete an item showed in my webpage that's connected to a database
<?php
if (isset($_GET['Pdelete'])) {
$delid = sanitize($_GET['Pdelete']);
$db->query("DELETE FROM products WHERE ID = $delid");
header('Location: Archived.php');
}
?>
<i class="far fa-trash-alt"></i>
these codes work fine, all i need now is a confirmation message. TIA

Your best option is to use javascript. I really like to use a jQuery library called Jquery Validator. This library is very convenient to use and has a lot of other very powerful tools for validating a input form. That would be my first choice.
If you are looking for a pure php way of doing this you can use something like this.
if(isset($_POST['yes'])){
$delid = sanitize($_POST['yes']);
$db->query("DELETE FROM products WHERE ID = $delid");
header('Location: Archived.php');
}
if(isset($_POST['no'])){
header('Location: whereever.php'); //Redirect to whereever.
}
if(isset($_GET['Pdelete'])){
echo
'<form action="" method="post" enctype="multipart/form-data">
<p>Are you sure that you want to delete this item?</p>
<input type="submit" name="yes" value="' . $_GET['Pdelete'] . '"> <input type="submit" name="no" value="No">
</form>';
exit();
}

Use this code i think i have helped you.
<i class="far fa-trash-alt"></i>

Related

How can I remove all items from the cart in PHP?

I already have some items in the cart. I want to remove them all. So, I've created a submit button with post method called 'remove_all', following doesn't work, it says "The requested URL was not found on this server." Can please someone help me?
<?php
if (isset($_POST['remove_all'])){
unset($_SESSION['cart']);
echo "<script>window.location = 'cart.php'</script>";
}
?>
And here what I wrote in my index.php
<form action="remove_all" method="post" class="cart-items">
<button type="submit" value="Очистить всё" name="remove_all" class="btn btn-danger mx-2">Очистить всё</button>
</form>
It looks like you you not linking properly to your remove_all.php file. Try this:
<form action="/remove_all.php" method="post" class="cart-items">
<button type="submit" value="Очистить всё" name="remove_all" class="btn btn-danger mx-2">Очистить всё</button>
</form>
Also in your PHP file, you do not need to mix PHP and js - simply change header as following:
<?php
if (isset($_POST['remove_all'])){
unset($_SESSION['cart']);
// echo "<script>window.location = 'cart.php'</script>";
header("Location: cart.php");
}
?>

How do I use variables after header() redirect?

I've spent ages trying to figure this out after reading guide after guide and I can't get it. I have 3 files - user.php, map.php and newaddress.php. I've only included what I understand to be the important bits to this issue. Happy to provide more info if needed.
user.php passes a "mapnumber" to map.php.
user.php
<form action="map.php" method="post">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php receives "mapnumber" and generates a list of addresses from the database with matching "mapnumber". From map.php, user can add new addresses which take the "mapnumber" value and is processed in newaddress.php.
map.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php adds the address to database with the "mapnumber" value and then redirects back to map.php where it SHOULD generate the list of addresses based on "mapnumber" but map.php doesn't pick up the "mapnumber" from newaddress.php and hence the list of addresses isn't generated.
newaddress.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php");
exit;
Please help
You can set the get variable like this:
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?nr=". $mapnumber);
exit;
Than in map.php you get get it by:
$mapnumber = $mysqli->escape_string($_GET['nr']);
You could use cookies to store values in the remote browser.
But the real problem you encounter is that the Location redirect is just that. It tells the browser to load another URL.
You can add get requests to that Location redirect URL like that:
header("Location: map.php?mapnumber=".urlencode($mapnumber));
But please keep in mind that the default Location redirect will be a permanent one (HTTP Status Code 301) and it is up to the browser to recheck if the redirecting url/document might have changed or not.
http://php.net/manual/en/function.header.php#78470
So you might want to consider using a 307 redirect:
header("Location: map.php?mapnumber=".urlencode($mapnumber), TRUE,307);
Anyway: passing variables from one page to another is generally not a good practice. Try to avoid it and try to dig into sessions.
http://php.net/manual/en/intro.session.php
I recommend you to use get method in map.php
Then it will receive map number like map.php?mapnumber=somenum
user.php
<form action="map.php" method="get">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php
$mapnumber = $mysqli->escape_string($_GET['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?mapnumber=$_POST['mapnumber']");
exit;

POST form displaying wrong echo

I'm trying to display one of two buttons depending on "magnum"(printername) being in present in the array "booleans".
My problem is that when the form gets posted, the data retrieved on page load is correct, but the buttons displayed are incorrect. if clicked on a button, the form posts and refreshes the page, "magnum" gets pushed into $_SESSION['booleans'] but the button still displays "btn btn-default", so it requires another page refresh for the button to load correctly('btn btn-succes').
Is my problem due to $_SESSION or am i missing something?
echo'
<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
if(isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']){
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans'][$pos]);
dump('unset');
}
}
elseif(!in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
if(isset($_POST['submitMagnum'])&& $_POST['submitMagnum']){
array_push($_SESSION['booleans'],'magnumBool');
dump('set');
}
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '
</div>
</form>';
$_SESSION['printers'] is an array containing "magnum" -
$_SESSION['booleans'] is the array which isn't working as i would like it to -
$enabled_printer = "btn btn-success" <br>
$disabled_printer = "btn btn-default" <br>
$lost_connection_printer = "btn btn-danger disabled"
The problem is that you are mixing elaboration and printing. Try to split you code, so it will work and it will be more readable:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']) {
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans']['magnumBool']);
} elseif (isset($_POST['submitMagnum'])&& $_POST['submitMagnum']) {
$_SESSION['booleans']['magnumBool'] = true;
}
}
echo'<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(isset($_SESSION['booleans']['magnumBool'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
} else {
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '</div>
</form>';
P.s. note the use of "magnumBool" as a key isset instead of as a value: in this way (when possible) you will avoid duplicate entry and will make you code lighter if you have large arrays ;)
P.p.s. try always to keep you login separate from you template, this will make your code more readable and easier to maintain

disable a submit button after clicking on it in codeigniter

I'm new to php and codeigniter. I have this task that i'm working on right now which is a hostel management system. the student has the ability to choose the meal they want to eat by selecting on it, all im trying to do is to disable the select button after they click on it, in other words the student can choose a specific meal once.
here's my code:
<td><div><?php echo $row['type'];?></div></td <td><div><?php echo $row['content'];?></div></td>
<td><div><?php echo $row['date'];?></div></td>
<td align="center">
<?php $is_selected=$this->db->get_where('selected_meal', array( 'meal_id' => $row['meal_id'],'student_id'=>$this->session->userdata('student_id')))->row()->delivered;
<a data-toggle="modal" href="#modal-form" onclick="modal('order_meal',<?php echo $row['meal_id'];?>)" class="btn btn-gray btn-small">
<i class="icon-wrench"></i> <?php echo get_phrase('select');?>
</a>
<?php ?>
</td>
Here is sample form, button and javascript. You need to modify it to fit your needs.
<form name="myForm" id="myFormId" class="form-horizontal" method="post" action="<?=base_url('invoice/create_invoice');?>">
<input type="submit" id = "formSubmit" class="btn btn-primary" value="Submit">
</form>
<script type="text/javascript">
document.getElementById('myFormId').onsubmit = function() {
document.getElementById("formSubmit").disabled = true;
}
</script>
Give a class to element, which you want to select. Ex 'select-meal'.
$('.select-meal').click(function(){
$(this).attr('disabled', 'true');
})
It seems you are using bootstrap, you can add class "disabled" on click submit
Or, try this, add class dis-btn and write script on event click
$('.dis-btn').prop('disabled', true);

PHP form not saving to table

I have a page called service.php that uses a modal window to open a form. The action on the form was service.php.
<div class="modal hide fade" id="myServiceModal" tabindex="-1" role="dialog" aria-labelleby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Service Failure Form</h3>
</div>
<div class="modal-body">
<p>
<form class="well-small" action="service.php" method="POST" id="serviceModalForm" name="serviceModalForm">
<label>Container Selected</label>
<input type="text" name="containerNumber" id="containerNumber" />
<label>Bol Selected</label>
<input type="text" name="bolNumber" id="bolNumber" />
<input type="submit" id="modal-form-submit" name="submit" class="btn btn-success btn-small" href="#" value="Save" />
<?php
$bol = $_POST['bolNumber'];
$container = $_POST['containerNumber'];
if(isset($_POST['submit'])){
$sql_query_string =
"INSERT INTO import_view_svc_fail (bol, container_num) VALUES
('$bol', '$container');";
if(mysql_query($sql_query_string)){
echo ("<script language='javascript'>
window.alert('Added New Service Failure')
</script>");
}
?>
</form>
This form worked, and it saved to the appropriate table.
Here is my problem: I had to move that form to another page, called dispatch.php. All I did was copy the code, and put it on dispatch.php.
I changed the action of the form to dispatch.php, and that's where I think the problem starts. When I change the action back to service.php, it works for whatever reason.
When I remove the form completely from service.php, the form on dispatch.php no longer works.
I've tried everything to make this work. I removed all of the code from service.php. I even removed the whole file from the folder.
Any insight would be helpful.
You tell the script what to do but you don't tell it to do it.
In order to excecute a your SLQ-query you have to use mysql_query($sql_query_string);
You will also want to connect to your database. Take a look at http://php.net/manual/de/function.mysql-connect.php for more information.
so.. you change the action in service.php:
<form class="well-small" action="dispatch.php" method="POST" id="serviceModalForm" name="serviceModalForm">
Move to dispatch.php
<?php
if(isset($_POST['submit']))
{
$bol = (isset($_POST['bolNumber'])) ? $_POST['bolNumber'] : '';
$container = (isset($_POST['containerNumber'])) ? $_POST['containerNumber'] : '';
if (!empty($bol) && !empty($container))
{
$sql_query_string =
"INSERT INTO import_view_svc_fail (bol, container_num) VALUES
('$bol', '$container');";
// run the query here
print "<br/><br/>".$sql_query_string."<br/><br/>";
}
else { print "<br/><br/>empty values;<br/>"; }
}
else { print "<br/><br/>\$_POST info not received;<br/>"; }
?>
prints (after submit):
INSERT INTO import_view_svc_fail (bol, container_num) VALUES ('input one value', 'input two value');
you probably should check and make sure you got all your post values inside the if(isset($_POST['submit'])) statement, too. or re-work the logic as a whole... it depends if you want to allow blank values, too.
Also, read up on sql injection and why you should learn to use mysqli_ or pdo.

Categories