php - How to set the last input of my form to be the default value using jquery without using a database?
Get the solution ↓↓↓I am just new to symfony framework and still working on how to set the last input in my form (e.g., strength: 79) as the default value of my progressbar without using the database after refreshing the page.
Here is some part of my code: index.html.twig
<form>
<fieldset>
<legend>Set chest targets</legend>
<div class="clear"></div>
<div class="responsivelabel aligninline" >
<label>Strength:</label>
</div>
<div class="responsiveelement aligninline" >
goal:
<input type="text" size="3" id="strength_goal" />
<div class="clear"></div>
<div class="responsivelabel aligninline" >
<label> </label>
</div>
<div class="responsiveelement aligninline" >
<p class = "clean-gray">Save Changes</p>
</div>
</fieldset>
</form>
And here is some part of my code: index.html.twig
<script type="text/javascript">
$(document).ready(function(){
$("p.clean-gray").on('click', function(){
var str = $("#strength_goal").val();
var mov = $("#movement_goal").val();
$("div.strength").css("width", str+'%')
$("div.movement").css("width", mov+'%');
});
});
</script>
Some part of the code: index.html.twig
strength
<div class="prog-barwrap mini">
<div class="prog-bar strength" style="width:0%" ></div>
</div>
movement
<div class="prog-barwrap mini">
<div class="prog-bar movement" style="width:0%"></div>
</div>
Please tell me how to do it. Others say i should use session but i dont know how to do it in symfony. Thanks in advance. I am currently using 2.3 version.
I tried visiting the w3school: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_text_defaultvalue3 It tells me how to set the default value but is it possible to make the last data i inputed into the form as the new default value?
Answer
Solution:
It sounds like you are trying to locally store the last values entered by a user.
If so, this is something that I have to do often, This is the method I use.
Simply include the below javascript then when you do your html, adddata-local-key="someKeyName"
to any element you want to store the last value for (where you name someKeyName whatever you want with no spaces). When the user inputs data and clicks away, the value is saved. When the page loads, it will automatically search for stored values and re-input them. Add as many elements as your heart desires with no additional js code.
Here is a working jsFiddle
<input type="text" id="test" data-local-key="someKeyName" value="some initial value">
The javascript:
/**
* Feature detect + local reference for simple use of local storage
* Use like:
* if (storage) {
* storage.setItem('key', 'value');
* storage.getItem('key');
* }
*
*/
var storage;
var fail;
var uid;
try {
uid = new Date;
(storage = window.localStorage).setItem(uid, uid);
fail = storage.getItem(uid) != uid;
storage.removeItem(uid);
fail && (storage = false);
} catch (exception) {}
// using the feature
$(function() {
$(document).on('blur', '[data-local-key]', function() {
var $this = $(this);
if (storage) {
storage.setItem($this.data('local-key'), $this.val());
}
});
if (storage) {
$('[data-local-key]').each(function() {
var $this = $(this);
var storedVal = storage.getItem($this.data('local-key'));
if(storedVal) $this.val(storedVal);
});
}
});
If you are just trying to store some arbitrary data, just use the first part of the above and call it like this:
Store the value:
if (storage) {
storage.setItem('key', 'value');
}
Retrieve the value:
if (storage) {
storage.getItem('key');
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot set properties of undefined (setting '_dt_cellindex')
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.