$(document).keyup(function(e){
var idd = $(".hidd").val();// Here i can't get the correct value of "class hidd" and always get value 1
var sss = ".comment_tarea" + idd;
$tArea = $(sss);
alert(sss);
if ($tArea.is(":focus") && e.keyCode == 13) {
var t = $tArea.val();
}
});
$id1=0;
while(...){
$id1++;
<form >
<? $t =$id1; $comment_tarea = "comment_tarea".$t;
echo("comment");
echo($comment_tarea);
?>
<textarea class="<? echo $comment_tarea; ?>" name="tarea"></textarea>
<input type="hidden" class="hidd" value="<?php echo htmlspecialchars($id1); ?>" />
</form>
}
In the Jquery function, there is always value 1 in variable idd of class "hidd" for all textarea that means all class of $comment_tarea such as "comment_trea1", "comment_trea2", "comment_trea3", "comment_trea4",.......ans so on.
Here the textarea input value is change by $id1 which is unique.
How can get the correct idd value means get idd=1 for "comment_trea1",get idd=2 for "comment_trea2", get idd=3 for "comment_trea3" and so on...
pls help .
.val() returns only the value of the first matched element.
What you need to do is call .val() on every matched element:
$(".hidd").each(function() {
alert($(this).val());
});
You are using val() with the selector and it will return value of element at zero index always. To get the value of element at one index if exists you can use $(".hidd").eq(1).val();
You better bind the keyup with your text area instead of binding with document. AS you are generating forms in loop and each loop iteration generates textarea with hidden field. You can relate the hidden field with the form enclosing textarea.
$("textarea[name=tarea]").keyup(function(e){
var idd = $(this).closest('form').find('.hidd').val();
//your code
}
Related
i have a form where two fields are dynamically generated through java script when a button is clicked.when the button is clicked each time,the two text field will generate again and again.now i have got the count of text field generated in a hidden field in JavaScript.How can i get the value of hiddenfield in controller and insert the values of text fields in database,by appending comma in data when the text box value is entered each time.please help me.
my javascript is
<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;
}
</script>
my html code is
<input type="button" id="button1" onclick=getField();/>
<div id="renderDiv">
</div>
inside this div the two textfield is generated along with the hidden field
i am not getting the value of textfield in controller while submitting and i am not getting the count of textfield.i tried like $hiddenfield=$this->input->post('hiddentextField');
you will have to post the variable inorder to pass this to the server
<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;
window.location.href = "index.php?name=" + countbox-1;
//change index.php to your page name
}
</script>
then in the same page
<?php
$hiddenfield=$_GET["name"];
?>
I had the same problem before, what I did is I insert those text box inside a table, lets say, tableSample
then I use jQuery find
var _content = '';
var _findcontent = $("#tableSample");
_findcontent.find("tr").each(function(){
$(this).find("td").each(function(){
$(this).find("input").each(function(){
_content += $(this).val+'~'+$(this).attr("id")+'|';
});
});
});
Then use ajax to pass it to your PHP
$.post("<?php echo site_url('controller_name/method_name'); ?>",
{
content : _content
}
,function( _data){
jAlert("alert",_data,"alert");
});
In your PHP, you can use explode to get the desired values,
$content_from_page = $this->input->post("content");
$explode_string = array();
$explode_string = explode("|",$content_from_page );
$explode_arr = array()
for($x=0;$x<count($explode_string)-1;$x++)
{
$explode_arr[] = explode("~",$explode_string[$x];
}
then print_r($explode_arr); to check
*Note: The only problem with this approach is that, if the character that is inserted into the textbox is ~ or |, coz that is the delimiter used.
I have couple of input field and values in them. This is projected to the user.
The user can modify these values and submit them.
When submitted, I need to check which input field is modified.
I can compare the previous fields and current fields and check. But I am trying to find more optimized way to do this.
I can use javascript, php, jquery and html tricks
<input id="input1" value="someValue" type="text">
<input id="input2" value="someValue" type="text">
Script:
$('input').on('change',function(){
var id = $(this).attr('id');
alert("input field is modified : ID = " + id);
});
You can create 2 different input, 1 hidden with a class like originalVal and 1 visible for every input.
Then on submit you do something like that :
$('input').each(function(){
var currentVal = $(this).val();
var originalVal = $(this).closest('.originalVal').val()
if(currentVal != originalVal){
//This input has changed
}
})
Since no code was given, you could compare what was in the input compared to what is now in it.
HTML Input:
<input type="text" id="testInput" value="DB Value"/>
jQuery
var modifiedInputs = [];
var oldVal = "";
$("#testInput").focus(function() {
oldVal = this.value;
}).blur(function() {
console.log("Old value: " + oldVal + ". New value: " + this.value);
//If different value, add to array:
if (this.value != oldVal) {
modifiedInputs.push(this.id);
}
});
Fiddle: http://jsfiddle.net/tymeJV/tfmVk/1/
Edit: Took it a step further, on modification of an input, if the changed value is different from the original, it pushes the elements ID to the array.
I would say your best bet would be to get the initial values from the input fields, and then compare them later on. Then, just do a comparison once the submit button is clicked. For instance, put this somewhere in your $(document).ready() that way it will retrieve the initial value.
var oldValue=[];
$('input').each(function(){
oldValue.push($(this).val());
});
Then you can compare later on when you hit the submit.
you could compare with default value like this
for(var i in formObj)
if('value' in formObj[i] && formObj[i].value!=formObj[i].defaultValue){
//do what ever here ...
}
I have this code to append the input panel #yearPanel after the select box #before changed.
$("#before").change(function() {
var selected = $('#before option:selected').text();
if(selected == bankShowYear) {
$(yearPanel).insertAfter("#yearAnchor");
var value = $("#yearHiden").val();
$("#year").val(value);
} else {
$("#yearPanel").remove();
}
});
I want to keep the input value of #year in #yearPanel after submitting by PHP. I tried to store the value in #yearHidden and assign it to #year after all the input are rendered but it doesn't work.
How to keep the input value?
Try this:
$("#before").change(function() {
var selected = $('#before option:selected').text();
if(selected == bankShowYear) {
$(yearPanel).insertAfter("#yearAnchor");
var value = $("#yearHiden").val();
$("#year").val(value).attr('name', 'year');
} else {
$("#yearPanel").remove();
}
});
Then, on your next page:
$("#year").value(<?php echo $_REQUEST['year']?>);
OR
<input id="year" value="<?php echo $_REQUEST['year']?>" />
your input needs a name attribute in order for it to be passed along to your serverside php script
You need to pass the value to PHP via a hidden field and PHP will have to render it on the next page. In other words the value needs to go from the client-side to the server-side and back to the client-side.
I have a loop in php that echos out a <input type="hidden" id="lol" value=$id />
Every time the loops go through i get a new value in the hidden input field as you can understand.
Now, im trying to grab the value from each of these items and get grab it with Javascript and SAJAX.
The javascript im using now works, But! It only grabs the first value (because the ID is the same on each input)
Javscript:
function Showbooking() {
id = document.getElementById('lol').value;
x_showBookingForm(id, do_showBookingForm);
}
function do_showBookingForm(html) {
openPopup(600, 550, html);
}
As you can see im opening a POPUP with javascript aswell and exports the value into that popup window.
So on every popup I get the same value (the value from the first input).
How Do I get around this problem?
Change ID to name
Use document.getElementsByName and loop
var lols = document.getElementsByName("lol");
var vals=[];
for (var i=0, n=lols.length;i<n;i++) {
vals.push(lols[i].value);
}
alert(vals.join(","));
getElementById says element rather than elements because it returns only one item. id is supposed to be unique. You could do something to the effect of:
var inputs = document.getElementsByTagName("input");
var values = [];
for(var i=0;i<inputs.length;i++){
if(inputs[i].type === "hidden"){
values.push(inputs[i].value;
}
}
Is it possible to get some other attribute's value than attribute named value with $_POST
example: <option value="FusRo" name="Dah"></option>
Normally when i use $_POST['Dah'] The php grabs FusRo (the value).
But I want to grab another attribute's value than attribute named value. I hope you understand.
If I cant use $_POST to grab some other value, is it some other comand i can use?
Another example:
If i use
<option value="FusRo" name="Dah"></option>
Can I get the "Dah" with $_POST instead of "Fusro" ?
You can put your other value in a hidden field:
<input type="hidden" name="DahHidden" value="somethingelse" />
Then get it from $_POST with:
$_POST['DahHidden']
If this value has to dynamically change based on what's in the <select>, then you'll need JavaScript.
If you want to grab the keys from $_POST (i.e. the name attributes from your form fields), you can iterate over $_POST like this:
foreach( $_POST as $key => $value)
echo $key . ' => ' . $value; // Will print Dah => value (eventually)
Note that iterating over $_POST will likely produce more output than just that one form element (unless 'Dah' is the only thing you submitted in your form.
The only way is to use JavaScript to modify the posted data, or even simpler use jQuery
then it would look like something like this :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.js"></script>
</head>
<body>
<form name="skyrim" id="skyrim">
<input type="text" value="FusRo" name="Dah" data-name="dhovakin" data-race="kajit" />
<form>
<script>
$('#skyrim').submit(function( e ){
data = {};
url = 'http://sandbox.local/testpost.php';
e.preventDefault();
$('input, textarea', this).each(function(){
var pcs = $( this ).data();
var ename = $( this ).attr('name');
if(undefined == data[ ename ] ){
data[ ename ] = {};
data[ ename ]['_'] = $(this).val();
}
$.each(pcs, function(k, v){
data[ ename ][k] = v;
});
});
$.ajax({
url : url
,data : data
,type : "POST"
}).done(function(){
});
});
</script>
</body>
</html>
The above code will add all the attributes starting with data- to the post .
the result of the above is :
Dah[_] FusRo // default value
Dah[name] dhovakin // data-name
Dah[race] kajit // data-race
$_POST only gives you the names of the field, and their corresponding value, nothing more.
No, you cannot post anything else then the "value" of an inputfield.
You could hack your way into it by using javascript. Something like
document.getElementById('FORM').onsubmit = function() {
document.getElementById('FIELD').value = document.getElementById('FIELD').customAttribute
}
However, if javascript is disabled, your form will submit the wrong values. Not a really solid solution.
Sounds to me more like you will have to redefine your values, I can't really imagine why you would like to alter this behavior.