Deleting objects using checkboxes with Codeigniter - php

I have read many post like this but have failed to find my particular situation.Trying to delete the selected checkbox. right now you can submit the form and it takes you to all the right pages except it doesn't actually delete anything.
Here is my controller info
function deleteFolder() {
if(array_key_exists('deleteMe',$_POST)) {
$checkbox = $this->input->post['checkbox'];
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
Here is my Model
function deleteFolder($checkbox) {
$this->db->where('folderName', 'folderName');
$this->db->delete('senior', $checkbox);
return;
}
Here is my View
<!DOCTYPE html>
<?php $this->load->view('partials/page_head'); ?>
<body>
<div id="container">
<div id="top">
<div class="topcenter">
<h2><a class="homebtn" href="<?php echo base_url();?>">Home</a></h2>
</div>
<div class="navdescription"><span>Delete Page</span></div>
</div>
<div class="projectFolders">
<?php foreach($foldername as $row) { ?>
<div class="folder">
<button><?php echo $row->folderName; ?></button>
<div class="delete">
<form name="delete" method="post" action="<?php echo base_url(); ?>index.php/home/folderdeleted">
<p>
<input type = "checkbox" id = "check_<?php echo $row->folderName; ?>"/>
<?php echo form_submit('deleteFolder', 'Delete'); ?>
</p>
</form>
</div>
</div>
<?php } ?>
</div>
</div><!-- End of container div -->
</body>
</html>

There are several errors in your code. I'm not sure whether I've found all of them and whether the code will work.
The controller should be:
function deleteFolder() {
if($this->input->post('checkbox') !== false) {
$checkbox = $this->input->post('checkbox');
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
The model should be:
function deleteFolder($checkbox) {
$this->db->where('folderName', $checkbox);
$this->db->delete('senior');
return;
}
The input tag in the view should be:
<input name="checkbox" value="<?php echo $row->folderName; ?>" type = "checkbox" id = "check_<?php echo $row->folderName; ?>"/>
A little warning for checkboxes: if they aren't checked, you won't find anything in the $_POST variable.

Related

Posting form with ajax to self with no reload and filter back in form data with relevant business names

I'm trying to auto submit a form when a user clicks checkbox select (with ajax). I want to prevent the default reload, and then attempt to use the category checkbox values to repopulate the relevant business names, which the user will be able to select to retrieve the full company details.
Quite frankly, it's been driving me a bit mad! I've tried both post and get methods, and 100 other workarounds and I just can't get it to work
If I remove the e.preventDefault(); from my on submit function then the page reloads and I get the correct info across both category and name via $_GET. But problem I have is getting the ajax data passed back to the var_dump($_GET). It always stays empty aside from the URL.
Am I going about this in the wrong way?
Here's my form:
<form id="businessSearch" action="" type="get" enctype='multipart/form-data'>
<div class="col-md-6 business-cat">
<h2>Business Category</h2>
<div class="business-inner">
<input id="category" class="main" <?php if ( !isset($_GET['category']) || ($_GET['category'] == 'All')) { echo 'checked'; } ?> type="checkbox" name="category" value="All" /> All <br>
<?php foreach($data['businessCats'] as $category) : ?>
<input id="category" class="main" <?php if ( isset($_GET['category']) && ($_GET["category"] == $category->BusinessCategory)) { echo 'checked'; } ?> type="checkbox" name="category" value="<?php echo $category->BusinessCategory; ?>"> <?php echo $category->BusinessCategory; ?><br>
<?php endforeach; ?>
</div>
</div>
<div class="col-md-6 business-name">
<h2>Company Name</h2>
<div class="business-inner">
<?php if( isset($_GET['category']) && ($_GET['category'] != 'All')) : ?>
<?php foreach($data['businessCategoryListing'] as $businessCatListing) : ?>
<input id="name" class="sub" <?php if (isset($_GET["name"]) && ($_GET["name"] == $businessCatListing->company_name)) { echo 'checked'; } ?> type="checkbox" name="name" value="<?php echo $businessCatListing->company_name; ?>"> <?php echo $businessCatListing->company_name; ?><br>
<?php endforeach; ?>
<?php else: ?>
<?php foreach($data['getAllBusinessListings'] as $getAllBusinessListings) : ?>
<input id="name" class="sub" <?php if (isset($_GET["name"]) && ($_GET["name"] == $getAllBusinessListings->company_name)) { echo 'checked'; } ?> type="checkbox" name="name" value="<?php echo $getAllBusinessListings->company_name; ?>"> <?php echo $getAllBusinessListings->company_name; ?><br>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</form>
<?php if ( isset($_GET['category']) && isset($_GET['name']) ) : ?>
<div class="clearfix"></div>
<div class="col-md-12 business-details">
<h2>Details</h2>
<div class="business-inner">
<h2><?php echo $data['businessListing']->BusinessName ?></h2>
<?php echo $data['businessListing']->BusinessDescription . '<br>' . $data['businessListing']->BusinessPhone . '<br>' . $data['businessListing']->BusinessWebsite . '<br>' . $data['businessListing']->BusinessAddress1 . '<br>' . $data['businessListing']->BusinessGrid . '<br>' ; ?>
</div>
</div>
<script>
$('input.main, input.sub').on('change', function() {
$('#businessSearch').trigger('submit');
});
$(document).ready(function () {
$('#businessSearch').on('submit', function(e) {
e.preventDefault();
var category = $('#category:checked').val(),
name = $('#name:checked').val();
$.ajax({
type: 'get',
data: { ajax: 1,category: category, name: name},
success: function(response){
//$('#response').text('category : ' + response);
}
});
});
});
</script>
I can see my output in the network tab in
I just cannot get it to filter back in the $_GET var_dump and then get the results to update correctly on my page whilst preventing the page reload.
Finally, here is how I'm calling that data from my db
public function getBusinessByCategory() {
$category = isset($_GET['category']) ? $_GET['category'] : '';
$this->db->query('SELECT * FROM business_directory WHERE category = :category and publish_status = "live" ORDER BY company_name ASC');
$this->db->bind(':category', $category);
$results = $this->db->resultSet();
return $results;
}
Can anyone give any pointers? I'm really stuck, and my head is about to explode!!!
If you want submit a form without reload the page then first do not user button type "submit" in HTML form. Instead of that use normal button and execute your ajax call when user click on that button.
If you will submit button then it will reload the page and it's default behaviour of the submit button type.

PHP (5.5) - database search with MySQLi - getting datalist but no output

I'm just learning how to do this so please forgive my ignorance!
Here is my test site: http://webtestkit.com/1KaraokeDJ/index.php
First, I found this code in a learning example and that code worked just fine. (http://www.mostlikers.com/2013/08/search-engine.html) - no problems. I even made the sample database and checked all was working.
now I wanted to change it to work for my purpose (a karaoke search)...
here is my code:
<?php
include("connect.php");
session_start();
if(isset($_POST['submit']))
{
$search=$_POST['search'];
$_SESSION['title']= $search;
if(($_SESSION['title'])!="")
{ header("location:index.php"); }
else
{ echo "<script> alert('Please enter something to search for') </script>"; }
}
?>
<html>
<head>
<title>1KaraokeDJ.com</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="login">
<form method="post">
<p><img src="top.jpg" /></p>
<p>
<?php if(isset($_SESSION['title'])) { ?>
<input name="search" type="search" list="searchkey" value="<?php echo $_SESSION['title'];?>" class="search" />
<?php } else { ?>
<input name="search" type="search" list="searchkey" placeholder="Just type your text here and press enter - ex : Abba" class="search" />
<?php } ?>
</p>
<datalist id="searchkey">
<?php
$tile=$db->query("SELECT * FROM `1KaraokeDJ.com`");
while($storetitle=mysqli_fetch_object($tile))
{ ?>
<option value="<?php echo $storetitle->title ?>">
<?php } ?>
</datalist>
<p><input type="submit" name="submit" id="click" class="searchbutton" value="Karaoke Search" /></p>
<?php if(isset($_SESSION['title'])) {
if(($_SESSION['title']!=""))
{
$data=$_SESSION['title'];
$view=$db->query("select * from 1KaraokeDJ.com where title like '%$data%' limit 10");
$check=mysqli_num_rows($view);
if($check!="")
{
while($descri=mysqli_fetch_object($view))
{
?>
<div class="reslt">
<h3 id="resuil-title"><?php echo $descri->title; ?></h3>
<p class="Description">
<?php $description = str_replace($data, '<span class="highlight">'.$data."</span>", $descri->artist);
echo $description; ?>
<p>
<hr>
</div>
<?php } } else { ?>
<div class="reslt">
<h3 id="resuil-title">Nothing fond!</h3>
<p class="Description">Try changing your search terms<p><hr>
</div>
<?php } } } ?>
</form>
</div>
</body>
</html>
The search field is finding data in the dropdown list so the connection is fine and the search works.
I think I understand most of the code, but I don't seem to understand this:
$view=$db->query("select * from 1KaraokeDJ.com where title like '%$data%' limit 10");
$check=mysqli_num_rows($view);
if($check!="")
{ while($descri=mysqli_fetch_object($view)) {
My If always goes to else "Nothing Found"
Any help? The way I learn is by doing - so it's trial and error until I figure it out!
Your table 1KaraokeDJ.com is interpreted as DatabaseName.TableName
To avoid this, escape your table name
Select * from `1KaraokeDJ.com` Where ...

Deleting divs via PHP

I'm making a To do application with PHP (school assignment)
At this moment, I can add tasks to the list. Deleting is the next problem.
The "problem" is that I HAVE to use PHP to delete the corresponding div. (It needs to delete the div i'm clicking)
My question: what's the best practice to do that? (Working with a specific number maybe?)
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="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['done'])) {
foreach ($_SESSION["todoList"] as $key => $toDo) {
if ($toDo == $_POST['done']) {
unset($_SESSION['todoList'][$key]);
break;
}
}
}
?>
foreach ($_SESSION["todoList"] as $key => $toDo)
then use $key as the value of your "done" button, When processing you can just unset($_SESSION['todoList'][$key])
you will have to check that the key is valid coming from the post though.
Add a <hidden> field to each toDo with, the id of the toDo, then you will know what to remove from the toDo list.
In addition to what exussum was stating. U'll need to define in your HTML which todo item u want to delete. Atm your just posting an empty button. Change your html to something like this:
<div class="toDo">
<form action="/../index.php" method="POST">
<button name="done" class="done" value="<?= $toDO ?>" type="submit">V</button>
<div class="textToDo"><?= $toDo ?></div>
</form>
</div>
Now if you post the form the variable $_POST['done'] will contain the task that is completed. Now the check this :
<?php
if (isset($_POST['done']) {
foreach ($_SESSION["todoList"] as $key => $toDo) {
if ($toDo == $_POST['done']) {
unset($_SESSION['todoList'][$key]);
break; //terminates the loop as we found the correct item
}
}
$empty = empty($_SESSION["todoList"]);
}
I'm not a huge fan of posting raw values to check whether items need to be deleted.
Its beter to work with (unique) id's if you are using these.

Add a classname after form is submitted and page reloads

Can anyone please help me on this:
I am trying to add a classname/ change the css of div using jquery; after I click submit on the form. Means, after clicking submit and when the page reloads this classname/ changes should stick to the target element. I am using php to get the values of form submitted. (no ajax, but if necessary I can modify my code). I tried to do this by jquery but when the page reloads the changes are gone !
Thanks.
update: Here is the sample code(so.php itself) I am working on. after form is submitted and page is reloaded; say I want to add a class to input that is clicked and manage the style in CSS:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<div id="container">
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
update2: I tried the logic provided by #NoPyGod and it worked. But here is the thing. I am trying to add a class to the button that is selected as well as update the text in the div with id "hello" with the value of input that is clicked or in active state once the form is submitted and page is reloaded. This is the one I ended up but once form is submitted
all the inputs are having the same class name as well the text in div is also not updated.
<html>
<head>
<?php if (isset($_GET['Load'])): ?>
<script>
$(document).ready(function() {
$('#hello').text('Hello');
});
</script>
<?php endif; ?>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Google" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Rediff" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Yahoo" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Sify" />
</form>
<div id="container" >
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
if (isset($_GET['Load1'])) {
$value = $_GET['Load1'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
<div id="hello"></div>
</body>
</html>
When the page is submitted, set a variable like so
$was_submitted = true;
Then in your HTML do something like this
<div id="myid" class="<?php if ($was_submitted) { echo "some_class"; } ?>">
Or if you absolutely insist on using jQuery to change it, include this in your html --
<?php if ($was_submitted): ?>
<script>
$(document).ready(function() {
$("#myid").addClass("some_class");
});
</script>
<?php endif; ?>
Updated:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<!-- I changed this line below -->
<div id="container" class=<?php if (isset($_GET['Load'])) { echo "some_class"; } ?>>
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>

how to set a controller variable in a view from a form post

I have a Zend view in PHP with a form:
<form action="https://example.com/checkout/" name="info" method="post">
<div id="payment">
<div id="paypal>
<?php $this->is_paypal_payment = true; ?>
</div>
<div id="credit_card>
<?php $this->is_paypal_payment = false; ?>
</div>
</div>
</form>
When a user clicks the submit button, how can I set the instance variable in the Controller?
This is what I tried which didn't work.
Controller:
$this->view->payment_option_paypal = false; // initialized value
$payment_method_option = $this->_getParam('payment_option_paypal', null);
if (!is_null($payment_method_option)) {
if ($payment_method_option == 'paypal') {
$this->_is_paypal = true;
}
}
View:
<div id="paypal" style="display: none;">
<?php $this->payment_option_paypal = "paypal"; ?>
<img src="paypal.gif" style="margin-right:20px;">
</div>
After clicking submit, the _getParam() function doesn't capture the string "paypal" that the view set for instance variable in Controller $this->payment_option_paypal
Any ideas what's wrong and how to go about this?
in you phtml page you have to use like this
<div id="paypal" style="display: none;">
<input type="hidden" name="paypalname" value="<?php echo $this->payment_option_paypal = "paypal"; ?>" id="paypalname"/>
<img src="paypal.gif" style="margin-right:20px;">
</div>
and the controller
function yourAction()
{
if ($this->_request->isPost())
{
$paypalvalue=$this->_getParam('paypalname');
}
}
I hope it helps you and if you have any problem in my answer then let me know.
a div will not submit data also anything that needs to be rendered as html must be echo'ed:
<form action="https://example.com/checkout/" name="info" method="post">
<div id="payment">
<div id="paypal>
<?php echo $this->is_paypal_payment = true; ?><!-- maybe should be an input element of kind and echo the $this-> statement -->
</div>
<div id="credit_card>
<?php echo $this->is_paypal_payment = false; ?><!-- maybe should be an input element of kind and echo the $this-> statement -->
</div>
</div>
</form>
again ECHO:
<div id="paypal" style="display: none;">
<?php echo $this->payment_option_paypal = "paypal"; ?>
<img src="paypal.gif" style="margin-right:20px;">
</div>

Categories