Hijacking the submit button I would think is bad practice. It will confuse, and you cannot rely on every browser to behave the same with it - it may well execute your JS and also submit the post, or just one or the other. Try instead if creating a button outside of the form and associate your click to that.
I tried adding another button but now not even the click event works. Also I placeed an alert at the top of the javascript code that I’m linking to and it never popped up. The following never executes:
Because your JavaScript is doing a Post command. All this is automatic within a form, as it was back in the '90’s! You don’t need any JavaScript to post when the submit input button is within the form.
For your scripts etc., maybe show the whole file here if possible. As a disclaimer though I won’t be providing code solutions until you consider the decision I put before you here: Passing Variable to the Method - #15 by Jawfin
And on Youtube, there is a Tutorial from Alison Pito (he also covers many more questions about cake4 and also has many tutorials for cake3 which can help)
How should anyone know, where „data named total“ is defined or collected in your script or what „a bunch of data, that i dont care about“ means?
It is nearly impossible to help you, if you only supply pieces of code, that you copied and pasted without understanding what you are doing.
Sorry to say, but you should start right from the beginning, go through the tutorial at cakephp and before that you should invest some time to understand the basics of software development and logical structures.
Please excuse my bad english and the hard words, but you are doing yourself no favour.
The total is in the data parameter in the code snippet above. I can’t get the total sent to the response page like I can get the query parameters. I want to read the total on the submitted page like I can the form inputs.
It was returning an empty string because I didn’t have an action tag in my form. Now when I use the code below, it dumps the form inputs but not the json data from the jQuery post.
$json = file_get_contents(‘php://input’);
var_dump($json);
The get_total_calories.js script that it links to is as follows:
$(function() {
var selectedBoxes = new Array();
var total = 0;
var userId = 1;
for (var i=1;i<100;i++){
$( "#draggable" + i ).draggable();
}
$("#droppable_box").droppable({
drop: function(event, ui) {
if($(ui.draggable).hasClass("rightSide"))//If already on the right do nothing
{
return true;
}
else
{
$(ui.draggable).addClass("rightSide");
$(ui.draggable).appendTo($(this));
$(ui.draggable).css("position","");
var data = $(ui.draggable).attr('id');
selectedBoxes.push($(ui.draggable).attr('id'));
for (var i=1;i<100;i++){
if(data == "draggable" + i){
//The parseInt() function parses a string argument and returns an integer
var myInt = parseInt($('#cals_per_serving' + i).val());
}
}
}
if ( typeof myInt === 'undefined' || !myInt) {
myInt = parseInt(0);
}
total += parseFloat(myInt);
$('#response').text(total);
});
$("#myId2").click(function() {
$('#result').append('total: ' + total);
var test = $("#result").html();
$.ajax({
type : 'POST',
url : "<?php echo Router::url(['controller' => 'mealplans', 'action' => 'add']); ?>",
dataType : 'HTML',
data : {
test : test
},
success : function(data) {
alert("success");
}
});
});
The ajax function returns confirmation of success but when I try to access the variable with $_REQUEST[‘test’], it returns the error: undefined index: test. I created this with help of the following forum post: