I got a code where you can add field inputs to a list, and then echo it out. What I'm looking for is a way of having a total of 3 fields. I not sure how to do it with the loop and so on, since the name of the field is the loop itself.
<?php
if(isset($_REQUEST['submit'])){
$field_values_array = $_REQUEST['field_name'];
foreach($field_values_array as $value){
echo $value;
}
}
?>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><img src="remove-icon.png"/></div>'; //New input field html
var x = 1; //Initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(x < maxField){ //Check maximum number of input fields
x++; //Increment field counter
$(wrapper).append(fieldHTML); // Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
</script>
<div class="field_wrapper">
<div>
<input type="text" name="field_name[]" value=""/>
<img src="add-icon.png"/>
</div>
</div>
Edit: 3 inputs instead of one being echoed out at the same row, and will be looped out as one. Something like an array $array[0],$array[1],$array[2]
I tried this solution, but doesn't work:
<?php
if(isset($_REQUEST['submit'])){
$field_values_array = $_REQUEST['field_name']." - ".$_REQUEST['field_name2']." - ".$_REQUEST['field_name3'];
foreach($field_values_array as $value){
echo $value;
}
}
?>
This is how you loop through each value in a $_REQUEST and get all values where the name is equal to field_name.
<?php
if(isset($_REQUEST['submit'])){
foreach ($_REQUEST as $parent => $value) { //Loop over $_REQUEST
if($parent == "field_name")
{
foreach ($_REQUEST[$parent] as $key=> $item){
//You can check if $item is empty
if(!empty($item))
{
echo "field_name[".$key ."] => ".$item."<br/>";
}
}
}
}
}
?>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 10;
//Input fields increment limitation
var addButton = $('.add_button');
//Add button selector
var wrapper = $('.field_wrapper');
//Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/>X</div>';
//New input field html
var x = 1;
//Initial field counter is 1
$(addButton).click(function(){
//Once add button is clicked
if(x < maxField){
//Check maximum number of input fields
x++;
//Increment field counter
$(wrapper).append(fieldHTML);
// Add field html
}
}
);
$(wrapper).on('click', '.remove_button', function(e){
//Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove();
//Remove field html
x--;
//Decrement field counter
}
);
}
);
</script>
<form>
<div class="field_wrapper">
<div>
<input type="text" name="field_name[]" value=""/>
Add
<button name="submit" type="submit">
Send values
</button>
</div>
</div>
</form>
New full source demo
Related
these are the javascript functions i'm using
function addFac() {
$("<div>").load("includes/facility.php", function() {
$("#fac").append($(this).html());
});
}
function deleteFac() {
$('div.facility-item').each(function(index, item){
jQuery(':checkbox', this).each(function () {
if ($(this).is(':checked')) {
$(item).remove();
}
});
});
}
here is the HTML code:
<div class="facility-item well" style="clear:both;">
<div class="row">
<div class="col-lg-1 col-sm-1 col-xs-3 padder-col">
<input type="checkbox" class="chk-lg">
</div>
<div class="col-lg-10 col-lg-offset-1 col-md-9 col-md-offset-2 col-sm-11 col-xs-9">
<input class="form-control" type="text" placeholder="Describe facility" name="faci[]" >
</div>
</div>
</div>
This code of html is written in a file named facilities.php and the file is included into the index.php. This is how i'm able to add and remove the dynamic input fields. But
when I wrote the PHP file:
$facilities = "0";
$faci = $_POST["faci"];
var_dump($faci);
even after having multiple facilities, it showed only one value in array.
Here is the working code Paste and run.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><img src="remove-icon.png"/>remove</div>'; //New input field html
var x = 1; //Initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(x < maxField){ //Check maximum number of input fields
x++; //Increment field counter
$(wrapper).append(fieldHTML); // Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
</script>
<form name="karan" action="" method="post">
<div class="field_wrapper">
<div>
<input type="text" name="field_name[]" value="">
add
</div>
</div>
<input type="submit" name="submit" value="SUBMIT">
</form>
<?php
print '<pre>';
print_r($_REQUEST['field_name']);
print '</pre>';
//output
?>
<?php
$field_values_array = $_REQUEST['field_name'];
foreach($field_values_array as $value){
//your database query goes here
}
?>
I have a scenario something like below.
When clicking on the "Add More Colour" button the same
<input type="text"> and <select>
will be added below. However, I stuck at this part where the 2nd row or onwards with empty data inside <select>
Add and remove are working fine just that the Select filed is empty.
What I want to achieve is that every new Row is the same. Anyone know how to fix that?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="product_colour[]"/><select name="product_image[]"></select>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
</head>
<body>
<?php
$pic[]="yellow.jpg";
$pic[]="blue.jpg";
$pic[]="red.jpg";
?>
<form action="jq2.php" method="POST">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Colour</button>
<br>
<div>
<input type="text" name="product_colour[]">
<select name="product_image[]">
<?php
foreach ($pic as $abc){
echo '<option VALUE="'.$abc.'">'.$abc.'</option>';
}
?>
</select>
</div>
</div>
<button>add product</button><br><br>
</form>
</body>
</html>
please try below code, hope this will help you.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
var option = $("select").html();
$(wrapper).append('<div><input type="text" name="product_colour[]"/><select name="product_image[]">'+option+'</select>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
</head>
<body>
<?php
$pic[]="yellow.jpg";
$pic[]="blue.jpg";
$pic[]="red.jpg";
?>
<form action="jq2.php" method="POST">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Colour</button>
<br>
<div>
<input type="text" name="product_colour[]">
<select name="product_image[]">
<?php
foreach ($pic as $abc){
echo '<option VALUE="'.$abc.'">'.$abc.'</option>';
}
?>
</select>
</div>
</div>
<button>add product</button><br><br>
</form>
</body>
</html>
I want to be able to add n number of books to n number of authors. What I want to do is this:
where the light blue boxes are the buttons. I want to add multiple books to a author with the "add book" button.
the code that I have is this:
<form action="next.php" method="post">
<div class="input_fields_wrap">
<button class="add_field_button">Add Author</button>
<div><input type="text" name="myAuthorText[]"></div>
<button class="add_sub_field_button">Add Author Books</button>
<div><input type="text" name="myBooksText[]"></div>
</div>
</form>
<SCRIPT language="javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var add_subButton = $(".add_sub_field_button"); //Add sub button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="myAuthorText[]"/>Remove</div>'); //add input box
}
});
$(add_subButton).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="myBooksText[]"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</SCRIPT>
what I'm getting is:
Check this code snippet. I made some chnages in HTML and Javascript too. Please compare it with your original one to see the differences. Hope it will help you a bit:
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var commonPart = $("#commonPart");
var add_button = $(".add_field_button"); //Add button ID
var add_subButton = $(".add_sub_field_button"); //Add sub button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
var htmlToAdd = commonPart.clone().attr("id","commonPart_"+x);
htmlToAdd.find(".addedDiv").remove();
$(wrapper).append(htmlToAdd); //add input box
x++; //text box increment
}
});
$(document).on("click",".add_sub_field_button",function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(this).closest(".commonPart").append('<div class="addedDiv"><input type="text" class="bookname" name="myBooksText[]" placeholder="Book name"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
.bookname{
margin-left: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form action="next.php" method="post">
<div class="input_fields_wrap">
<button class="add_field_button">Add Author</button>
<div id="commonPart" class="commonPart">
<div><input type="text" name="myAuthorText[]" placeholder="Auth name"></div>
<button class="add_sub_field_button">Add Author Books</button>
<div><input type="text" class="bookname" name="myBooksText[]" placeholder="Book name"></div>
</div>
</div>
</form>
You can use jquery.clone()
Like this:
$( "#id-of-input" ).clone().appendTo( "#id-of-same-div" );
Place this inside button click.
JSFiddle
Not a full answer, but this gives the nesting that you're looking for and lets you add books to multiple authors. I couldn't get the "saving" to work properly, I suspect the script in your question is not the complete script.
Key changes:
added styling to indent books
don't show the add book button until there is an author
add the "add book" button below each author
add books inside the author block, not the top level wrapper block
Disclaimer:
As I mentioned above, I wasn't able to test this fully. This answer is "you might find something like this helpful" rather than "I have fully and completely solved your problem, please copy+paste+deploy".
<form action="next2.php" method="post">
<div class="input_fields_wrap">
<button class="add_field_button">Add Author</button>
<div><input type="text" name="myAuthorText[]"></div>
</div>
</form>
<style>
.author_wrap button,
.author_wrap input {
margin-left: 10px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var author_wrapper = $(".author_wrap"); // Author's book fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var add_subButton = $(".add_sub_field_button"); //Add sub button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="author_wrap"><input type="text" name="myAuthorText[]"/>Remove<br/><button class="add_sub_field_button">Add Author Books</button><div><input type="text" name="myBooksText[]"></div></div>'); //add input box
}
});
$(add_subButton).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(author_wrapper).append('<div><input type="text" name="myBooksText[]"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
Note: I took the liberty of replacing the depracated language attribute on the script tags with type='text/javascript'
If two input elements in a form - for example - text fields, lets say username and password, get text input with length > 0, how would you create an event to change a submit button color the moment the user has typed in both fields?
document.getElementById('submit').onclick = function() {
var inputs = document.getElementsByTagName('input'),
empty = 0;
for (var i = 0, len = inputs.length - 1; i < len; i++) {
empty += !inputs[i].value;
}
if (empty == 0) {
//write code for changing the button color
}
};
I like the answer above, and here is a jquery answer if you like the looks better!
$(document).ready(function() {
$("#username").change(checkFunction());
$("#password").change(checkFunction());
}
function checkFunction() {
var user = $("#username").val();
var pass = $("#password").val();
if (user && pass) {
$("#buttonid").css("background-color:#HEXCODE");
}
}
Make a javascript function that checks if both inputs have a value greater than zero and if they do it changes the color of the submit button when called.
Use the onChange attribute in the input tag and place the name of the javascript function in the attribute.
You can also set the attribute by doing
Object.onChange = functionToCall();
onChange fires the function it's set to whenever the input value changes.
If you have Jquery in your project, you can try something like this:
Assuming your inputs have ids:
<input id="username" name="username"/>
<input id="password" name="password"/>
<input type="submit" value="Submit" id="submit" />
You can bind to the keyup event on either input element to change the color of the button:
$('#username, #password').keyup(function(){
if ($.trim($('#username').val()) != '' && $.trim($('#password').val()) != '') {
$('#submit').css('background-color', 'red');
}
});
Here is the running fiddle:
http://jsfiddle.net/NS5z6/
HTML:
<input type=text id=username />
<input type=password id=password />
<input type=submit id=submit />
CSS:
#submit{ background-color: red; }
JS:
$('input[id="username"], input[id="password"]').change(function(){
if ($(this).val()!="")
{
$("input[id='submit']").css("background-color","green");
}else{
$("input[id='submit']").css("background-color","red");
}
});
You can use following logic
<input type = "text" name = "username" class = "text">
<input type = "password" name = "password" class = "text">
<input type = "button" class = "button">
<script>
$(".text").live("keyup", function(){
var filled = true;
$(".text").each(function(i, v){
if($(this).val() == ''){
filled = false
}
});
if(filled){
$(".button").css("background-color:#black");
}
});
</script>
[+] //each time I click this button the textbox will generate and I want to have a link beside each textbox, link is "remove" when I click "REMOVE" the textbox will remove..
[hello1] Remove
[hello2] Remove
[hello3] Remove
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var i=0,j=0;
var t1= new Array();
function createtext(){
i++;
t1[i]=document.createElement('input');
t1[i].type='text';
t1[i].name='text'+i;
t1[i].value = "hello"+i;
t1[i].size = 10;
document.forms[0].appendChild(t1[i]);
var mybr=document.createElement("br");
document.forms[0].appendChild(mybr);
}
</SCRIPT>
</HEAD>
<BODY >
<form action="" method="get" name="f1">
<input name="b1" type="button" onClick="createtext()" value="+">
<input name="b1" type="Submit"><br>
</form>
</BODY>
</HTML>
Well this is simple.
Just add a id attribute with your text field array that will be assigned to each newly created textarea like this:
t1[i].id='some_unique_suffix'+i
t1[i].onClick='remove("some_unique_suffix"'+i+')'
Then you can go on creating a remove link after each textfield via your loop and pass the id of that particular textfield to a remove function that will be called upon clicking on the remove link like this:
function remove(id)
{
$('#some_unique_suffix'+id).remove();
}
Hope you get the idea.
You can add a remove button along with the input tag like this:
var i=0,j=0;
var t1= [];
function add(){
i++;
var parent = document.forms[0];
var div = document.createElement('div');
var input = document.createElement('input');
input.type='text';
input.name='text'+i;
input.value = "hello"+i;
input.size = 10;
t1.push(input);
div.appendChild(input);
var removeButton = document.createElement("button");
removeButton.innerHTML = "Remove";
removeButton.onclick = function(e) {
this.parentNode.parentNode.removeChild(this.parentNode);
return(false);
};
div.appendChild(removeButton);
parent.appendChild(div);
}
Working demo: http://jsfiddle.net/jfriend00/ky9nv/
This code makes it easier to remove an input element and it's associated button by enclosing them in a containing div. A clicked button can then just get it's parent container and remove that.
And, since your question is tagged with jQuery (thought it doesn't save you a lot here), here's a version that uses jQuery:
var i=0,j=0;
var t1= [];
function add(){
i++;
var div = $('<div>');
var input = $('<input>')
.attr({
size: '10',
type: 'text',
name: 'text' + i,
value: 'hello' + i
}).appendTo(div).get(0);
t1.push(input);
$('<button>Remove</button>')
.click(function() {
$(this).parent().remove();
}).appendTo(div);
$("#myForm").append(div);
}
add();
add();
$("#add").click(add);
Working example: http://jsfiddle.net/jfriend00/nbXak/
<script type="text/javascript">
var i=0;
function createtext() {
i++;
$('<div id="field'+i+'"><input type="text" name="text'+i+'" value="Hello'+i+'" size="10" /> Remove</div>').appendTo('#inputsPlaceholder');
}
function removeField (id) {
$('#'+id).remove();
}
</script>
HTML:
<form action="" method="get" name="f1" id="f1">
<input name="b1" type="button" onclick="createtext();" value="+" />
<div id="inputsPlaceholder"></div>
<input name="b1" type="submit" />
</form>
Try it: http://jsfiddle.net/Z3L5C/