Tagging system with dynamic input - php

Right now I'm trying to combine the bootstraps tagging-system with a dynamic input field code. I want for every dynamic generated field/div a tag-inputfield.
like this way:
Topic1: Title
Description: it's about a title
Tags: hello, boring, title
Topic2: it's a title again
Description: it's a description again
Tags: idk, help
(...) and so on.
My jquery file for adding additional fields looks like this:
$(document).ready(function () {
var maxGroup = 10;
$(".addMore2").click(function() {
$(".tagging").css("display", "none");
if ($('feld2').find('.fieldGroup').length < maxGroup) {
var fieldHTML = '<div class="form-group fieldGroup">' +
$(".fieldGroupCopy").html() + '</div>';
$('feld2').find('.fieldGroup:last').after(fieldHTML);
} else {
alert('Maximum ' + maxGroup + ' groups are allowed.');
}
});
//remove fields group
$("feld2").on("click", ".remove", function () {
$(this).parents(".fieldGroup").remove();
});
});
The not working part is this one:
When I click on my "add" button:
see here
it makes a copy of the this part:
<div class="form-group fieldGroupCopy" style="display: none;">
<table>
<tr>
<td class='first_td'><label for="titel"><b>Titel:</b></label></td>
<td><input type="text" name="description[]" class="form-control" placeholder="Title"/></td>
<td>-</td>
</tr>
<tr>
<td><b>Beschreibung:</b></td>
<td><textarea type="text" name="description[]" class="form-control" placeholder="Beschreibung des Themas"/></textarea></td>
</tr>
<tr>
<td><label for="Tags"><b>Tags</b></label></td>
<td colspan='2'>
div class="form-group">
<input type="text" name='tags_WiBe[]' placeholder='Add Tags' data-role="tagsinput" class="form-control" />
</div>
</td>
</tr>
</table>
</div>
But the tagging-system does not work within the copied fields.
Its only working outside of the div "form-group fieldGroup".
see here
Everytime I enter a tag the form wants to submit, but thats not what I want.
Please help me...
edit:
Here is a jsfiddle to show the problem more clearly.
https://jsfiddle.net/t5vrLsur/#&togetherjs=pbAhjTR1t1
I know it's not the most beautiful structure.
Don't be too hard on me. :(

The thing is that the plugin itself take care about the first initialisation on DOM ready. In your case, you want to run it after each adding card.
You can do this by simple run the plugin initialisation in the new card context.
Also, you have to remove the data-role attribute from the input in the template because, as we said, this attribute meant to initialise the plugin automatically but in the template's case we will do it manually after we will load the card.
So here are the relevant lines:
// wrap the html with jQuery so you could delete the inputtags wrapper later
var fieldHTML = $('<div class="form-group fieldGroup">' + $(".fieldGroupCopy").html() + '</div>');
$('feld2').find('.fieldGroup:last').after(fieldHTML);
// initialise again
fieldHTML.find('input').tagsinput();
Working demo
http://output.jsbin.com/dabijac/
Also, you have couple of jQuery references in your code which cause problem. You have either remove it or using use $.noConflict to keep them.

Related

Auto-Populate text field from drop-down selection in dynamically added rows

I have a form in which users can dynamically add rows. In each row there is a drop-down menu of products that should auto-populate a text field with the price associated with the product chosen. This works perfectly for the first row, but does not work in the dynamically added rows. The product names are still being pulled from the mysql database into the drop-down, but it is not auto-populating the text field when chosen. Any help would be appreciated!
EDIT: I added the following section, which I think will make this whole thing work, I just need to figure out how to attach the i variable to the name or id or class, and then I can have the auto-populate code include price[i] and product[i]... and I THINK that will make it work for each dynamically added row. Any ideas now?
for(var i=0;i<$('.orderform tr').length;i++)
{
}
END EDIT
Auto-populate code:
<script>
$(function() {
$('select[name="product[]"]').change(function()
{
$('#price').val($('select[name="product[]"] option:selected').data('price'));
});
});
</script>
Adding a row code:
<script>
$(document).ready(function(){
//This line clones the row inside the '.row' class and transforms it to plain html.
var clonedRow = $('.row').clone().html();
//This line wraps the clonedRow and wraps it <tr> tags since cloning ignores those tags
var appendRow = '<tr class = "row">' + clonedRow + '</tr>';
$('#btnAddMore').click(function(){
//this line get's the last row and appends the appendRow when it finds the correct row.
$('.orderForm tr:last').after(appendRow);
for(var i=0;i<$('.orderform tr').length;i++)
{
}
});
</script>
HTML/PHP:
<table class="orderForm" id="orderForm" width="100%">
<tr class="row">
<td>
<div class="pure-control-group">
<label>Product or Service</label><select name="product[]" id="product">
<option value=""></option>
<?php while($productRow = mysql_fetch_assoc($productResult)){?>
<option value="<?php echo $productRow['prouct_id'];?>" data-price="$<?php echo $productRow['price']; ?>"><?php echo $productRow['product']; ?></option>
<?php } ?>
</select>
</div>
<div class="pure-control-group">
<label>Price</label><input type="text" id="price" name="price[]">
</div>
<input type="button" class="deleteThisRow" id="deleteThisRow" value="Delete"/>
</td>
</tr>
</table>
<input type="button" id="btnAddMore" value="Add Product or Service" class="pure-button"/>
.clone() by default doesn't clone the event handlers. You can use .clone(true).appendTo(".orderForm");. The true parameter of the .clone() function copies the values and events over as well.

How can I add more textboxs to page with php?

So I have this snippet of code that i want to insert into my site.
<table border="1">
<col width="130">
<col width="80">
<tr>
<td align = "right">Steps:</td><td align="center">Add as many steps as you need</td>
</tr>
<tr>
<td align = "right">Step 1:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<tr>
<td align = "right">Step 2:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<!--<tr>
<td align = "right">Step 3:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
<tr>
<td align = "right">Step 4:</td> <td><textarea style="width: 300px" class="form-control" name="steps[]" rows="3"></textarea></td>
</tr>
-->
</table>
For the most part, I have some commented out since I am testing to make sure that my site can handle multiple text boxs but the problem that I have is how can I insert steps into my site using php? I have these all on .php files in case you are wondering but I would like to insert more boxes with a button that is just under the current boxes. Every time the user clicks it, it should insert another box underneath the current boxes and update the number accordingly. I just am not sure where to start and how to get them in. Any ideas?
You would create new elements in JS and jQuery using the following code:
HTML
<button id="addButton">Add me!</button>
JS
var stepCounter = 3; //Number of the first step to be added
$('#addButton').click(function () {
var tr = document.createElement("tr");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var textarea = document.createElement("textarea");
$(textarea).attr("name", "steps[]");
$(td1).innerHTML("Step " + stepCounter);
stepCounter++;
$(tr).append(td1);
$(tr).append(td2);
$(td2).append(textarea);
$('#giveTheColAnId').append(tr);
});
Remember to include jQuery for this solution
Note that this code might not be perfect, but it should give you a very good start.
You can add these new Boxes with JavaScript/JQuery, if you dont want to reload the page after adding a new box. I would really recommend to have a look at this, instead of trying to do it with PH.
If you really want to do it just with php, you have to have a submit-button in a form
so add something like
<form method="post" action="?">
<input type="submit" name="submit" value="add row">
</form>
Now the PHP-part:
if(isset($_POST['submit'])) {
echo " [your usual tr-td-structure] ";
}
Keep in mind, that after reloading the page, a variable will lose its value.
So if you want to count up, you have to do it with javascript (or determine somehow which Step is the current and insert it in the echo). Of course set the last step in a hidden field inside the form (or append it as a get-param).
Can you use Jquery, Can't you do something like this ?
$(document).ready(function(){
$('#myButton').click(function(){
$(':input').append('<input type="textbox">');
});
});

Wordpress jQuery to PHP data transfer

everyone. I'm a relatively new developer with limited experience in jQuery AJAX and PHP. I'm working on a Wordpress plugin that is very much a learning exercise for me as well.
Basic description: On my plugin's admin page, I have a bunch of forms that will be displayed as modal windows (using jQuery UI) and when filled out, will submit their fields to a separate PHP file for processing. That file will accept the data and prepare it for insertion into the wpdb table I have set up.
Plugin Admin page (PHP):
<div id="form1" title="My Awesome Form">
<form id="frmNewCom">
<fieldset>
<table>
<tr>
<td><label for="name">Community Name</label>
<td><input type="text" name="newComName" id="newComName" />
</tr>
<tr>
<td><label for="lefthead">Left Column Header</label></td>
<td><input type="text" name="newComLefthead" id="newComLefthead" /></td>
</tr>
<tr>
<td><label for="righthead">Right Column Header</label></td>
<td><input type="text" name="newComRighthead" id="newComRighthead" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
jQuery UI code for form ($pcal is my no-conflict thing):
$pcal('#form1').dialog({
autoOpen: false,
height: 275,
width: 400,
modal: true,
buttons: {
"Add Living Option": function() {
// Functionality for submit
},
Cancel: function() {
$pcal(this).dialog("close");
}
},
close: function() {
// close function
}
});
Now here's the problem. I'm not really sure what to do from this point. I've read a bunch of stuff about using .post() and .serialize(), and I've gotten myself very confused at this point.
Do you guys have some insight into the proper javascript/jQuery to PHP handling you could lend? I've asked this on the Wordpress forum as well, but I've always found good advice here, so thought I'd ask. Any and all help is appreciated.
A very simple example would be :
$pcal.post("test.php", $pcal("#frmNewCom").serialize(), function() {
// this will run once returned from PHP
alert('thanks');
$pcal(this).dialog("close");
});
This posts the form (id = frmNewCom) in a serialized format (newComName=1&newComLefthead=2&newComName=3 where 1,2 and 3 are the values entered in your form) using .post() and .serialize()
then in test.php you would access your values like this :
$newComName = $_POST['newComName'];
$newComLefthead = $_POST['newComLefthead'];
$newComName = $_POST['newComName'];
// insert into table
The above is a) untested and b) does not contain any prevention against SQL injection should you want to store this in a DB

light box to show content according to what I have pressed

My problem has to do with PHP, jQuery and CSS.
I want to make a lightbox in a while loop, and then have the lightbox give me info for each of the rows.
There is a problem because it makes lightboxes for each of the rows, and with position:absolute we can see only the last row from the result. I don't want to see the last but I want the light box to show me info depending on which row I have clicked.
Here is the code:
jQuery:
$(document).ready(function(){
$('.lightbox').click(function(){
$('.boxi').css('display','block');
});
});
PHP:
$result = $db->query("SELECT * FROM destinations WHERE direction=1;");
while ($rows = mysql_fetch_array($result)) {
$name = $rows['name'];
$table .= '<div class="destionations">
<div class="name">Prej: <strong>'.$name.'</strong></div>
<table width="100%" class="extra" cellspacing="1" cellpadding="5" border="0" >
<tr class="bgC3" style="font-weight:bold">
<td width="20"></td>
<td>Deri</td>
<td width="50">Çmimi</td>
'.managment::cmimet_e_caktuara($name).'
</tr>
</table>
<div class="buttoni">
test
<div class="boxi">'.$name.'</div>
<form action="" method="POST">
<input type="text" name="new_city">
<input type="hidden" name="prej" value="'.$name.'">
<input type="submit" name="new_dest" value="Shto destinacionin">
</form>
</div>
</div>';
}
}
It looks like you're wanting to turn a div into a lightbox? You could just try adjusting your current CSS on .boxi to display: none; and set a z-index on it before - then adjust your javascript to something like this:
$('.lightbox').click(function(){
$('.boxi').show();
return false;
});
You could get much more complex with grabbing the next lightobx container when .lightbox is clicked - I'd recommend checking out the plugin Fancybox at fancybox.net, that's the one I typically use. Good luck!
$(document).ready(function(){
$('.lightbox').click(function(){
$('.boxi').css('display','block');
$('.boxi').css('z-index','999999');
});
});
I tried this because I thought that it will give higher z-index to the selected block but no its really primitive and wont ever work.
I am really too far from jQuery!

Put specific tds from a table row into edit using jQuery (then update w/ ajax)

I'm somewhat new to jQuery, so I could use some help here.
This is my issue:
I have a PHP script outputting a dynamic table. Each row has an "edit" button, plus some other fields. Only 3 of those need to be turned into an input box. The edit button should only put that specific row into "edit mode." I got as far as assigning each row a unique class by adding a number to the end of it.
I have been able to use jQuery to change all of the rows into edit mode, but I need it to be specific to a row.
An example row would have classes like name0, price0, and desc0. The next row would go on to classes name1, price1, and desc1 (for the fields that need changed). How can I reference these values and pass them to jQuery so it processes an event on just those elements?
There are two ways of doing this:
Dynamically creating the elements when the button is pressed; or
Hiding and showing elements that already exist.
Too much DOM manipulation can be really slow (particularly on certain browsers) so I favour (2). So for example:
<table class="editable">
<tbody>
<tr>
<td>one</td>
<td>
<div class="view">two</div>
<div class="edit"><input type="text"></div>
</td>
<td>
<div class="view">three</div>
<div class="edit"><input type="text"></div>
</td>
<td>
<input type="button" class="edit" value="Edit">
<input type="button" class="send" value="Send" disabled>
<input type="button" class="cancel" value="Cancel" disabled>
</td>
</tr>
</tbody>
</table>
with:
table.editable div.edit { display: none; }
and
$(function() {
$(":button.edit").click(function() {
var row = $(this).closest("tr");
row.find("input.view").attr("disabled", true");
row.find("div.view").each(function() {
// seed input's value
$(this).next("div.edit").children("input").val($(this).text());
}).fadeOut(function() { // fade out view
row.find("div.edit").fadeIn(function() { // fade in edit
row.find("input.edit").removeAttr("disabled"); // enable edit controls
});
});
});
$(":button.cancel").click(function() {
var row = $(this).closest("tr");
row.find("input.edit").attr("disabled", true");
row.find("div.edit").fadeOut(function() {
row.find("div.view").fadeIn(function() {
row.find("input.view").removeAttr("disabled");
});
});
});
$(":button.save").click(function() {
// ...
});
});

Categories