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);
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.
(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 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.
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.