jQuery Form Doesn't Post

The ajax complexity wss all for naught. Getting the jQuery variable in the PHP is as simple as this:
In the controller:
$this->set('jsvalue','total');
where total is a variable set in the javascript used to sum up the total calories.
In the template in the javascript
alert("You are at php var " + <?php echo $jsvalue; ?>);
Only problem now is that I can’t save it to the database in my controller
$mealplan->total_calories = 'total';
This just saves the string literal and inserts the word ‘total’ in the database.

As mentioned do not use Javascript to calculate a total which is to go back into your database. It cannot be trusted - you can never trust the client. Instead you send back the tiles selected or whatever and do the sum in the action post for your table. Also from skimming over your code you’re still using the submit button to do an AJAX; either use a dedicated button or actually use the submit form. To submit just put a hidden field which holds a nice long string representing all their selections [populated by Javascript] from your user interface.

Why would I use a long string of hidden inputs instead of just getting the values of the hidden inputs separately, one by one?

The script does exactly what you tell do.

$mealplan->total_calories = $total;
would try do find a variable named *total’ and save this value.

I suggested that as you don’t know how many tiles they intend to drag off - or whatever your mechanism is. You could dynamically create and destroy elements within the form to represent the selection, but I would have felt it easier to just build a single JSON string which goes into your hidden selection field that represents all their tiles selected.

In my view I’ve tried
<?php $drag = "draggable7"; ?>
and
<?php $this->set('drag', 'draggable7'); ?>
Neither one of these sets a variable that can be used in the controller
I’m trying to save to the database in the controller but the following just inserts a NULL into the database.
$mealplan->draggables = $drag;

I tried getVar but it returned null too:
echo "Here " . $this->viewBuilder()->getVar('drag');

maybe you should include those variables in your form.
i.e. $this->Form->hidden…

I am trying to set the hidden input in the javascript but it doesn’t set it in the php.

This is the hidden input. It corresponds to a field in the database table with the name of draggables.

<?php echo $this->Form->hidden('draggables', ['id' => 'drag12', 'value' => 'draggable12']); ?>

if(data == "draggable3"){

alert("data " + data);
var x = document.getElementById("drag12").value;
x =  "draggable3";
alert(x);
} //end if

The alert pops up with draggable3 as it should but the value of draggable12 is what gets saved in the database. The javascript doesn’t change the PHP value; it only changes the client side value.

I found code that works:

var z = $("#drag12").val("draggable3");

updates the field in the table with draggable3

I find it strange that it works in jquery but not in javascript.

jQuery is JavaScript. Your problems here are, it seems, largely because you don’t have a clear understanding of how the various parts of the system work together. You’re basically just throwing random things at the wall and hoping that something sticks. Sorry, but this approach has long since led me to abandon actually trying to help with this particular thread, it’s just too much of a morass to try to wade into, and I expect there’s others in the same boat (in case you’re wondering why not much help is forthcoming). I really think you’d be well served by going back and doing more reading (yes, I know) on the basics of server-side vs client-side, MVC data flow (including the Post/Redirect/Get paradigm), Ajax basics (though I still think that’s probably an approach that’s hindering you more than helping in this particular case), and then, after all that, go through the Cake tutorial, which should make a lot more sense then, you should feel some concepts start to “click”.

I hope this isn’t coming across as too negative. The truth of the matter is that coding is hard. There’s a lot of fundamentals to understand just to start properly making small changes to what the Cake tools will “bake” for you. What you’re attempting here is well beyond “small changes”, and it’s clear from your questions and comments that you do not yet have the grasp of those fundamentals that’s required to build what you’re trying to build. You can keep banging your head against it, without much help from us because of how hard it is to help someone fix such deep flaws in the code, or you can put in some work to learn those fundamentals.

1 Like

I think that the problem I have with learning CakePHP is due to my mental disability. I hear voices so I can’t read. My reading comprehension is very low because of the voices so I just don’t learn as well as someone who’s not hearing voices.

Maybe there’s some YouTube videos on these topics? Most of it is pretty generic, and not Cake-specific, in fact not even PHP-specific, so there should be a fair bit of info out there in various formats.

I have trouble listening to videos as well.