Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: [FIXED] pre fill form

[FIXED] pre fill form 27 Oct 2017 20:25 #15421

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
Hello everyone
I am looking to pre-fill a form with localStorage data.

It is better to go through the form or model?

I do not know what to do and I need your help.

thank you in advance

EDIT :

for the moment I chose to do it like this:
<?phpdefined('_JEXEC') or die('Restricted access');

if (!$this->form)
	return;

$fieldSets = $this->form->getFieldsets();
$fieldSet = $this->form->getFieldset('demandeform.form');
?>
<script>
jQuery(document).ready(function() {
	var title = localStorage.getItem('title');
	jQuery("#jform_title").val(title);
// others thinks...
});
</script>

<fieldset class="fieldsform">
	<?php
	// JForms dynamic initialization (Cook Self Service proposal)
	$fieldSet['jform_resourceId']->jdomOptions = array(
			'list' => $this->lists['fk']['resourceId']
		);
	$fieldSet['jform_Chambre']->jdomOptions = array(
			'list' => $this->lists['fk']['Chambre']
		);
		?>
			<?php $field = $fieldSet['jform_title']; ?>
					<?php echo $field->label; ?>
					<?php echo $field->input; ?>
//others thinks...
</fieldset>
</script>

But i don't know if it is protected. For sure that's work
Other Idea without localStorage ??
Last Edit: 12 Dec 2017 10:28 by Nicolas.
The administrator has disabled public write access.

pre fill form 29 Oct 2017 12:14 #15423

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
I am stuck for the auto-select of ckcombo.
how to make jquery so that the selection is automatic from a var ?

I try it but that's don't work ...
jQuery('#jform_resourceId :nth-child(3)').attr('selected', true); // To select via index

any idea ?

Thx in advance
The administrator has disabled public write access.

pre fill form 31 Oct 2017 19:02 #15426

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 290
  • Thank you received: 125
  • Karma: 48
localStorage is a webstorage, you can only acces it via javascript.
So, there is no way of getting it in the model, the path you choose is therefore correct.

For the jquery stuff: the select box in Cook is a chosen select. You can change it in jquery like this:
        $('#adminForm').find("input[name='jform[resourceId]']").val(value);
        $('#adminForm').find("select[name='jform[resourceId]']").val(value);
        $('select').trigger("chosen:updated");
        //old way of updating
        $('select').trigger("liszt:updated");
The administrator has disabled public write access.
The following user(s) said Thank You: admin, Nicolas

pre fill form 22 Nov 2017 16:22 #15442

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
Hello !

apologies for the answer delay. I was on holiday
everything works perfectly with your method.
thanks again

Is there a way to make a modification on the table without going through the form? something like Ajax post
$.ajax({
     url: 'index.php?option=com_mycomponent&task=component.save',
     data: 'title='+ event.title+'&start='+ start +'&end='+ end +'&id='+ event.id ,
     type: "POST",
     success: function(json) {
     alert("modified");
     }
});

thx a lot
Last Edit: 22 Nov 2017 16:28 by Nicolas.
The administrator has disabled public write access.

pre fill form 22 Nov 2017 22:12 #15443

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 290
  • Thank you received: 125
  • Karma: 48
Sure you can
            jQuery.ajax({
                type: "POST",
                url: "index.php?option=com_mycomponent&task=component.save",
                data: '<?php echo JSession::getFormToken()?>=1&title='+ event.title+'&start='+ start +'&end='+ end +'&id='+ event.id
                 });

You need to get the getFormToken in there.
If you need to know the result, you need to fork the controller, create a new function like "update" or so.
Then
	public function update($key = null, $urlVar = null)
	{
		JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
                $jinput = JFactory::getApplication()->input;
                $title = $jinput->get('title','','string');
                //here the rest of your inputs
		$result = new stdClass;
                $r = false;
                //you can do what you want here with adding properties to $result
                $result->status = $r;
                //echo the result
                echo json_encode($result);
                //important! Will otherwise put the html the repsonse
                exit;
         }

You the script call a bit different:
            jQuery.ajax({
                type: "POST",
                url: "index.php?option=ccom_mycomponent&task=component.update",
                data: '<?php echo JSession::getFormToken()?>=1&title='+ event.title+'&start='+ start +'&end='+ end +'&id='+ event.id,
                cache: false,
                success: function(r){
                    //parse the json
                    r = jQuery.parseJSON(r);
  		    if (r.status == false) {
			//do something when false
		    } else {
                       //yeah, all went well!
		    }
                } 
            });
The administrator has disabled public write access.
The following user(s) said Thank You: Nicolas

pre fill form 23 Nov 2017 02:00 #15444

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
Thanks for this answer.
For the moment i have an error 500 with your code ... i'll try To resolve it tomorow
Last Edit: 23 Nov 2017 02:00 by Nicolas.
The administrator has disabled public write access.

pre fill form 23 Nov 2017 10:15 #15446

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 290
  • Thank you received: 125
  • Karma: 48
Well, I see a typo in ccom_mycomponent, but my code might have other cut and paste issues :D
The administrator has disabled public write access.

pre fill form 23 Nov 2017 13:13 #15447

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
I had already modified the typo and I made the corrections on the code you gave me.
always error 500...
I will continue to search. you already gave me a hint
Last Edit: 23 Nov 2017 15:19 by Nicolas.
The administrator has disabled public write access.

pre fill form 23 Nov 2017 18:41 #15448

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
Hello Romkabouter.
I'm trying to understand your code. but I don't understand how the controller can save the update on the database without something like :
$query->update($db->quoteName('#__litsupp_demandes'))->set($fields)->where(event_id)

for the moment I copied the code into the controller <fork / controllers / demande.php> but it does not seem to work.
The code I did on my page works fine but I do not know or send the result to edit the record:
eventDrop: function(event, delta, revertFunc, resource) {
			start = event.start.format("Y-MM-D HH:mm:ss");
			end = event.end.format("Y-MM-D HH:mm:ss");
			resource = event.resourceId;
			jQuery.ajax({
                type: "POST",
                url: "<?php echo JUri::base(); ?>index.php?option=com_litsupp&task=component.save",
                data: '<?php echo JSession::getFormToken()?>=1&start='+ start +'&end='+ end +'&resource='+ resource +'&id='+ event.id,
                cache: false,
                success: function(data){
                     //reload the page after update event on database
                    //alert("that's ok");
		},

why can not we use the component.save that already exists in the component for forms?

thx
The administrator has disabled public write access.

pre fill form 23 Nov 2017 20:35 #15449

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 290
  • Thank you received: 125
  • Karma: 48
Better to save data via the model
In your call, you give id as param, you can use that to save the model
public function update($key = null, $urlVar = null)
{
    JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
    $jinput = JFactory::getApplication()->input;
    $id = $jinput->get('id','','int');
    $resource = $jinput->get('resource','','int');
    $r = false;
    if ($id > 0) {
        $r = $model->save(array(
            'id' => $id,
            'resource' => $resource
        ));
        $r = true;
    }			
    $result->status = $r;
    $result->row = $id;
    echo json_encode($result);
    exit;		
}
You can use $model, because you are in the controller, it will be available there.
You have to put the info you need returned in $result, in my example $result->row will be your id which you have given as a parameter.

Then, in your ajax call (using your snippet)
eventDrop: function(event, delta, revertFunc, resource) {
			start = event.start.format("Y-MM-D HH:mm:ss");
			end = event.end.format("Y-MM-D HH:mm:ss");
			resource = event.resourceId;
			jQuery.ajax({
                type: "POST",
                url: "<?php echo JUri::base(); ?>index.php?option=com_litsupp&task=component.save",
                data: '<?php echo JSession::getFormToken()?>=1&start='+ start +'&end='+ end +'&resource='+ resource +'&id='+ event.id,
                cache: false,
                success: function(data){
                     //reload the page after update event on database
                    //alert("that's ok");
                    result = jQuery.parseJSON(data);
                    console.log(r);
		},

Console.log will log the result, should also have a property row, holding the ID
Please update what console.log outputs :)
After that, you can use the info to update elements on your page via Jquery

Why a different controller task?
Because you need a raw json output accomplished by "exit", which stops processing the rest of the controller and thus just echoing the json data.
The save function cannot be modified, because then you can not save the data via the form anymore (because of the exit)
The administrator has disabled public write access.

pre fill form 24 Nov 2017 01:05 #15452

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
I do not understand ...
I'm really not good, sorry.

The log returns "null" and nothing is registered in the database.

Here is what I did

In my controller : <fork / controllers / demande.php>
public function update($key = null, $urlVar = null)
	{
		JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
		$jinput = JFactory::getApplication()->input;
		$id = $jinput->get('id','','int');
		$start = $jinput->get('start','','string');
		$end = $jinput->get('end','','string');
		$resource = $jinput->get('resource','','int');
		$r = false;
		if ($id > 0) {
			$r = $model->save(array(
				'id' => $id,
				'start' => $start,
				'end' => $end,
				'resource' => $resource
			));
			$r = true;
		}			
		$result->status = $r;
		$result->row = $id;
		echo json_encode($result);
		exit;		
	}

and in my view : <fork/views/demandes/tmpl/default.php> :
eventDrop: function(event, delta, revertFunc, resource) {
			start = event.start.format("Y-MM-D HH:mm:ss");
			end = event.end.format("Y-MM-D HH:mm:ss");
			resource = event.resourceId;
			jQuery.ajax({
                type: "POST",
                url: "<?php echo JUri::base(); ?>index.php?option=com_litsupp&task=demande.update",
                data: '<?php echo JSession::getFormToken()?>=1&start='+ start +'&end='+ end +'&resource='+ resource +'&id='+ event.id,
                cache: false,
				success: function(data){
                    result = jQuery.parseJSON(data);
                    console.log(result);
				} 
			});
		}

i keep looking about that ...
Last Edit: 24 Nov 2017 18:23 by Nicolas.
The administrator has disabled public write access.

pre fill form 24 Nov 2017 06:59 #15453

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 290
  • Thank you received: 125
  • Karma: 48
Well, no 500 error so that is good news.
I see some problems, first change
$id = $jinput->get('id','','int');
to
$id = $jinput->get('id',0,'int');
My mistake.

Then, try console.log (data), that is the raw data. The return might actually return an error or something.
jsonParse will not work then.

If you still have problems, break it further down by removing all code except the result.
Something like
public function update($key = null, $urlVar = null)
	{
		$r = false;
		$result->status = $r;
		echo json_encode($result);
		exit;		
	}
The administrator has disabled public write access.

pre fill form 24 Nov 2017 16:03 #15454

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
Romkabouter wrote:
Well, no 500 error so that is good news.
yes my mistake is that i forget to call the "controller.update" like "demande.update" .... :P

Romkabouter wrote:
I see some problems, first change
$id = $jinput->get('id','','int');
to
$id = $jinput->get('id',0,'int');
My mistake.
i modified that but Is there a difference ?

Romkabouter wrote:
Then, try console.log (data), that is the raw data. The return might actually return an error or something.
jsonParse will not work then.
console.log(data) return nothing. there is no message 'null' in the console

Romkabouter wrote:
If you still have problems, break it further down by removing all code except the result.
Something like
public function update($key = null, $urlVar = null)
	{
		$r = false;
		$result->status = $r;
		echo json_encode($result);
		exit;		
	}

It's the same probleme, nothing appens in the console.log and there is no error. i tried with an alert instead but there is nothing in my alert ....

should my event register at the same time normally? cause that's don't work too :dry:
Last Edit: 24 Nov 2017 16:07 by Nicolas.
The administrator has disabled public write access.

pre fill form 24 Nov 2017 18:47 #15455

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 290
  • Thank you received: 125
  • Karma: 48
I do not know the rest of your code, but you might want to try just the ajax call in the domready event.
Or do a console.log('test'); before start = event.start.format("Y-MM-D HH:mm:ss"); to see if your code is even called.
Got any script errors maybe?
The administrator has disabled public write access.

pre fill form 25 Nov 2017 00:51 #15456

  • Nicolas
  • Nicolas's Avatar
  • Offline
  • Senior Member
  • Posts: 72
  • Thank you received: 11
  • Karma: 5
eventDrop: function(event, delta, revertFunc, resource) {
			start = event.start.format("Y-MM-D HH:mm:ss"); //return start date of droped event (verified)
			end = event.end.format("Y-MM-D HH:mm:ss");  //return end date of droped event (verified)
			resourceId = event.resourceId;  //return row id on calendar of droped event (verified)
			jQuery.ajax({
                type: "POST",
                url: "<?php echo JUri::base(); ?>index.php?option=com_litsupp&task=demande.update", //link to the task that we created in the controller
                data: '<?php echo JSession::getFormToken()?>=1&start='+ start +'&end='+ end +'&resourceId='+ resourceId +'&id='+ event.id, 
                cache: false, //don't know why ....
				dataType: "json",
				success: function(data){
                   alert(data); //return nothing ......			
				} 

after many tests, my code above don't return the data .... and i don't know why ....

the function is triggered when I move an event on the calendar.
I would like the new start date and the new end date to be changed on the database ...
my variables 'start', 'end', 'resourceId' exist and are verified.
but I cant even return them on my ajax data....
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.122 seconds

Get Started