Skip to main content

Change value JS, PHP getting old value



I've a silly problem.





Depending on a the visibility of a div I want to set a hidden value







if($('#crewMember').is(':visible')) {

$('#visibility').attr('value', 'hidden')

} else {

$('#visibility').attr('value', 'visible')

}







This works. I've checked it via FireBug and I can see that the HTML has changed.





But when I try to get this value after form submission I get the original value, not the changed value.







echo $_POST['visibility']

//returns default value, not the adjusted valueHow come?







How come?





EDIT SOme example code







<html>

<script type="text/javascript">

$(document).ready(function() {



$('#div').click(function() {

$('#visibility').val('hidden');

$('#value').html('hidden value: ' + $('#visibility').val());

});



$('#value').html('hidden value: ' + $('#visibility').val());

});



<body>

<form method="post">

<div id="div">

click this area to change value

</div>



<div id="value"> <!-- This div will show the actual value of the hidden field -->

</div>

<input type="hidden" id="visibility" name="visibility" value="initial value" />

<input type="submit" name="button" value="button" />

</form>

</body>

</html>







When I submit this form the $_POST['visibility'] always contains the string 'initial value'. Even when I changed the value with JS to 'hidden';


Comments

  1. As ManseUK said, Change $("").attr('value',...) to $("").val(...) (see the jQuery documentation for .val)

    But to answer the question as to why, you need to understand how DOM objects work in JavaScript. For every element (div, p, a, img, form, input, whatever...) every single element is an object in the DOM.

    Here's the important part: the element has a value, but also each attribute has a value. In jQuery, when you use .attr('value',...), what you are really doing is creating an attribute with the name of 'value'. But this does NOT change the element value, which stays the same.

    In firebug, it will read the JavaScript attributes and layer it on top of the element, which explains why it showed up that way in firebug. However, when it comes time to submit the data to the server, the browser will NOT submit the attribute values - only the element's value (which has not changed). But using .val() will change the element value, which is what will be submitted to the server.

    Perhaps this will make more sense in raw JavaScript, instead of jQuery:

    the equivalent of .attr() is: element.setAttribute("value","...");

    the equivalent of .val() is: element.value = "...";

    Do you see the difference? In .attr() you never changed the value, only the attribute.

    ReplyDelete
  2. Try using val() instead (im assuming your visibility element is an input element):

    if($('#crewMember').is(':visible')) {
    $('#visibility').val('hidden');
    } else {
    $('#visibility').val('visible');
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...