Create Element Doesn't work in jquery 1.8.3 - php

I Have used jquery 1.8.3 to Dynamically Create the Element. But It doesn't working on this. but It's working on 1.3.2 version. Below is My Jquery Code Which I Have used for that.
$(document).ready(function(){
var counter = 2;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'CallBackDiv' + counter);
newTextBoxDiv.after().html('<label>Call Back Date Time #'+ counter + ': </label>'+
'<div class="controls"><input type="text" name="callback' + counter +
'" id="callback' + counter + '" value="" ></div>');
newTextBoxDiv.appendTo("#control-group");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#CallBackDiv" + counter).remove();
});
$("#getButtonValue").click(function () {
var msg = '';
for(i=1; i<counter; i++){
msg += "\n CallBack #" + i + " : " + $('#callback' + i).val();
}
alert(msg);
});
});
<div id='control-group'>
<div id="CallBackDiv1">
<label class="control-label" for="input01">
Call Back Date Time #1:</label><div class="controls">
<input type='textbox' id='callback1' ></div>
</div>
</div>
<input type='button' value='+' id='addButton'>
<input type='button' value='-' id='removeButton'>

There's nothing wrong with the way you are creating the div. The problem with your code is your use of jQuery's after() function. Calling after() on a newly created element will return null, which you are then calling the html() method on. I suggest you read up on how to use the after() function -> http://api.jquery.com/after/

Using jQuery you can dynamically create elements like this:
// Create a new div
var newTextBoxDiv = $('<div/>', {
id: 'CallBackDiv' + counter
});
// Append the new element to the DOM
newTextBoxDiv.appendTo("#control-group");
// You can check the console for the 'newTextBoxDiv' id
console.log(newTextBoxDiv.attr('id'));
// Then call the after part
newTextBoxDiv.after().html('...');
FIDDLE DEMO

why dont you do it in plain js
var newTextBoxDiv = document.createElement('div');
newTextBoxDiv.setAttribute("id", 'CallBackDiv' + counter);

Related

Jquery not gathering textarea values and not sending arrays to php under specific conditions

I am trying to make a system that gets the value of all textareas into one array and then send it to php for processing, if a page only has textareas with textbox like here:
<div class='form-group'>
<textarea class='col-sm-2' id='" + counter + "'></textarea>
<div class='col-sm-10'>
<input type='text' class='form-control'></input>
</div>
</div>
Everything goes fine. However if the page includes radio boxes:
<div class='form-group'>
<textarea class='col-sm-2' id='" + counter + "'></textarea>
<div class='radio'>
<input type='radio' name='optradio'></input>
<textarea class='col-sm-2' id='" + counter + "'></textarea>
</div>
<div class='radio'>
<input type='radio' name='optradio'></input>
<textarea id='" + counter + "'></textarea>
</div>
</div>
The jquery code does not collect all textareas and does not successfully send it to the php code for processing.
$("#save").click(function () {
for (var i = 0; i < counter; i++) {
form1[i].push($("#" + i).val());
}
$.post("FormResponse.php", {form: form1, formid:$("#formnum").val()}, function (response, status) {
document.write(response);
});
Update:
I changed my jquery code around to get both the type and the textarea values in one place and it still doesn't work. I didn't want to use objects because I want it to be easy for my php code to differentiate which textareas belong to which inputs.
Here is the code revision:
$("#save").click(function () {
var ind = 0;
for (var i = 0; i < inputcounter; i++) {
if (document.getElementById("input#" + i) !== null) {
form1.push($("input#" + i).type());
ind = ind + 1;
}
form1[ind].push($("textarea#" + i).val());
}
document.write(form1);
$.post("FormResponse.php", {form: form1, formid: $("#formnum").val()}, function (response, status) {
document.write(response);
});
});
Haven't tested it but try this.
$("#save").click(function () {
var data = {};
$.each($('textarea'),function(){
var $this = $(this);
data[$this.id] = $this.val();
})
$.post("FormResponse.php",data,function(rep){
console.log(rep);
});
});
Edit
I have no idea what your comment means.
You do realize that you sending a object in your code right?
The PHP code still accesses the data in the same way.
If you mean that you don't know what index the data will be in, then try the code below.
$("#save").click(function () {
var data = {};
$.each($('textarea'),function(){
var $this = $(this);
data[$this.id] = $this.val();
})
$.post("FormResponse.php",{form:data, formid:$("#formnum").val()},function(rep){
console.log(rep);
});
});

Posting and retrieving Multiple Text box values using Jquery and PHP

I have Jquery code that can create multiple text boxes .Now, i want to submit my text boxes' values so that i could get them using PHP on next page.I want to submit text box in the same way as ( )
and on the second page,i could get values using POST method.How can i i post multiple text values in this case and how can i retrieve them on the next page using php?? Plz help as i am a new bee.
CODE:
<head>
<script type="text/javascript">
$(document).ready(function(){
var counter = 2;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
'<input type="text" name="textbox' + counter +
'" id="textbox' + counter + '" value="" >');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--; $("#TextBoxDiv" + counter).remove(); });
$("#getButtonValue").click(function ()
{
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
});
</script>
</head><body>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='textbox' id='textbox1' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>
</body>
Try to giving name for text boxes as array
e.g. input type='textbox' name='textbox[]' id='textbox1'
and access it on PHP page as array
$arrayRequest = $_REQUEST['textbox'];

Dynamically add equal select form fields with data taken from php

I have a form with select fields dynamically generated and with the options taken from php (same for all the selects). But I need that every <select>...</select> must be shown in a different line. This can be done having every <select>...</select> inside a <div>...</div>.
This is the code that I have, it´s working but the select fields are not within a div:
<script type="text/javascript">
//<![CDATA[
$(function(){
var counter = 1;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var select = $('<select>').attr({id:'select'+counter,name:'select'+counter});
$.ajax({
url: 'selects.php',
dataType: "html",
success: function(data) {
select.html(data);
}
});
select.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#select" + counter).remove();
});
$("#getButtonValue").click(function () {
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
});
//]]>
</script>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='text' id='textbox1' >
</div>
<div id="TextBoxDiv2">
<label>Textbox #2 : </label><input type='text' id='textbox2' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>
And this is the html code generated by select.php:
<option value="1">Uno</option>
<option value="2">Dos</option>
<option value="3">Tres</option>
<option value="4">Cuatro</option>
Use the wrap() function. :
Put this code just before the counter++;. :
$("#TextBoxesGroup > select").wrap ( function () {
return '<div id="wrap_' + this.id + '"></div>';
} );
See it in action at jsFiddle.

Dynamically remove select form fields inside divs with data taken from php

I have a form with select fields dynamically generated and with the options taken from php (same for all the selects). The "add" button is working but the "remove" button is not working perfectly, it´s not deleting everything and if you add again, it goes below the ideal position.
This is the code that I have:
<script type="text/javascript">
//<![CDATA[
$(function(){
var counter = 1;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var select = $('<select>').attr({id:'select'+counter,name:'select'+counter});
$.ajax({
url: 'selects.php',
dataType: "html",
success: function(data) {
select.html(data);
}
});
select.appendTo("#TextBoxesGroup");
$("#TextBoxesGroup > select").wrap ( function () {
return '<div id="wrap_' + this.id + '"></div>';
} );
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#wrap_" + counter).remove();
$("#select" + counter).remove();
});
});
//]]>
</script>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Textbox #1 : </label><input type='text' id='textbox1' >
</div>
<div id="TextBoxDiv2">
<label>Textbox #2 : </label><input type='text' id='textbox2' >
</div>
</div>
<input type='button' value='Add Button' id='addButton'>
<input type='button' value='Remove Button' id='removeButton'>
And this is the html code generated by select.php:
<option value="1">Uno</option>
<option value="2">Dos</option>
<option value="3">Tres</option>
<option value="4">Cuatro</option>
the problem ids this when you return
return '<div id="wrap_' + this.id + '"></div>'
this.id is select's id
but when you are removing
$("#wrap_" + counter).remove();
try to remove by
$("#wrap_select" + counter).remove();
because your div's id is
wrap_select+'some value' not wrap_+'some value'
As the other 2 answers said, the problem is:
$("#wrap_" + counter).remove();
Change it to:
$("#wrap_select" + counter).remove ();
To match how the wrap-div was created.
See it in action at jsFiddle.
The selects get the id 'select'+counter, and the wrappers get the id 'wrap_' + this.id, and this.id is, if I'm not too morning tired, not the same as the counter.
And since you're doing this
$("#wrap_" + counter).remove();
$("#select" + counter).remove();
You're not removing the wrapper.

post dynamically generated textbox using foreach loop

i want to post dynamically generated multiple text box using php by foreach loop.
for instance i m working on question bank and for post multiple answer from php.
here is my code that dynamically generated textbox but now i donot know how to post value of generated textbox thru foreach loop.
$("#questionbank").live('click',function(){
var mydata = '<div id="questionbank-content" class="page">';
mydata += '<div class="question"><div class="Label">Question</div><div class="textarea"><textarea id="question" rows="3" cols="30"></textarea></div></div><div class="answer"><div class="Label">Answer</div><div class="textarea"><textarea id="answer" rows="3" cols="30"></textarea></div><div class="option"><input id="a" name="answers" type="radio"></div><span id="add">Add</span>';
var i=1;
$('#add').live('click',function(){
j=i++;
$(this).after('<div class="Label">Answer</div><div class="textarea"><textarea id="answer + '+ j +'" rows="3" cols="30"></textarea></div><div class="option"><input id="a + '+ j +'" name="answers" type="radio"></div><span id="add">Add</span>');
//$(this).after('<input id="'+ j +'" type="text" value="'+ j +'"/><span id="add">Add</span>');
$(this).remove();
});
mydata += '</div></div>';
$("#leftcontainer").html(mydata);
});
Consider the following HTML
<div id="start">Start</div>
<div id="container">
</div>
I guess you want to post data from each value of the textbox/textarea by $.post() or $.ajax()
var i=1;
$('#start').one('click',function(){
j=i++;
$('#container').append('<input id="answer'+ j +'" type="text" value=""/><span id="add">Add</span>');
$('#container').after('<div id="submit">Submit</div');
});
$('#add').live('click',function(){
j=i++;
$(this).after('<input id="answer'+ j +'" type="text" value=""/><span id="add">Add</span>');
$(this).next().next().after('');
$(this).remove();
});
$('#submit').live('click',function(){
var x = $('#container > input');
var qstring='op=insertquestion'; //additional para if u want to pass any
$.each(x,function(index,value){
var id = $(this).attr('id');
var data = $(this).val();
qstring += '&'+id+'='+data+'';
});
$('#start').before(qstring + '<br/>');
});
After you get the variable qstring just post the string using $.post() method.
Example on JSFIDDLE.net
The easiest way would be to construct the textarea and submit button via jQuery and attach an click handler. Not a complete solution but it should hopefully give you an idea:
$("#add").live("click", function() {
var textarea = $("<textarea />", { "class": "textarea" });
var submitButton = $("<button />", { "type": "submit" });
submitButton.bind("click", function() {
var text = textarea.val();
$.post("/path/to/script.php", { text: text });
});
});
This will create a textarea and a submit button. When invoking the submit button, a HTTP POST is made to script.php with the textarea content as a parameter.

Categories