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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex