Jump to content

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


makamo66

Recommended Posts

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.

<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:

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.

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.

Link to comment
Share on other sites

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.

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

 

Link to comment
Share on other sites

None of this works for more than one hidden input because the name is what gets saved to the database and the hidden input can't have several names with the same value. I can't do

<input type="hidden" name="draggables" id="draggable3" multiple="multiple" value=""/>
<input type="hidden" name="draggables" id="draggable4" multiple="multiple" value=""/>
<input type="hidden" name="draggables" id="draggable5" multiple="multiple" value=""/>

because the name is repeated. CakePHP saves the input name to the database with the same field name so I guess this belongs in a CakePHP forum after all. 

Link to comment
Share on other sites

The array push isn't working but my last two posts are incorrect and there is no option to delete them. The database is being updated with the last value that gets set for the hidden input after all so it's not true that the value was hard coded in the input array. Also, using the same name for multiple hidden inputs works in cakePHP after all so that was incorrect as well. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

PHP will create an array of the posted values if your name ends with [].

<input type="hidden" name="draggables[]" id="draggable3">
<input type="hidden" name="draggables[]" id="draggable4">
<input type="hidden" name="draggables[]" id="draggable5">

In your PHP code you can then take that array of values and do whatever you need to with it.

13 hours ago, makamo66 said:

This is where I change the value in the javascript:

The reason your "change" didn't work in the original code is because you only changed the value of the variable x, not the value of the actual input element.  Assigning the value to x doesn't link the two, it makes a copy.  Changing the copy doesn't change the original.  To change the value of the input, you need to assign a new value to the element's .value property.

document.getElementById("drag12").value = "draggable3"

 

Link to comment
Share on other sites

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. 

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?

Link to comment
Share on other sites

There's probably a Cake-specific method of creating form fields with an array-enabled name, but I don't have the slightest notion what it would be.

To answer your other question, you'll need to submit the form data. Whether you do it via AJAX or a traditional HTML submit doesn't matter - send the data to the PHP script and process it there.

I'm not going to lie, given what you've described I'm hard-pressed to believe that what you're doing is actually what you want to do, but then again I don't know your project.

Link to comment
Share on other sites

You get the data to the server by submitting the form. You can do it traditionally or via AJAX, but either way the data has to be submitted to get to the PHP. I don't know cake, but if you normally get the request data via $this->request->getData() but it's empty in this case then something's wrong elsewhere.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.