how to post the data with out click on checkbox in cakephp? - php

I have form where the select boxes are there and select all option, i want to post all the data without click on check boxes, i dont want to view this page, mean if user go to signup page the data will already selected by default how can i do that wothout any view?
this is my view code
<?php v_start($this);?>
<?php
#if(element exits of controller-action)
echo $this->element('validations/users/signup');
?>
<script>
function toggleChecked(status)
{
jQuery(".new-checkbox input").each( function() {
jQuery(this).attr("checked",status);
}
)
}
</script>
<h1><span style="color:CornflowerBlue"><?php echo __l('Step1');?></span></h1>
<?php
$form = $this->FormManager;
echo $form->create('User',array_merge($form_options,array('default'=>true)));
?>
<table width = "100%">
<tbody>
<tr>
<td>
<?php echo $form->input('',array('id'=>'mark-all','onclick'=>'toggleChecked(this.checked)','type'=>'checkbox','label'=>'Select All Products' ));?>
<?php echo $form->input('product_id',
array(
'multiple'=>'checkbox',
'options'=>$products,
'div'=>false,
'class'=>'new-checkbox'
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
echo $this->FormManager->end(LBL_BTN_NEXT);
?>
</td>
</tr>
</tbody>
</table>
i dont want this page, the user will select the data by default.
this is my controller code
$products = $this
->User
->ProductsUser
->Product
->find('list',array(
'fields' => 'product_id,name',
'conditions'=>array(
'Product.is_visible'=>true
)
)
);
$this->set('products',$products);
if($this->request->is('post'))
{
$this->Session->write('signUp.signUpProduct',$this->request->data['User']);
$this->redirect(array('action'=>'signup_product'));
}
}
how can i do that, Thanks in advance.

The attr() doesn't take true or false as an argument.
jQuery(this).attr("checked","checked"); // Correct usage of attr()
Although I think attr is deprecated for the favorable prop() method.
jQuery(this).prop("checked",status);
Which indeed does take a true/fales argument. :)
Hope this helps.

Related

Getting an info from the DB with Tables and Updating it

Hi guys im having a difficulty with this scenario:
I want to get the product information using modals, i got this following code:
//Model.php
public function getProduct($product_id){
$this->db->select('product_id,product_name,product_price,product_qty');
$this->db->from('tbl_products');
$this->db->where('product_id',$product_id);
$query = $this->db->get();
return $query->result();
}
//Controller.php
public function view_product(){
$product_id = $this->input->post('product_id');
$this->load->view('header');
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
//update_product.php (modal) my View
lets just go straight into the form
<form action="" method="post">
<?php foreach($posts as $post){ ?>
<input type = "hidden" name = "product_id" value = "<?php echo $post->product_id;?>"/>
<input type = "text" name = "product_name" value = "<?php echo $post->product_name;?>"/>
<input type = "text" name = "product_price" value = "<?php echo $post->product_price;?>"/>
<input type = "text" name = "product_qty" value = "<?php echo $post->product_qty;?>"/>
<button type="submit">Update</button>
<?php } ?>
</form>
I got a table already: i can see all products, in my product_page.php
Here is the tables face looks like:
ID Name Price Quantity Option
1 Shoes 150.00 1 Update
2 Liquor 67.50 5 Update
3 Paint 1000.00 5 Update
Once I click the Update button, the update_product.php(a modal) will pop up and get the result of 1 of the product, if I press the first Update only the information for Shoes will be inside the modal, at first i tried it, I get all the information of all the products which makes my modal redundant and looping due to foreach, then I tried getting the information from the table ID itself, and no product pops out, how can I see only 1 product using modal? thank you very much maam and sirs. Please I really need youre help :(
Although I'm not really clear what you're asking, Here is an answer to you question. Your controller code doesn't seem to be making any sense.If you want to display all your products in on page and then you want to edit/update a product when clicked on corresponding update link, here is what you can do.
Use single controller method for list and update
//Controller.php
public function view_product(){
$product_id = (isset($this->input->post('product_id')) ? $this->input->post('product_id'): False ;
if($product_id == False){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
} else{
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('modal/update_product',$this->data);
}
}
This is simple modification to your code to make it work correctly, but since this code cannot handle form submission of the update form (unless you're pointing update form to a different controller), you will have to add some other code to this controller and your code will get messy in no time. My personal suggestion to you is this,
Use different controllers for list view and update
//Controller.php
public function view_product(){
$this->data["post"] = $this->Model->ProductList();
$this->load->view('product_page',$this->data);
}
public function update_product($product_id){
if($this->input>post('submit')){ // 'submit'should be replaced with the name attribute of your submit button
//call your model and handle the update form submission here
}
$this->data["post"] = $this->Model->getProduct($product_id);
$this->load->view('product_page',$this->data);
}
now, update option of your product_page.php should point to 'update_product' controller method with corresponding product id e.g. {base_url}/controller_class_name/update_product/1
I think your product_page.php has some code like this to loop through all the products and display products in a table, now when you click update link it will point to update_product controller and it will handle the update process.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php if(!$products){ ?>
<tr>
<td colspan="5">No result</td>
</tr>
<?php } ?>
<?php if($products){ ?>
<?php foreach ($products as $product) { ?>
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->name; ?></td>
<td><?php echo $product->price; ?></td>
<td><?php echo $product->quantity; ?></td>
<td>
<a href="<?php echo base_url(); ?>controller_class_name/update_product/<?php echo $product->id; ?>" >Update</a>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
You won't be needing a foreach loop inside your update_product.php because you're updating only one product at once.
Hope this is the answer you're looking for, if not please comment and I will edit the answer accordingly.

multiple deleting with checkbox cakephp

I have a list of post and i want to delete them using multiple checkboxes.
I followed this link Multiple Check boxes in cake php
but i get this error(i use cakephp 2.4):
The view for PostsController::deleteSelect() was not found.
Confirm you have created the file: C:\xampp\htdocs\cakephp2\app\View\Themed\Cakestrap\Posts\delete_select.ctp
I want to access this data from index.ctp not from delete_select.ctp. My question is how i access this data "data['Post']['box'][]"?
My code is:
index.ctp
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'])); ?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
<td>
<?php echo $this->Form->checkbox('post',
array(
'value' => $post['Post']['id'],
'name' => "data['Post']['box'][]",
));?></td>
<td>
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Post']['id']),
array('confirm' => 'Are you sure?'));
?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
<p><?php echo $this->Html->link('deleteSelect', array('action' => 'deleteSelect')); ?></p>
deleteSelect function
public function deleteSelect(){
if(!empty($this->data)) {
foreach($this->data['Post']['box'] as $key => $value){
$this->Post->delete($value);
}
$this->redirect(array('action' => 'index'));
}
}
you have to include all your checkboxes in a form if you want some data passed to your action.
But you can't do this since you are using Form::postLink that creates a form and you can't nest a form inside another form.
So you have to get rid of your postLinks. Are you sure you need them? Can't them all be simple links?
Once you have removed your postlinks then you can put all your code inside a big form
echo $this->Form->create('Post', array('action' => 'deleteSelect'));
// your foreach code here
echo $this->Form->end('Delete selected posts');
also: in your controller put this piece of code
$this->redirect(array('action' => 'index'));
outside the if condition
so the page will be redirected even if no data is passed (no checkbox is checked)
There is no post data
The last line of the view file is:
<p><?php
echo $this->Html->link(
'deleteSelect',
array('action' => 'deleteSelect')
);
?></p>
Unless there is some javascript listening for a click - that's just a link, not something which submits a form as such that means there is no form data. Given that, the relevant controller action does not enter the appropriate if and therefore attempts to render a view file:
public function deleteSelect(){
if(!empty($this->data)) {
...
// Unreachable
$this->redirect(array('action' => 'index'));
}
}
To prevent the problem mentioned in the question - simple don't make redirecting dependent on the presence of form data:
public function deleteSelect(){
if(!empty($this->data)) {
...
}
// Always executed
$this->redirect(array('action' => 'index'));
}
But that won't address the main problem that as written, it will simply do nothing.
Inputs need to be in a form
For inputs to do anything (ignoring the use of javascript) they need to be in a form. Therefore, the raw html needs to change from:
<table>
...
<input type="checkbox">
...
<input type="checkbox">
...
<input type="checkbox">
</table>
<p>
<a href="/deleteSelect" ...>deleteSelect</a>
</p>
To:
<form action="/deleteSelect" ...>
<table>
...
<input type="checkbox">
...
<input type="checkbox">
...
<input type="checkbox">
</table>
<input type="submit" value="deleteSelect">
</form>
I.e.:
Wrap the table in a form
Define the form action to go to the appropriate function
The link must change to a submit button.
In this way, the desired result can be achieved.
Warning - nested forms
Know that it's not valid to put a form inside another form. Therefore to get the "multi-delete" function to work using a normal form, will require removing the individual delete buttons since they are also embedded forms. An alternative technique to using postLink would be to make them normal links and use a simple javascript handler to prevent submitting via get, for example:
$('a.delete').click(function(e) {
if (confirm('Sure?')) {
$.post(this.attr('href')});
}
return false;
});
CakePHP requires that, if you're calling a function in a controller, you have a corresponding .ctp file, as noted.
To get around this you can use $this->autoRender = false; inside your method.
i have implemented it in my code just see my code.
<div>
<?php //echo $this->Form->create();
echo $this->Form->create(null, array(
'url' => array('controller' => 'manageParcels', 'action' => 'deleteParcel')
));
?>
<table width="100%" border="1">
<tr>
<th></th>
<th>Parcel Number</th>
<th>Consignment Number </th>
<th>Customer Name </th>
<th>Customer Address </th>
<th>Customer Phone-Number </th>
<th>Customer EmailId </th>
</tr>
<?php foreach($parcelDatas as $parcelData){
?>
<tr>
<td><input type="checkbox" name ="status[]" value="<?php echo
$parcelData['ManageParcel']['id']; ?>"></input></td>
<td align='center'><?php echo $this->html->link($parcelData['ManageParcel']
['parcelNo'], array('action' => 'editParcel',$parcelData['ManageParcel']['id']),
array('escape' => false));?> </td>
<td align='center'><?php echo $parcelData['ManageParcel']['ConNo']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusName']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusAddress']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusPhone']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusEmail']; ?></td>
</tr>
<?php
}?>
</table>
<?php
echo $this->Form->end(__('Delete Parcel')); ?>
</div>
**My controller code**
public function deleteParcel()
{
$this->autoRender=FALSE;
if ($this->request->is('post'))
{
if(empty($this->request->data))
{
$this->Session->setFlash(__('Please select parcel '));
return $this->redirect(
array('controller' => 'ManageParcels', 'action' =>
'listParcel')
);
}
$deleteParcels=$this->request->data['status'];
$size=sizeof($deleteParcels);
foreach ($deleteParcels as $deleteParcel)
{
$this->ManageParcel->id = $deleteParcel;
$parcelData=$this->ManageParcel->findById($deleteParcel);
if ($this->ManageParcel->delete()) {
$this->recordActivity('deleteParcel','Parcel
Number '.$parcelData['ManageParcel']['parcelNo'] . ' deleted' );
$this->Session->setFlash(__('Parcel data deleted'));
}
else {
$this->Session->setFlash(__('Parcel data was not Deleted'));
return $this->redirect(array('action' => 'listParcel'));
}
}
$this->recordActivity('deleteParcel',$size.' Parcels data deleted ');
return $this->redirect(
array('controller' => 'ManageParcels', 'action' => 'listParcel')
);
}
}

CodeIgniter - 2 forms in a view

I have a view which has 2 forms:
<table>
<th>Write a comment.</th>
<tr>
<td>
<?php echo form_open($this->uri->uri_string(),$form1);
echo form_textarea($comment);
echo form_submit('submit','submit');
echo form_close();
?>
</td>
</tr>
</table>
<table>
<tr>
<td>
<?php echo form_open($this->uri->uri_string());
echo form_dropdown('portion', $portion_options);
echo form_submit('book','book');
echo form_close();
?>
</td>
</tr>
</table>
In the controller I check which button was clicked and then I perform some action by adding the corresponding form's values to the database.
if(isset($_POST['book']))
{
//sending the data to the database
echo "Book button clicked";
}
if(isset($_POST['submit']))
{
//sending the data to the database
echo "Submit button clicked";
}
However when the 'book' button is clicked no action is performed. It is like the button was never clicked. Whereas when I click the 'submit' button, every action is done properly.
In the past I have used the same technique on plain php (i mean no framework, just php), and has worked fine for me. Does codeigniter need any further configuration? Am I doing something wrong?
Why not add a hidden field to both forms called form_idwith values 1 and 2 respectively? Easy to catch in your controller upon post; e.g.:
if($this->input->post()){
switch($this->input->post('form_id')){
case 1:
// do stuff
break;
case 2:
// do stuff
break;
}
}
<?php echo form_open($this->uri->uri_string(),$form1);
and
<?php echo form_open($this->uri->uri_string());
Looks like you forgot to provide the settings in the second one like:
<?php echo form_open($this->uri->uri_string(),$form2);
Well after spending my whole day on this I finaly managed to solve it somehow (although I believe it is not such a propper way to handle this).
Well:
$comment = array(
'name' => 'comment',
'id' => 'comment',
'value' => 'write you comment',
'row' => '5',
'cols' => '100'
);
<table>
<th>Write a comment.</th>
<tr>
<td>
<?php echo form_open($this->uri->uri_string());
echo form_hidden('form_id', 1);
echo form_textarea($comment);
echo form_submit('submit','submit');
echo form_close();
?>
</td>
</tr>
</table>
<table>
<th>Write a comment.</th>
<tr>
<td>
<?php echo form_open($this->uri->uri_string());
echo form_hidden('form_id', 2);
echo form_dropdown('comment', $portion_options);
echo form_submit('book','book');
echo form_close();
?>
</td>
</tr>
</table>
Probably the forms fields (textarea and dropdown) needed to have the same name (which i have set to 'comment'). Although I do not understand why :/
Thank you all for trying to help me :)

passing a php array item into a javascript popup window to be edited and updated in mysql db

I am creating a diabetes management system for a university project. One of the features of this system is for the patient to be able to send latest glucose readings and for the nurse to be able to login and comment on those readings.
I have been able to code the patient feature, however I wish to add a comment button in the comments column, which when clicked, brings up a popup window or a textbox for the nurse to be able to comment on that specific record. If a comment has not already been entered, an empty box should come up, however if there has been a previously entered comment, it should be displayed in the box to be updated and sent back to the mysql database. I would like to ask if someone could give me a way to include this comment box and code for the existing value to be displayed in the box and if there is no existing comment, then a new comment can be entered and stored in the database.
Below is my php code.
<?php//run query
$result = mysql_query($GetReadings);
?>
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>Glucose Level</th>
<th>SBP</th>
<th>DBP</th>
<th>Comments</th>
</tr>
<?php
//display results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td><?php echo $row["Date"]; ?> </td>
<td><?php echo $row["Time"]; ?> </td>
<td><?php echo $row["GlucoseLevel"]; ?> </td>
<td><?php echo $row["SBP"]; ?> </td>
<td><?php echo $row["DBP"]; ?> </td>
<td><?php echo $row["Comments"];
<?php
//if statement to add comment link if user is a nurse
if ($_SESSION['User_level'] == 2)
{
//code for comments
}
?> </td>
</tr>
<?php
//end of while loop
}
?>
Hope I haven't missed out any crucial information.
Use the javascript function :
window.open(URL, windowName[, windowFeatures])
Where, URL - desired URL to display a page containing Textbox, use any window name you want.
Echo <a> or button with a onclick event eg:
Add Comment
Edit
The most basic way to achieve is, echo a <div></div> containing the past comments, the text box for new comments and Send/Cancel buttons. The trick is to set the display:none style property of that div. You the following code a guideline :
Echo the following code if the User has right user level.
Show Comments
<div id="comment-<?php echo $row['id']?>" style="display:none">
//display previous comments
<form method="post" action="addComment.php?id=<?php echo $row['id']?>">
<textarea name="comment"></textarea>
<input type="submit" value="Add Comment" /><input type="button" onclick="hideComment('<?php echo $row['id']?>')">
</form>
</div>
<script type="text/javascript">
function hideComment(id) {
document.getElementById('comment-' + id).style.display = 'none';
}
function showComment(id) {
document.getElementById('comment-' + id).style.display = 'block';
}
</script>

jquery datatable and cakePHP

i am trying to implement jquery datatable, in my cakePHP based website, but it just wont load. this website is already half developed, and from the way i see it, the js' is loaded through a file called _head.inc.ctp located inside the views/layouts folder, i have added the datatables library inside the libs folder which is webroot/js/libs and load it inside the _head.inc.ctp file.
suppose i have this:
my controller:
var $helpers = array(
'Form',
'Html',
'Javascript'
);
//my method
function dataTable_example($id=null){
$details = $this->Detail->find("all");
$this->set('details', $details );
}
my view:
<div>
<?php echo $javascript->link('libs/jquery.dataTables.js'); ?>
<script>
$(document).ready(function(){
$('#js-datatable').dataTable();
});
</script>
<h2><?php echo __l('Tickets');?></h2>
<div>
<table id="js-datatable">
<tr>
<th>some heading 1</th>
<th>some heading 1</th>
<th>some heading 1</th>
</tr>
<?php
if (!empty($details)){
foreach ($details as $detail):
?>
<tr>
<td><?php echo $detail['Detail']['id'];?></td>
<td><?php echo $detail['Detail']['created'];?></td>
<td><?php echo $detail['Detail']['ticket_detail'];?></td>
</tr>
<?php
endforeach;
}else{
?>
<tr>
<td>No Data Found</td>
</tr>
<?php }?>
</table>
</div>
</div>
i even hard coded it using the usual call, and checked it using firebug to see if the script is loaded or not, and according to firebug, it is loaded, so i cant see whats making the script fail my table.
did i missed some steps ? please help
thanks
You don't have thead and tbody elements as required by the datatables script
You should use the find function in your controller and pass the array to the view and in the view write it.. don't just leave the table empty

Categories