Get Javascript Array to Submit to the Database

The following alerts the array as it should. It alerts draggable3, draggable4 after both if conditionals have been executed. I have a field in my database named draggables but it has no connection to the javascript array also named draggables. How do I get the client side variable to speak to the server side variable? I want to submit the javascript draggable array to the database.

if(data == "draggable3"){
draggables.push("draggable3");
} 
if(data == "draggable4"){
draggables.push("draggable4");
} 
alert("Outside of if " + draggables);

In the template I have the following two hidden inputs

<?php

echo $this->Form->hidden('draggables', [
    'id' => 'draggable3',
	'multiple' => 'true',
    'value' => '',
]);

echo $this->Form->hidden('draggables', [
    'id' => 'draggable4',
	'multiple' => 'true',
    'value' => '',
]);

?>

The two inputs have the same name but this is allowed. I want to make the names arrays by adding square brackets to the name but the form doesn’t submit when I do that.

This is in the template and prints the javascript variable on the page:

$draggables = "<script type=\"text/javascript\">window.document.write(draggables)</script>";
print_r($draggables);

but when I try to save it to the database in the controller like this:

$mealplan->draggables = $draggables;

I get the error: Undefined variable: draggables

It is allowed, but senseless, because the second hidden form overwrites the first. If you want “draggables” to be an array, go this way:

                echo $this->Form->hidden('draggables[]', [
                    'id' => 'draggable3',
                  'multiple' => 'true', 
                    'value' => 'Value of draggable 3',
                ]);

                echo $this->Form->hidden('draggables[]', [
                    'id' => 'draggable4',
                  'multiple' => 'true',
                    'value' => 'value of draggable 4',
                ]);

(How) do you sent the variable $draggables from your form to your controller?

Please don’t get me wrong, but I think you should start again with the basics and also forget the jQuery part for a long time.

For helping you get a new start, I recommend you to describe what you want to achieve with your project and what your DB structure is like.
DB structure is helpful, because if the structure is not optimized, the rest of the project will never be optimal.

Otherwise I wish you the best luck with your project

Like I wrote before, the form doesn’t submit to a response page when I add the square brackets. It stays on the same page after the submit button is clicked.

if I put the above inside a form and hit [SUBMIT], then I get the following in my controller inside the request data:

‘draggables’ => [
(int) 0 => ‘Value of draggable 3’, (int) 1 => ‘value of draggable 4’,
],

If you don’t get this, you are doing something other than me.

here is what my form looks like:

        <?= $this->Form->create($mealplan) ?>
        <fieldset>
            <legend><?= __('Add Mealplan') ?></legend>
            <?php
                echo $this->Form->control('name');
                echo $this->Form->hidden('draggables[]', [
                    'id' => 'draggable3',
                  'multiple' => 'true',
                    'value' => 'Value of draggable 3',
                ]);

                echo $this->Form->hidden('draggables[]', [
                    'id' => 'draggable4',
                  'multiple' => 'true',
                    'value' => 'value of draggable 4',
                ]);


            ?>
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>

I have to explicitly set the action page when I open the form even though there is a redirect in my controller. I set:

<?php echo $this->Form->create($mealplan, ["id" => "myForm2", 'url' => ['action' => 'edit']]); ?>

and then it goes to my edit page and throws the error:

Tampered field draggables in POST data (expected value draggable4 but found ``)

I set the hidden inputs like this

<?php

echo $this->Form->hidden('draggables[]', [
    'id' => 'draggable3',
    'multiple' => 'true',
    'value' => 'draggable3'
]);

echo $this->Form->hidden('draggables[]', [
    'id' => 'draggable4',
    'multiple' => 'true',
    'value' => 'draggable4',
]);

?>

If I change it from hidden input to a form control and add an id for the edit page, it works. I think it’s a security problem from what I’ve googled. I changed my CSRF token somewhere but I can’t remember where.

It’s not related to CSRF at all. The form security component doesn’t allow you to change the values of hidden fields. You should be able to ->unlock() those fields with the form helper.

I switched back to redirecting the submission via the controller with no hidden inputs and added $this->loadComponent(‘Security’); to my controller and I got the error ‘_Token’ was not found in request data. When I don’t add the security component, the page doesn’t submit.

Once again you’re doing yourself no favours in how you present this. Your comments are so devoid of context that we can’t possibly tell what’s wrong. Where, exactly, did you add the loadComponent call? (“my controller” is incredibly vague.) What does “doesn’t submit” even mean?

public function initialize(): void
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
	$this->loadComponent('FormProtection');
        $this->loadComponent('Security');
    }

If I don’t put the redirection in the view template, the form stays on the same page (the add template) and doesn’t submit to the edit page.

If I put the redirection in the template like this:

<?php echo $this->Form->create($mealplan, ["id" => "myForm2", 'url' => ['action' => 'edit', 618]]); ?>

I can’t use $mealplan->id to get the id to navigate to and I have to hardcode it to an id that is already in the database.

I could set an id to $mealplan->id in my controller and use it in my template but I don’t want to do the redirection from the template.

I can’t set the edit id in the controller after all.

Didn’t we go over this once before? I think your understanding of redirection is lacking. Your add form should not be submitting (which you seem to be calling “redirecting”) to the edit action. Add is for adding, edit is for editing. If you want to redirect (the actual meaning of redirect) to the edit page after you add the new record, you do that with return $this->redirect(['action' => 'edit', $mealplan->id]); after you successfully save in the add action.

In other words:

  • add action called without any data at all
    • creates an empty entity and sets it for the view
  • add view is rendered
    • the form of which should have “add” as the target
      • this is not a “redirect”
  • add action called with the form data
    • patches the data into the empty entity
    • saves it
    • if successful, redirects to the edit action
      • this is an actual “redirect”
  • edit action called with the newly saved ID
    • loads that entity and sets it for the view
  • edit view is rendered
    • the form of which should have “edit/xxx” as the target

Are you using the bake tool? Apart from the where the add function redirects to after saving, this is all 100% standard code that Cake would happily generate for you.

Even when I use the form control instead of hidden input for the draggables input, the form stays on the same page and doesn’t submit to the edit template unless I tell it to in the template and not the controller.

If I remove the square brackets it redirects just fine but I need draggables to be an array.

please show the whole code of the form