Thursday, March 7, 2013

Re: Saving AJAX Checkboxes

Hi there,

at the moment i have still no success and the same problem.

I have created a new views. Everything works fine. But still the same problem that only the event.id will stored into the table. the location_id will not stored.

Any ideas ?

EventLocationsController.php
function add($id = null) {
if (!empty($this->data)) {
$this->EventLocation->create();
if ($this->EventLocation->save($this->data)) {
$this->Session->setFlash(__('The Post has been saved.', true));
$this->redirect(array('action'=>'add',$this->EventLocation->id));
} else {
$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->EventLocation->read(null, $id);
}
$events = $this->EventLocation->Event->find('all');
$this->set(compact('events'));
}

public function locations($city_id) {
$this->layout = NULL;
$locations = $this->EventLocation->Location->find('list', array('conditions' => array('Location.city_id' => $city_id)));
                $this->set(compact('locations'));
}

Model/EventLocation.php
public $belongsTo = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'event_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Location' => array(
'className' => 'Location',
'foreignKey' => 'location_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $hasAndBelongsToMany = array(
'Location' => array(
'className' => 'Location',
'joinTable' => 'EventLocations',
'foreignKey' => 'event_id',
'associationForeignKey' => 'location_id',
//'with' => 'PostToTag',
),
);

View/add.ctp
<?php 
echo $this->Html->script('http://code.jquery.com/jquery-1.9.1.min.js');
?>
<div class="eventLocations form">
<?php echo $this->Form->create('EventLocation'); ?>
<fieldset>
<legend><?php echo __('Add Event Location'); ?></legend>
<?php
$select_value = array();
foreach($events as $event) {
$key = $event['Event']['id'];
$cityid = $event['Event']['city_id'];
$value = $event['Event']['name'];
$select_value[$cityid] = $value;
}

$mydata = $this->data;
echo $this->Form->select('event_id', $select_value, array('label'=> 'Veranstaltung'));
echo $this->Js->writeBuffer();
?>
<div id="locations"></div>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

<script typ="text/javascript">
$("#EventLocationEventId").click(function(){
   var cityid = $("#EventLocationEventId").val();
   $.get("/evants/EventLocations/locations/"+ cityid,
function(data) {
$('#locations').fadeOut("fast", function() { 
  $('#locations').html(data);
  $('#locations').fadeIn("fast");
});
});
   return false;
});
</script>

View/locations.ctp
<?php
echo $this->Form->input('EventLocation.location_id',array(
        'label' => __('Location',true),
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $locations,
    ));
?>

Am Mittwoch, 6. März 2013 16:03:55 UTC+1 schrieb Jerome Walitzek:
Hi there,

i have following tables

Events
Cities
Locations
EventLocations

When i edit an Event i can select the city (city_id) where the event is.
But every City has more than one location which i get over AJAX.
That works Great.
But now i want to store the locations which i have "checkboxed" into the table "event_locations"
How can i do that ?

Any ideas ?

Here is my Code:
edit.ctp
<?php
echo $this->Form->input('city_id');
echo $this->Js->writeBuffer();
?>

<div id="target-div"></div>

<script typ="text/javascript">
$("#EventCityId").click(function(){
   var cityid = $("#EventCityId").val();
   $.get("/events/fetch/"+ cityid,
function(data) {
$('#target-div').fadeOut("fast", function() { 
  $('#target-div').html(data);
  $('#target-div').fadeIn("fast");
});
});
   return false;
});
</script>

EventsController.php
public function fetch($city_id) {
$locations = $this->Event->Location->findAllByCityId($city_id);
$this->set('locations', $locations);
}

fetch.ctp
<?php
$select_value = array();
foreach($locations as $location) {
$key = $location['Location']['id'];
$value = $location['Location']['name'];
$select_value[$key]    = $value;
}
$mydata = $this->data;
echo $this->Form->select('Location', $select_value, array('label'=> false, 'multiple' => 'checkbox'));
echo $this->Html->link(__('Add another Location'), array('controller' => 'locations', 'action' => 'add'));
?>

Thanks for help

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: