Reputation: 3628
I got a list where PHP creates an <input type="text" readonly value="' . $row[$item] . '">';
from mySQL. Sometimes a field in the database table is empty, so the input would just look like this: <input type="text" readonly value>';
So there is a value tag, but there isn't any value.
What I try to do is just to remove the empty inputs.
I tried using this code:
function clearEmpty(){
var input = $('.plannerlist input').val();
if(input < 0){
$('.plannerlist input').remove();
}
}
.plannerlist
is the following list:
<ul class="plannerlist" id="plannerlist1">
<input type="text" readonly value="Some value here">
<input type="text" readonly value>
<input type="text" readonly value="or no value like above">
<input type="text" readonly value>
</ul>
The function I wrote above is not working at all. No input gets removed, no console error, nothing... What could be the problem?
That's my PHP (i know it's a mess, but it works ;) ):
<?php
$host = "********";
$user = "********";
$pass = "********";
$db_name = "********";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
echo '<div id="content">
<form action="save.php" method="post">
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist1">';
//get results from database
$result = mysqli_query($connection,"SELECT FR_PM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist2">';
//get results from database
$result = mysqli_query($connection,"SELECT SA_AM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist3">';
//get results from database
$result = mysqli_query($connection,"SELECT SA_PM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist4">';
//get results from database
$result = mysqli_query($connection,"SELECT SO_AM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist5">';
//get results from database
$result = mysqli_query($connection,"SELECT SO_PM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist6">';
//get results from database
$result = mysqli_query($connection,"SELECT MO_AM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<input name="plannersubmit" id="plannersubmit" type="submit">
</form>
</div>';
?>
Upvotes: 0
Views: 80
Reputation: 193
//in your php foreach can you just do it like this?
foreach ($all_property as $item) {
if($row[$item] != NULL || $row[$item] != '')
{
echo '<input type="text" readonly value="' . $row[$item] . '">';
}
}
Upvotes: 1
Reputation: 467
$('.plannerlist input')
returns an array of inputs.
You need to loop over it and find the elements where val() == ''
.
var inputs = $('.plannerlist input');
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
if ($(input).val() == '') {
$(input).remove();
}
}
Upvotes: 1