HELP ME Please. I created a delete process wherein there i a confirm box to ask the user if he is sure or not.
but the confirm box doesnt appear and automatically deletes the data and doesnt even redirect me. Help me plss!
I need to show the confirmation box ("are u sure") and if the input is true. it will delete the data and redirect to other page.
<?php
require_once('connect/connect.php');
?>
<script>
var r = confirm("Are you sure?");
if (r == true)
{
alert(' item successfuly deleted!!');
</script>
<?php
if(isset($_GET['pid'])){
$sql ='Select * FROM product where pid = '.$_GET['pid'];
$qry = mysql_query($sql);
$data = mysql_fetch_array($qry);
$pname = $data['pname'];
$pstock = $data['pstock'];
}
$sq3="INSERT INTO report VALUES('NULL','".$pname."', '" .$pstock. "', 'Item Deleted' )";
$qry3 = mysql_query($sq3);
$sql2 = 'DELETE FROM product where pid = '.$_GET['pid'];
$qry2 = mysql_query($sql2);
?>
<script>
window.location.assign('products.php');
}
</script>
<script>
else
{
window.location.assign('products.php');
}
</script>
you missed the closing brace } of if condition
<script>
var r = confirm("Are you sure?");
if (r == true)
{
alert(' item successfuly deleted!!');
}
</script>
FIDDLE
Try a short if statement
(r == true) ? alert('item successfuly deleted!!') : "" ;
You have to remove the script tags before php tags start. Update the code as below:-
<?php
require_once('connect/connect.php');
?>
<script>
var r = confirm("Are you sure?");
if (r == true)
{
alert(' item successfuly deleted!!');
<?php
if(isset($_GET['pid'])){
$sql ='Select * FROM product where pid = '.$_GET['pid'];
$qry = mysql_query($sql);
$data = mysql_fetch_array($qry);
$pname = $data['pname'];
$pstock = $data['pstock'];
}
$sq3="INSERT INTO report VALUES('NULL','".$pname."', '" .$pstock. "','Item Deleted' )";
$qry3 = mysql_query($sq3);
$sql2 = 'DELETE FROM product where pid = '.$_GET['pid'];
$qry2 = mysql_query($sql2);
?>
window.location.assign('products.php');
}
else
{
window.location.assign('products.php');
}
</script>
Try this and confirm.
Related
<a href='../../include/Query.php?
deletepro=yes&proID=".$row['proID']."&proName=".$row['proName']."' onClick= 'confirmation()'><i
class='fas fa-trash-alt' style='color:Gray' ></i></a>
//Delete Data of a product\
function confirmation
var r= confirm('you want to delete?')
if(r== "true"){
if (isset($_GET['deletepro'])){
if($_GET['deletepro'] == 'yes' AND $_GET['proID'] !=''){
//$sql = "DELETE from main where userid =".$_GET['userid'];
$sql= "UPDATE products SET proDeleted=1 where proID= ".$_GET['proID'];
$query=mysqli_query($con, $sql);
if ($query) {
echo "<script> alert('".$_GET['proName']." delete Successfully');
window.location.href = '../admin/pages/products.php';</script>";
}
else{
echo "error while deleting";
}
}
}
}
</script>
The first popup shows then if a user clicks on ok then delete the product.
I Don't know how to embed or show a popup msg so please someone help to solve my problem. Thanks
You can use js delete confirmation as follows:
function confirmation() {
if (!confirm("Do you want to delete")){
return false;
}
}
I have a registration form (simple html form) that inserts registered users into a database table and this form validates with jQuery validation plugin and I need to reset form after successful registration, but I can't and don't know why.
Here is a javascript (jquery) snippet that I use for ajax form submission, that is according to the jQuery validate way
submitHandler: function() {
$.ajax({
url: "registration_form.php",
type: "POST",
data: $("#form").serialize(),
success: function (result) {
console.log(result);
if(result == '') {
$('#form')[0].reset() /*this doesn't work'*/
$("#errors").empty(); /*this doesn't work'*/
}
else {
$("#errors").text(result);
}
}
});
}
And here is my registration_form.php file.
<?php
date_default_timezone_set('UTC');
$name = htmlspecialchars($_POST['name']);
$s_name = htmlspecialchars($_POST['s_name']);
$email = htmlspecialchars($_POST["email"]);
$ticket = htmlspecialchars($_POST['ticket']);
$date = date('d_m_Y');
$valid_email = filter_var($email, FILTER_VALIDATE_EMAIL);
$table_name = 'registration_' . $date;
if(!$valid_email) {
die ("Fill the correct email");
}
if((strlen($name) <= 1) || (!$s_name) || (!$email) || (!$ticket)){
die ("All the fields should be filled");
}
$connect = mysqli_connect('localhost', 'root', 'password') or die('Connection error');
$create_db = mysqli_query($connect, "CREATE DATABASE IF NOT EXISTS kultprosvet");
$connect_db = mysqli_select_db($connect, "kultprosvet");
$table = "CREATE TABLE $table_name (
id INT(10) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL DEFAULT '',
s_name VARCHAR(100) NOT NULL DEFAULT '',
email VARCHAR(100) NOT NULL DEFAULT '',
ticket VARCHAR(100),
PRIMARY KEY (id)
)";
$result = mysqli_query($connect, "SELECT email FROM $table_name");
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
if($row['email'] == $email){
die('this email is already registered');
};
}
$query_table = mysqli_query($connect,$table);
$query = mysqli_query($connect,"insert into $table_name(name, s_name, email, ticket) values ('$name', '$s_name', '$email','$ticket')");
if(mysqli_errno($connect) > 0){
echo mysqli_errno($connect). ": " . mysqli_error($connect);
}
mysqli_close($connect);
?>
So, now everything works fine, except the form reset.
Can someone show my mistakes and help me to fix this?
P.S. Recently I did the same javascript, but all the data inserted into the .txt file and everything were good, I have no idea why this code doesn't work with database variant.
Add this line of code to reset all textbox, checkbox elements your values in the form
$("input[type=text], textarea").val("");
$('input:checkbox').removeAttr('checked');
Change
if(result == '') { ... }
to
if(result) { ... // show error } else { // reset }
here is my html:
<?php echo ($row['status'] == 1)? ?>
<span id='slide_".$row['id']."' name="slid_id" class='label label-danger'>Inactive</span>
<?php : ?>
<span id='slide_".$row['id']."' name="slid_id" class='label label-success'>Active</span>
<?php ; ?>
here is my jquery code:
$(document).ready(function()
{
$("span").click(function()
{
var id = $(":input[name=slide_id]").val();
alert(id);
});
});
i didn't get anything , i want to get the
ID & its dynamic generated value
of the span element and then perform some action like on click i want to change the status from active to inactive or vise versa.
thanks in advance.
var id = $(":input[name=slide_id]").attr('id'); should do the job. .val() only returns the current value of input elements.
Your HTML is missing from the question, but eliminating the colon in your jQuery selector might do the trick in selecting an input field - i.e. $("input[name=slide_id]").val().
If you want to get the ID attribute of a span element, this would work:
$('span_selector').attr('id');
okay finally i got the way to solve this things. m just sharing this if anyone have same issue one day then this might helpful.
html codes:
<tr class="tr">
<td class="td">
<div id="status">
<?php
echo ($row['status'] == 1)?
"<span id='".$row['id']."' class='label label-danger'>Inactive</span>":
"<span id='".$row['id']."' class='label label-success'>Active</span>";
?>
</div>
</td>
</tr>
my jquery code:
$(document).ready(function()
{
$("#status > span") .click(function()
{
var id = $(this).attr('id');
var tempelement = $(this);
var texts = $(this).closest(".tr").find("#texts").val();
var author = $(this).closest(".tr").find("#author").val();
var status = $(this).text();
$.ajax({
type: 'POST',
url: 'manage_feedback.php',
data: {id:id, texts:texts, author:author, status:status},
success: function(data)
{
if (data == "Active")
{
$(tempelement).removeClass("label label-danger");
$(tempelement).addClass("label label-success");
$(tempelement).html(data);
alert('status changed');
}
else if (data == "Inactive")
{
$(tempelement).removeClass("label label-success");
$(tempelement).addClass("label label-danger");
$(tempelement).html(data);
alert('status changed');
}
else
{
alert(data);
}
}
});
});
php script
//ajax status changer code
if (isset($_POST['id']) && isset($_POST['texts']) && isset($_POST['status']) && isset($_POST['author']))
{
$id = $_POST['id'];
$texts = trim($_POST['texts']);
$author = trim($_POST['author']);
$status = $_POST['status'];
$qry = "SELECT count(id) as count FROM tbl_testimonials WHERE texts = '".$texts."'
AND author = '".$author."' AND id != ".$id." ";
$sql = mysql_query($qry);
$data = mysql_fetch_assoc($sql);
if ($data['count'] == 0)
{
if($status == 'Inactive')
{
$qry = "UPDATE tbl_testimonials SET status = 0 WHERE id = ".$id." " ;
$sql = mysql_query($qry);
if($sql == 1)
{
echo 'Active';
exit;
}
}
elseif ($status == 'Active')
{
$qry = "UPDATE tbl_testimonials SET status = 1 WHERE id = ".$id." " ;
$sql = mysql_query($qry);
if($sql == 1)
{
echo 'Inactive';
exit;
}
}
}
else
{
echo "name already taken";
exit;
}
}
hope it will help someone.
So basically I am using PHP/AJAX/JavaScript/SQL, and I'm new to it all, to display a list of all my users with a button beside each on that says 'Enable' if there is a 1 in my 'Enabled' column in the database or 'Disable' if there is a 0. The button beside each user displays properly when I first load the page, and if the user has an 'Enable' button, it does enable the user when I click on it, the text changes to 'Disable' but it doesn't disable the user when clicked.
If the user has a 'Disable' button when the page is first loaded, clicking it will change it to blank and the user is not disabled.
I can't figure out where I'm going wrong. I'll include my code so far below.
Code for enable/disable button in adminPage.php
if($enabled=='1'){
$allUsers.='<button id="adminButton_'.$username.'" onclick="adminHandler(\'disable\',\''.$username.'\',\'adminButton_'.$username.'\')">disable</button>';
}
else if($enabled=='0')
{
$allUsers.='<button id="adminButton_'.$username.'" onclick="adminHandler(\'enable\',\''.$username.'\',\'adminButton_'.$username.'\')">enable</button>';
}
adminHandler function in adminPage
<script type="text/javascript">
function adminHandler(action,username,elem){
var conf = confirm("Press OK to '"+action+"' this user.");
if(conf != true){
return false;
}
_(elem).innerHTML = "processing ...";
var ajax = ajaxObj("POST", "Includes/adminProcessing.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "enable_ok"){
_(elem).innerHTML = "Disable";
} else if(ajax.responseText == "disable_ok"){
_(elem).innerHTML = "Enable";
} else {
_(elem).innerHTML = ajax.responseText;
}
}
}
ajax.send("action="+action+"&username="+username);
}
The adminProcessing.php page
if (isset($_POST['action']) && isset($_POST['username']))
{
$username = $_POST['username'];
if($_POST['action'] == "enable")
{
$sql = "UPDATE users SET enabled='1' WHERE username='$username' LIMIT 1";
$query = mysqli_query($conn, $sql);
mysqli_close($conn);
echo "enable_ok";
exit();
}
else if($_POST['action'] == "disabled"){
$sql = "UPDATE users SET enabled='0' WHERE username='$username' LIMIT 1";
$query = mysqli_query($conn, $sql);
mysqli_close($conn);
echo "disable_ok";
exit();
}
AJAX File
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x) {
if (x.readyState == 4 && x.status == 200) {
return true;
}
}
Your "adminHandler" is being passed "disable", but you're checking for "disabled."
I've got a form that I am populating from the database using PHP.
I would like a popup using Javascript confirming if the user would like to delete the movie.
The popup should contain the movie & release year.
Thus far, only the first value will appear in the popup...
print("<script type=\"text/javascript\">\n");
print("function confirmDelete(){");
print("var del = document.getElementById('movie').value;\n");
print("var status = confirm('Are you sure you wish to delete: ' + del + '?');\n");
print("return status;\n");
print("}</script>\n");
print("<h1>Delete Record</h1>");
//Select Movies
$query = "SELECT title release_year
FROM movies
ORDER BY title";
$result = mysql_query($query)
or die("<h1>Error - (Movies) the query could not be executed</h1>\n");
print("<form method=\"post\" action\"\">\n");
print("<select id=\"movie\" name=\"movies\">\n");
while ($row = mysql_fetch_array($result))
print("<option value=\"$row[0], $row[1]\">$row[0]</option>\n");
print("</select>\n");
print("<input name=\"select_delete\" type=\"submit\"
value=\"Delete\" onclick=\"return confirmDelete();\">\n");
print("</form>\n");
Whats the best way to tackle this?
Try this:
Javascript
var form = document.querySelector('form');
form.addEventListener('submit', function(e) {
e.preventDefault();
var x = confirm('confirmation message here');
if (x) {
form.removeEventListener('submit');
form.submit();
}
});
you can use this function to confirm user action
....
print("<input name=\"select_delete\" type=\"submit\"
value=\"Delete\" onclick=\"confirmDelete();\">\n");
print("</form>\n");
?>
<script>
function confirmDelete()
{
var e = document.getElementById("movie");
var msg = e.options[e.selectedIndex].value;
var x;
var r=confirm("Delete" + msg + "entry?");
if (r==true)
{
//OK button pressed
}
else
{
//Cancel button pressed
}
}
</script>
You can write the 'delete function' using ajax and most of your code you can simply write like this:
<?php
//code in php
?>
code in javascript
<?php
//more code in php
?>