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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.