Dynamic Input Values Not Recognized By Php - php

I have a table where i dynamically add rows with input fields based on the user requirement. These input fields are stored as an array so that looping over them can get the values in each row to insert into the db. The issue is that only the first "static" row from the table is recognized in the array. Every other dynamic input field is not stored in the input array when submitted. A snippet of the table code is
<tr><td><input type=number name=minimum[] required></input></td><td><input type=number name=maximum[] required></input></td> <td><input type=number name=overall[] required></input>
<td><?php echo $formular;?></td></tr>
</tbody><tfoot><tr><td colspan="25"><div class="text-center">
<button type="submit" class="btn btn-success">Submit</button>
</div></td></tr></tfoot></table></div></form>
The jquery code to add an extra row is:
<script>
$(window).load(function(){
$(function () {
var rowv="<tr><td><input type=number name=minimum[] required></input></td>
<td><input type=number name=maximum[] required></input></td><td><input
type=number name=overall[] required></input></td><td><?php echo $formular;?>
</td></tr>";
$("#addRow").click(function () {
var row = $(rowv);
$("#mt > tbody").append(row);
});
});
});
</script>
The (abbreviated) php code to process the array is :
foreach($_POST['fi'] as $key=>$value){
$sql="insert into band(fi) values ('$value')";
}
Only the first row is being inserted. Any row that added dynamically is not submitted to the post array. How can i resolve this?

Create your input field like this and then append it to the form and then try.
var input = document.createElement("input");
input.type = "text";
input.name = "minimum[]";
input.required = "true";
$("#testForm").append(input);

Your jQuery syntax seems incorrect. Change that click function to this:
$("#addRow").click(function () {
$("#mt > tbody").append(
"<tr>" +
"<td><input type='number' name='minimum[]' required></td>" +
"<td><input type='number' name='maximum[]' required></td>" +
"<td><input type='number' name='overall[]' required></td>" +
"<td><?php echo $formular;?></td>" +
"</tr>"
);
});

Related

how to get multiple value of textbox onclick on checkbox

code:
<script>
$(document).ready(function(){
$(".check").click(function(){
comment = $(".comment2").val();
alert(comment);
});
});
</script>
<?php
$sql = "select * from enquires2";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<tr>
<td>
<input type='checkbox' class='check' id='chk".$row['id']."' name='chk' value=".$row['id'].">
</td>
<td>
<input type='text' name='comment2' class='comment2' id='comment2_".$row['id']."' value='' />
</td>
</tr>";
}
?>
In this code when I enter text inside comment2 input field and then check checkbox it alert only 1st row value but when I enter second textbox and then again check checkbox it show nothing. So, how can I get multiple textbox value onclick on checkbox ? Please help.
Thank you
Problem is, that both the elements are of class .comment2, which means the $(".comment2") creates an array for you, and the .val() only gets the value of the first element in the array.
You have to use a more unique value (since ID's not always start at 0, a simple counter thingy wont work)
May I suggest
<script>
$(document).ready(function(){
$(".check").on("click", function(){
var $id = $(this).val();
comment = $("#comment2_" + $id).val();
alert(comment);
});
});
</script>
Using $(this).val() to get the id value saved in your checkbox, and concatenating that with the ID comment2_x to get the content of the input box

Getting the value of a dynamically created input

I have following jquery script which load a form into a div "blankform"
jQuery('#metentry').submit(function(e){
e.preventDefault();
// getting data from form to variables
var date = jQuery('#stddate').val();
var kgsunit = jQuery('#unit').val();
var kgsshift = jQuery('#shift').val();
//sending data to script
jQuery.post("get_blank_form.php", {
"date": jQuery('#stddate').val(),
'unit': kgsunit,
'shift': kgsshift,
}, function(data) {
//loading a form to the div blankform
jQuery('#blankform').html(data);
});
get_blank_form.php contains following code
<?php
echo'<form id="shiftentry" name="shiftentry" >';
echo "Date:<input id=shiftdate value='".$date."'/>";
echo "Unit:<input id=shiftdate value='".$unit."'/>";
echo "Shift:<input id=shiftdate value='".$shift."'/><br/>";
echo "<table><tr><th>No</th><th>Ele</th><th>Location</th><th>Dose Rate (mGy/h)</th><th> Tritium (DAC)</th> <th>Part (DAC)</th> <th>Iodine (DAC)</th><th> Surface Cont. (Bq/cm2) </th></tr>";
while($row2 = mysql_fetch_array($result_sec))
{
echo "<tr>";
echo "<td>".++$rc."</td>";
echo "<td><input size='5' id='".$row2['elevation']."' value='".$row2['elevation']."' /></td>";
echo "<td><input id='".$row2['loc_id']."' value='".$row2['location']."' /></td>";
echo "<td><input size='5' id='dose".$rc."' value='".$row2['radn_level']."'/></td>";
echo "<td><input size='5' id='h3".$rc."' value='0' /></td>";
echo "<td><input size='5' id='part".$rc."' value='0' /></td>";
echo "<td><input size='5' id='iod".$rc."' value='0' /></td>";
echo "<td><input size='5' id='cont".$rc."' value='0' /></td>";
echo "</tr>";
}
echo " </table>";
echo '<div align="center">';
echo '<input type="submit" id="submit" name="submit" value="Submit" width="30" />';
echo '</div>';
?>
this will create a form which will have input like id1, id2 ... id25, dose1, dose2 ... dose25 and so on.
I want to read the values of these input boxes. Normal method like var loc1 = $("#id1").val(); is not working since these elements are dynamically created.
Even the submit button clicking even is also not triggered.
the script below do not produce any output.
jQuery('#shiftentry').submit(function(e) {
e.preventDefault();
alert("Submitted");
Any suggestions please?
Since the Form DOM is dynamically created after Ajax Call, You can use .on ?
like:
$(document).on('submit' , '#shiftentry' , function(e) {
e.preventDefault();
alert("Submitted");
});
You will need to use on() method for event handling as the form is created using ajax call and added later.
As it's added later, it will not be bound to submit event handler that you had specified. To bind it with the handler you need to use on() from jQuery.
$(document).on('submit', '#shiftentry', function() {
//Your code
});
As for the submit button, you can trigger it using
$('#submit_id').on('click', function(){
$('#shiftentry').send(function(e) {
e.preventDefault();
alert("Submitted");
);
})
as for getting the values using.
$('#id1').val()
this should just work just fine but remember that some characters are not permitted in id field such as [, ], . etc.. if you cant avoid this special characters in your id field you can use.
$('input[name="value"]').val();
to get the value of your fields.

convert checked rows to input fields

I am showing data from database into table with the checkbox in front of every row. All I want is to change the checked rows columns with class click into input fields on clicking the edit button. I am very new to jQuery. I don't have much concepts about it. So help me please.
Here is the jQuery code I've tried:
$(function () {
$("input[name='check[]']:checked").each(function (){
$('#edit').click(function(){
var OriginalContent = $('.click').text();
$('.click').addClass("cellEditing");
$('.click').html("<input type='text' value='" + OriginalContent + "' />");
$('.click').children().first().focus();
$('.click').children().first().keypress(function (e) {
if (e.which == 13) {
var newContent = $('.click').val();
$('.click').parent().text(newContent);
$('.click').parent().removeClass("cellEditing");
}
});
$('.click').children().first().blur(function(){
$('.click').parent().text(OriginalContent);
$('.click').parent().removeClass("cellEditing");
});
});
});
});
And here is the html:
echo '<div class="table-responsive">';
echo '<table class="table table-bordered table-hover"><thead><tr>
<th>ID</th><th>Name</th><th>Category</th><th>Description</th><th>Price</th><th>Status</th><th colspan="2">Image</th></tr></thead><tbody>';
while($row = mysqli_fetch_array($retval))
{
echo '<tr><td>'.$row["ID"].'</td>
<td >'.$row["Name"].'</td>
<td>'.$row["Category"].'</td>
<td>'.$row["Description"].'</td>
<td class="click1">'.$row["Price"].'</td>
<td class="click">'.$row["Status"].'</td>
<td><img style="width: 3em; heigth: 3em;" class="img-responsive" src="'.$row["Imagepath"].'" alt="'.$row["Name"].'"/></td>
<td><input class="checkbox" name="check[]" id="check[]" type="checkbox" value='.$row["ID"].'/></td></tr>';
}
echo '</tbody></table>';
echo '</div>';
?>
<center><input class="btn btn-default" name="deletebutton" type="submit" value="DELETE" />
<input class="btn btn-default" name="edit" id="edit" type="button" value="EDIT" />
<input class="btn btn-default" name="updatebutton" type="submit" value="UPDATE"/></center>
</form>
Basically you need two functions to run...
when "edit" is clicked: convert all "editable" columns to INPUTs, if that row is ":checked"
when ENTER is pressed while editing one of your "editable" INPUTs: revert it to a string
I simplified your HTML a bit for clarity. An explanation of the jQuery needed is inline.
Also, here's a working jsFiddle: http://jsfiddle.net/msz76tgp/2/
HTML
<table>
<tr>
<td class="editable status">something</td>
<td class="editable price">2</td>
<td class="checkbox"><input type="checkbox"/></td>
</tr>
<tr>
<td class="editable status">something else</td>
<td class="editable price">3</td>
<td class="checkbox"><input type="checkbox"/></td>
</tr>
</table>
<a id="edit" href="#edit">edit</a>
jQuery
$(function () {
// When edit is clicked...
$('#edit').on('click', function(){
// For each ":checked" checkbox...
$('.checkbox INPUT:checked').each( function(){
// Get the parent row...
var $row = $(this).closest('tr');
// And that row's "editable" columns...
var $editable= $row.children('.editable');
// Add the "editing" class...
$editable.addClass('editing');
// And change all strings to INPUT fields.
$editable.each( function(){
$(this).html('<input value="'+$(this).text()+'"/>');
});
});
});
// Also...
// Any time an .editing INPUT receives a keypress...
$('table').on('keypress', '.editing', function(e){
// If the key pressed is ENTER...
if( e.which == 13 ){
// Get the current row...
var $row = $(this).closest('tr');
// Get the editable columns...
var $editable = $row.children('.editable');
// Remove the class...
$editable.removeClass('editing');
// Change back to string...
$editable.each( function(){
$(this).html( $(this).find('INPUT').val() );
});
}
});
});
I didnt really use your code, since you're writing html with php which is hard to maintain and isnt my preferred style (id rather write html and then put php inside) but this should get help you with your question: http://jsfiddle.net/swm53ran/28/.
comments in the code show what's going on. in essence, im using jquery to add the input with the data that is already in there. but since you are using php, you can just modify the code below by inserting <?php echo();?> inside the html code built in the javascript section.
<tr>
<td class="name">php echo name here</td>
<td class="description">php echo description here</td>
<td class="data">php echo data here</td>
<td>Edit Info</td>
</tr>
$('.edit').on('click', function() {
var row = $(this).closest('tr');
***NOT NECESSARY SECTION OF CODE BECAUSE YOU'RE USING PHP (JUST FOR DEMO)****
//collect data thats there (you dont have to do this if you have php,
//then you can just code in <?php echo();?> into your html building in
//javascript) because i have no php here
var name = row.find('.name').html();
var description = row.find('.description').html();
var data = row.find('.data').html();
***END OF NOT NECESSARY SECTION****
//construct inputs to be inserted into cells
//since you're using php, you modify the below code to something like this:
//var nameInput = '<input type="text" value="<?php echo('name');?>"/>';
//but you have to be careful because ^ this code wont work because of the
//nested parenthesis escape each other, so just put like <?php echo('name');?>
//into another variable and stick the variable in instead.
var nameInput = '<input type="text" value="'+ name +'"/>';
var descriptionInput = '<input type="text" value="'+ description +'"/>';
var dataInput = '<input type="text" value="'+ data +'"/>';
//remove all contents and append inputs
row.find('.name').html('').append(nameInput);
row.find('.description').html('').append(descriptionInput);
row.find('.data').html('').append(dataInput);
});
hope this helps!

JQuery .get not working on my page

I dont understand why this code doesnt work :
$( "#remove" ).click(function() {
var getcontent = $("#noticeid").val();
$.get("admincp.php", { removeid : getcontent } ,function(){
});
});
Heres the html textbox :
<td><input class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
And the button
<input type='button' id='remove' value='remove' class='btn btn-danger'>
And heres the PHP (on the same page):
if($_GET['removeid']){
$removeid = $_GET['removeid'];
$querydelete = "DELETE FROM `$dbtable`.`Notices` WHERE `NoticeID` = $removeid";
mysqli_query($conn,$querydelete);
}
This worked with just simple PHP and HTML so it must be something to do with the script. Sorry if its a really simple mistake but I cant seem to fix it.
Edit:
I was putting the input in a if clause that wasnt working so I fixed that but it still doesnt work, here is the new HTML code ($info7 = notice id ):
<input id='noticeid' class='form-control' style='position:absolute;left:500%;' type='text' name='myname' value='$info7'>
You are trying to get a dom element by ID, but only name is set in your html:
<td><input class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
You should add id, e.g.:
<td><input id='noticeid' class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
And then you will be able to successfully do:
var getcontent = $("#noticeid").val();
Try getting the value by name as you don't have an id in your input type
$( "#remove" ).click(function() {
var getcontent = $("input[name='noticeid']").val();
$.get("admincp.php", { removeid : getcontent } ,function(){
});
});
You are trying to retrieve
$("#noticeid").val();
But there is no element with this Id in the DOM. Please add id attribute.
<input class='form-control' id="noticeId" style='position:absolute;left:500%;' type='text' name="noticeid" value='$editid'>
If you don't want to add id then you can do like this
var getContent=$("input[name='noticeId']").val();
else
var getcontent=document.getElementByTagName("noticeId").value;
else
var getcontent=document.querySelector("input[name='noticeId']").value;

Select All Checkbox from MYSQL

Today I'm currently using "Checkbox" and just a little problem. I have a dropdown list in my first page, I choose the items "DropDown 1" and then I click the Submit button after that it will go to the next page. So then, the second page will load all items under the "DropDown 1" using a checkbox only.
My problem is:
How can I check them all by using a checkbox "Check All", where the items in "DropwDown 1" are came from my database [MySQL].
Here's my code in page two:
<input type="checkbox" name="all" id="all" /> <label for='all'>All</label>
<?php
$dropdown_value = (string)$_POST["id"];
echo "<br/>";
if ($dropdown_value == 'All Building')
{
$all = mysql_query("SELECT fldBldgName FROM tblbuildings");
while ($row = mysql_fetch_array($all))
{
echo "<tr><td>";
echo "<input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName'] . "'>";
echo $row['fldBldgName'];
echo "</td></tr><br/>";
}
}
?>
<script>
$(document).ready(function(){
$('input[name="all"]').bind('click', function(){
var status = $(this).is(':checked');
$('input[type="checkbox"]').attr('checked', status);
});
});
</script>
look checked Attribute
echo "<input type='checkbox' class='chk_boxes1' name='play[]' value='".$row['fldBldgName']. "' checked>";
and why a <br> in a table?
EDIT
try this:
echo '<div><input type="checkbox" class="checkall"> Check all</div>';
$all = mysql_query("SELECT fldBldgName FROM tblbuildings");
while ($row = mysql_fetch_array($all))
{
echo "<div><input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName']."'>";
echo $row['fldBldgName'];."</div>";
}
and jQuery function:
$(function () {
$('.checkall').on('click', function () {
$(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);
});
});
http://jsfiddle.net/H37cb/210/
Try listening to the change event on the .chk_boxes element. You should also use prop instead of attr when working with "binary" properties like `checked':
$('.chk_boxes').on('change', function(){
$('.chk_boxes1').prop('checked', $(this).prop('checked'));
});
http://api.jquery.com/prop/
You also need to wrap all your tr/td elements in a table element.:
<table>
<?php
...
?>
</table>
There's no need for br elements after a tr. tr will always start on a new line.

Categories