Hidden Input Doesn't Change Serverside When Updated in the Client

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 8:00 am

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

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

Code: Select all

<form method="post" accept-charset="utf-8" id="myForm2" action="/mealplans/add">

<input type="hidden" name="draggables" id="drag12"/>

<button id="myId" type="submit">Submit Form</button>

</form>
This is where I change the value in the javascript:

Code: Select all

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 null is what gets saved in the database. The javascript doesn't change the PHP value; it only changes the client side value.

I've also tried getElementsByName but that didn't work either.

Code: Select all

var y = document.getElementsByName("draggables").value;	//only changes it on the client
This is actually a cakephp site but this problem is with the PHP so I am publishing it here.
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 8:28 am

I found code that works:

Code: Select all

var z = $("#drag12").val("draggable3");
updates the field in the table with draggable3
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 10:17 am

Now I'm trying to push the z element to an array but the following code doesn't work. The code adds the last z that was selected to the database instead of adding an array of zs.

Code: Select all

var draggables = [];

if(data == "draggable3"){
alert("data " + data);
var z = $("#draggable3").val("draggable3");
draggables.push(z);
} //end if

if(data == "draggable4"){
alert("data " + data);
var z = $("#draggable4").val("draggable4");
draggables.push(z);
} //end if
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 1:00 pm

Code: Select all

var z = $("#draggable4").val("draggable4"); 
is an object. If I do alert(z), it shows [object Object].
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 2:35 pm

I deleted this because it contains misinformation.
Last edited by makamo66 on Sat Apr 17, 2021 4:12 pm, edited 1 time in total.
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 3:14 pm

I deleted this because it contains misinformation.
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 4:29 pm

This only works for the hidden input with the id of draggable4 because that is the last input with the name draggables.
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sat Apr 17, 2021 4:36 pm

The name of the hidden input is the name of the field in the database and so in cakephp it automatically saves the field name/input name to the database when the form is submitted so maybe this is a question for a cakephp forum and not a plain php forum.
makamo66
New php-forum User
New php-forum User
Posts: 9
Joined: Sat Apr 17, 2021 7:54 am

Sun Apr 18, 2021 5:01 am

I don't need to change the value of the hidden input after all. I just need to submit an array with the ids like follows to the database.

Code: Select all

var draggables = [];
draggables.push("draggable3");
draggables.push("draggable4");
and the following alert responds with draggable3, draggable4 as it should
alert(draggables);

But draggables is a javascript array and I need the PHP array in order to submit it to the database. How can I get the clientside variable on the server side?
Post Reply