Deleting specific div with PHP - php

I'm making a To do application in PHP for a school assignment.
At this point, I can add new tasks. But the problem is:
When I click at a div (to delete it), the div is deleted, BUT all the other tasks get invisible. They only show up again if I create a new task.
This is my code
File Index.php
<div class="container">
<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
<input class="addText title" type="text" value="Click to add a task" name="nextToDo">
<input class="clickablePlus" type="submit" value="+" name="submit"></div>
</form>
<?php if(!$empty): ?>
<?php foreach ($_SESSION["todoList"] as $_SESSION["key"] => $toDo): ?>
<div class="toDo">
<form action="/Periodeopdracht/index.php" method="POST">
<button value="<?php echo $_SESSION["key"] ?>" name="delete" class="delete" type="submit" >X</button>
<button value="<?php echo $_SESSION["key"] ?>" name="done" class="done" type="submit" >V</button>
<div value="<?php echo $_SESSION["key"] ?>" class="textToDo"><?= $toDo ?></div>
</form>
</div>
<?php endforeach ?>
<?php endif ?>
</div>
Application.php
<?php
session_start();
$GLOBALS["empty"] = true;
$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();
if(isset($_POST["submit"]))
{
$empty = false;
array_unshift($_SESSION['todoList'], $_POST["nextToDo"]);
}
if(isset($_POST["delete"]))
{
unset($_SESSION['todoList'][$_SESSION["key"]]);
//var_dump($key);
}
?>

Try to handle it like a normal post, since this key will be available on submission:
<!-- no need to create every form -->
<form action="/Periodeopdracht/index.php" method="POST">
<?php foreach ($_SESSION["todoList"] as $key => $toDo): ?>
<div class="toDo">
<button value="<?php echo $key; ?>" name="delete" class="delete" type="submit" >X</button>
<button value="<?php echo $key; ?>" name="done" class="done" type="submit" >V</button>
<div value="<?php echo $key; ?>" class="textToDo"><?= $toDo ?></div>
</div>
<?php endforeach; ?>
</form>
Then, upon submission:
if(isset($_POST["delete"])) {
$key = $_POST["delete"];
unset($_SESSION['todoList'][$key]);
}
Sidenote: Are you sure your form action is /Periodeopdracht/index.php? Maybe this operation is intended to use Application.php like you posted the $_POST processes above.
Also, this is superfluous/unneeded.
$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();
This reassignment makes no sense.
Just use a simple initialization:
if(!isset($_SESSION['todoList'])) {
$_SESSION['todoList'] = array();
}
A Little bit of rewrite:
Form:
<div class="container">
<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
<input class="addText title" type="text" value="Click to add a task" name="nextToDo">
<input class="clickablePlus" type="submit" value="+" name="submit"></div>
</form>
<!-- no need to create every form -->
<form action="/Periodeopdracht/index.php" method="POST">
<?php foreach($_SESSION["todoList"] as $key => $toDo): ?>
<div class="toDo">
<button value="<?php echo $key; ?>" name="delete" class="delete" type="submit" >X</button>
<button value="<?php echo $key; ?>" name="done" class="done" type="submit" >V</button>
<div class="textToDo"><?php echo $toDo; ?></div>
</div>
<?php endforeach; ?>
</form>
</div>
Then in /Periodeopdracht/index.php:
<?php
session_start();
// simple initialization
if(!isset($_SESSION['todoList'])) {
$_SESSION['todoList'] = array();
}
// addition / push inside items
if(isset($_POST["submit"], $_POST["nextToDo"])) {
$_SESSION['todoList'][] = $_POST["nextToDo"];
}
// delete key
if(isset($_POST["delete"])) {
$key = $_POST["delete"];
unset($_SESSION['todoList'][$key]);
}
?>

Related

i want to update my database

1-now already i added some texts on my database then after i want to update my database
anybody can help me?
i hope someone can help me
<?php
mysqli_query($db,"INSERT INTO tasks (task,status)
VALUES ('$task','$status')");
header('location:SIMPLE.PHP');
}
}
if (isset($_GET['check_selected'])) {
foreach ($_GET['check_selected'] as $key => $value) {
$row = mysqli_query($db,"DELETE FROM tasks WHERE id =
$value;") or die(mysqli_error($db));
}
}
$tasks = mysqli_query($db,"SELECT * FROM tasks ORDER BY id DESC");
?>
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="SIMPLE.PHP">
<input type="text" name="search" class="status_input" placeholder="TEXT" />
<input id='textbox' type='checkbox' value='1' name='status'>
<button type="submit" class="add_btn" name="submit">Add</button>
<p><?php echo $errors; ?></p>
</form>
<form action="SIMPLE.PHP" method="GET">
<tbody>
<tr>
<p><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
<?php
foreach ($rows as $row_id => $row)
{
?>
<input name="check_selected[]" type="checkbox" value=<?php echo $row['id'];?>"/>
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['status'];?>"
<?PHP if($row['status']){echo 'checked';
}
?>
>
<td class= "task"><?php echo $row['task']?></td>
<?PHP
}
?>
</tr>
</tbody>
</form>
</body>
</html>

How to get values of selected dropdown box display in url

I'm setting up an eCommerce website and I need to select product quantity user enters in drop down. I have tried this but it gives me last quantity not the selected. I'm still in the process of creating the addtocart.php file but before that I need to pass the values so that I can use them in a later stage.
I have implemented the following code
<div class="product-cartq">
<div class="product-quantity pull-left">
<span>Quantity:</span>
<select name="quantity_option">
<?php for($qty = 1; $qty < 11; $qty++){
echo '<option value="'.$qty.'">'.$qty.'</option>';
}?>
</select>
</div>
<div class="btn_addtocart">
<form method="GET" action="cart.php" enctype="multipart/form-data">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="quantity_option" value="<?php echo $qty; ?>">
<input class="btn btn-primary btn-submit" type="submit" name="add_to_cart" value="add to cart"/>
</form>
</div>
</div>
I can't proceed as I'm unable to get the value. Any suggestions or improvements on this?
Do this:
<form method="GET" action="cart.php" enctype="multipart/form-data">
<div class="product-cartq">
<div class="product-quantity pull-left">
<span>Quantity:</span>
<select name="quantity_option">
<?php for($qty = 1; $qty < 11; $qty++){
echo '<option value="'.$qty.'">'.$qty.'</option>';
}?>
</select>
</div>
<div class="btn_addtocart">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="quantity_option" value="<?php echo $qty; ?>">
<input class="btn btn-primary btn-submit" type="submit" name="add_to_cart" value="add to cart"/>
</div>
</div>
</form>
The select control is outside form tag.

PHP to reload page from modal

i am trying to reload page using onClick="location.href='Edit_Equip.php?txtidforsite='" /> but i get Notice: Undefined variable: id
i have tried to define id but still get errors any help would be great
<?php
include('connect.php');
if(isset($_POST['txtidforsite']))
{
$id=$_POST['txtidforsite'];
if(isset($_POST['submit']))
{
$query3 = mysql_query("UPDATE ftn_site_equip SET siteidequip='".$_POST['txtidforsite']."', patype='".$_POST['txtpa']."', drxtype='".$_POST['txtdrx']."' WHERE siteidequip='$id'");
if($query3)
{
header('refresh:2; url=#openModa3');
}
}
$query1=mysql_query("select * from ftn_site_equip where siteidequip='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="" align="center">
<input name="txtidforsite" value="<?php echo $query2['siteidequip']; ?>" size="36" class="siteinfotable"/>
<input name="txtdrx" value="<?php echo $query2['drxtype']; ?>" size="36" class="siteinfotable"/>
<input name="txtpa" value="<?php echo $query2['patype']; ?>" size="36" class="siteinfotable"/>
<input type="submit" name="submit" class="update_record_button" value="Update Record" onclick="window.location='#openModal2';" />
</form>
<?php
}
?>
<div id="openModal" class="cancel_modal">
<div align="center">
<h2 align="center">Are you sure you want to cancel all changes ?</h2>
<input name="yes" type="button" value="Yes" class="yes_button" onClick="location.href='Edit_Form.php?txtid='" />
<input name="no" type="button" value="No" class="no_button" onclick="location.href='#close'"/>
</div>
</div>
<div id="openModal2" class="cancel_modal">
<div align="center">
<img alt="" src="Images/tick.jpg"/><br/>
<h2 align="center">Record Successfully Updated</h2>
</div>
</div>
<div id="openModa3" class="cancel_modal">
<div align="center">
<h2 align="center">What Next ?</h2>
<input name="yes" type="button" value="Retrun to Site Information" class="yes_button" onClick="location.href='Edit_Equip.php?txtidforsite=<?php echo $id; ?>'" />
<input name="no" type="button" value="Search for Another Site" class="no_button" onclick="location.href='search.php'"/>
</div>
</div>

ddslick plugin: get value in wordpress

I have a choice menu in WordPress plug-in with ddslick . I want to pass the id of the selected option and send the form to a database , but the value is not saved although it appears within the html . When put into manual is well maintained.
<script type='text/javascript' src='http://g-page.co.il/wp-content/themes/g-page/js/jquery.ddslick.min.js'></script>
<div id="myDropdown">
<select id="demo-htmlselect">
<?php
$thumbnails = get_posts('numberposts=5');
foreach ($thumbnails as $thumbnail) {
$url = wp_get_attachment_url( get_post_thumbnail_id($thumbnail->ID) );
?>
<option value="<?php echo $thumbnail->ID;?>" data-imagesrc="<?php echo $url; ?>"
data-description="<?php echo $thumbnail->post_title;?>"><?php echo $thumbnail->post_title;?></option>
<?php } ?>
</select>
</div>
<?php
?>
<div class="wrap">
<form method="post" action="options.php">
<?php wp_nonce_field('update-options') ?>
<input type="text" name="main_art_id" size="45" value="<?php echo get_option('main_art_id'); ?>" />
</p>
<p><input type="submit" name="Submit" value="Store Options" /></p>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="ain_art_id" />
</form>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#myDropdown').ddslick({
onSelected: function(selectedData){
var cor_val = selectedData['selectedData']['value'];
$("input[name=main_art_id]").val(cor_val);
}
});
});
</script>
<?php
}

PHP Code with HTML

I am creating a simple MVC site that uses CRUD. For the delete function I have a button, the code below is what I am trying to use to send the rowID to the controller to handle.
I am having trouble in that the code doesnt print out on the screen correctly, The php code is being printed as well as the button.
The rowID isnt being passed either and I am wondering if my approach with the php code is the correct way?
Code
<?php
$itemsDAO = new ItemsDAO();
$result=$itemsDAO->getItems();
foreach ( $result as $row ) {
$uid = $row['id'];
var_dump($uid);
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Delete Function
public function getItems () {
$sqlQuery = "SELECT *";
$sqlQuery .= " FROM items";
$result = $this->getDbManager()->executeSelectQuery($sqlQuery );
return $result;
}
You could try a string replace in you forloop if you have one.
I think it would look something like this
foreach ( $this->model->itemList as $row )
$HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" .
$row ["description"] . " " . str_replace("value2replace", $row['id'], $buttons) . "" . $update . "</blockquote></li>";
And then you would change your form to the following
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId"
value="value2replace"> <input type="submit"
class="btn btn-success" value="Delete" />
</div>
</div>
</fieldset>
Assuming you want a delete form for each and every result, you need to wrap the HTML inside the foreach statement:
<?php
$result=$itemsDAO->getItems();
foreach ($result as $row) {
$uid = $row['id'];
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fID" name="uid" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>

Categories