How do I select the value of a clicked checkbox? - javascript

I have four checkboxes. Based on the checkbox that is checked (or unchecked) I need to addClass (or removeClass) a css class to any certain divs with class of filter-result AND containing a child div with a class that matches the value of the checkbox checked. (This is to show/hide search results, so checking/unchecking the boxes will show/hide various search results that have corresponding classes.)
I can write four jquery functions using ids that are almost identical except for the ids, or I can write one jquery function using variables. I'd prefer to write only one, for obvious reasons. I'm new to jQuery, JavaScript, PHP, etc., so I expect my error is super elementary here.
This is the HTML/PHP:
<!-- HTML for the checkboxes -->
<label>
<input type="checkbox" class="filterBox" value="devotion" id="toggleDevotion" checked/> Devotions
</label>
<label>
<input type="checkbox" class="filterBox" value="blog" id="toggleBlog" checked/> Blog Posts
</label>
<label>
<input type="checkbox" class="filterBox" value="product" id="toggleProduct" checked/> Products
</label>
<label>
<input type="checkbox" class="filterBox" value="program" id="toggleProgram" checked/> Programs
</label>
<!-- HTML/PHP for the wordpress search results -->
<div class="rowSectionInner results-container">
<?php if (have_posts() && strlen(trim(get_search_query())) != 0 ) { ?>
<?php while (have_posts()) { the_post(); ?>
<?php // Render a separation line. ?>
<?php if ($hasLoopedOnce) { ?>
<?php } $hasLoopedOnce = true; ?>
<?php // Render the search result. ?>
<div class="row justify-content-md-center filter-result">
<div class="col-md-7">
<article>
. . . . . . . . . . . . . . .
<div class="siteSearch_date previewDate">
<?php echo get_the_date(); ?> <?php echo rename_post_types(get_post_type()); ?>
</div>
. . . . . . . . . . . . . . .
</article>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
This is the jQuery I have written that works, but is super clunky:
$('#toggleDevotion').click(function() {
if( $(this).is(':checked')) {
$('.siteSearch_date:contains("Devotion")').closest('.filter-result').removeClass('remove');
} else {
$('.siteSearch_date:contains("Devotion")').closest('.filter-result').addClass('remove');
}
if ($(".results-container").children().length == $(".results-container").children(".remove").length) {
$(".no-results").css("display", "block");
$("#wrapper").css({"display": "flex", "flex-direction": "column"});
$("#container").css("flex-grow", "1");
} else {
$(".no-results").css("display", "none");
}
});
$('#toggleBlog').click(function() {
if( $(this).is(':checked')) {
$('.siteSearch_date:contains("Blog")').closest('.filter-result').removeClass('remove');
} else {
$('.siteSearch_date:contains("Blog")').closest('.filter-result').addClass('remove');
}
if ($(".results-container").children().length == $(".results-container").children(".remove").length) {
$(".no-results").css("display", "block");
} else {
$(".no-results").css("display", "none");
}
});
$('#toggleProduct').click(function() {
if( $(this).is(':checked')) {
$('.siteSearch_date:contains("Product")').closest('.filter-result').removeClass('remove');
} else {
$('.siteSearch_date:contains("Product")').closest('.filter-result').addClass('remove');
}
if ($(".results-container").children().length == $(".results-container").children(".remove").length) {
$(".no-results").css("display", "block");
} else {
$(".no-results").css("display", "none");
}
});
$('#toggleProgram').click(function() {
if( $(this).is(':checked')) {
$('.siteSearch_date:contains("Program")').closest('.filter-result').removeClass('remove');
} else {
$('.siteSearch_date:contains("Program")').closest('.filter-result').addClass('remove');
}
if ($(".results-container").children().length == $(".results-container").children(".remove").length) {
$(".no-results").css("display", "block");
} else {
$(".no-results").css("display", "none");
}
});
This is the jQuery I have written to try to combine the main show/hide toggle into one function:
$(".filterBox").click(function() {
var $lemon = $(this).val();
if( $(this).is(':checked')) {
$(".siteSearch_date:contains($lemon)").closest('.filter-result').removeClass('remove');
} else {
$(".siteSearch_date:contains($lemon)").closest('.filter-result').addClass('remove');
}
});
I expect it to apply or remove remove class from divs with class of .filter-result - but I get no response whatsoever from anything, including the console. What am I missing?

The correct syntax for the selector is:
$(".siteSearch_date:contains('"+$lemon+"')")
Edit---
The following snippet should simulate your code adding a bakground as example, is this what you wanted to achieve?
$(".filterBox").click(function() {
var $lemon = $(this).val();
if( $(this).is(':checked')) {
$(".siteSearch_date:contains('"+$lemon+"')").closest('.filter-result').removeClass('remove');
} else { $(".siteSearch_date:contains('"+$lemon+"')").closest('.filter-result').addClass('remove');
}
});
.remove{
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
<input type="checkbox" class="filterBox" value="devotion" id="toggleDevotion" checked/> Devotions
</label>
<label>
<input type="checkbox" class="filterBox" value="blog" id="toggleBlog" checked/> Blog Posts
</label>
<label>
<input type="checkbox" class="filterBox" value="product" id="toggleProduct" checked/> Products
</label>
<label>
<input type="checkbox" class="filterBox" value="program" id="toggleProgram" checked/> Programs
</label>
<div class="row justify-content-md-center filter-result">
<div class="col-md-7">
<article>
. . . . . . . . . . . . . . .
<div class="siteSearch_date previewDate">
product
</div>
. . . . . . . . . . . . . . .
</article>
</div>
</div>
<div class="row justify-content-md-center filter-result">
<div class="col-md-7">
<article>
. . . . . . . . . . . . . . .
<div class="siteSearch_date previewDate">
blog
</div>
. . . . . . . . . . . . . . .
</article>
</div>
</div>
<div class="row justify-content-md-center filter-result">
<div class="col-md-7">
<article>
. . . . . . . . . . . . . . .
<div class="siteSearch_date previewDate">
product
</div>
. . . . . . . . . . . . . . .
</article>
</div>
</div>
<div class="row justify-content-md-center filter-result">
<div class="col-md-7">
<article>
. . . . . . . . . . . . . . .
<div class="siteSearch_date previewDate">
program
</div>
. . . . . . . . . . . . . . .
</article>
</div>
</div>
<div class="row justify-content-md-center filter-result">
<div class="col-md-7">
<article>
. . . . . . . . . . . . . . .
<div class="siteSearch_date previewDate">
devotion
</div>
. . . . . . . . . . . . . . .
</article>
</div>
</div>

Related

Script for open image in new window not working

I'm using this code to open an image in a new window, but i can't get it to work. What is wrong?
echo "
<td align='center'>
<a href='images/spasergjengen/" . $row['Grad'] . "' target='_blank'>
<img src='images/spasergjengen/" . $row['Grad'] . "' width='125' height='150'
title='Spasergjengen' alt='Spasergjengen' />
</a>
<br>
<button onclick='myFunction" . $row['MedlemId'] . "'()'>Se Bilde</button>
<script>
function myFunction" . $row['MedlemId'] . "'() {
window.open('images/spasergjengen/" . $row['Grad'] . "', '_blank',
'toolbar=yes,scrollbars=yes,resizable=yes
,top=200,left=300,width=750,height=565'); }
</script>
</td>
";
[Tested]
<?php echo "<td align='center'><a href='images/spasergjengen/" . $row['Grad'] . "' target='_blank'><img src='images/spasergjengen/" . $row['Grad'] . "' width='125' height='150' title='Spasergjengen' alt='Spasergjengen' /></a><br><button onclick='myFunction" . $row['MedlemId'] . "()'>Se Bilde</button>
<script> function myFunction" . $row['MedlemId'] . "() { window.open('images/spasergjengen/" . $row['Grad'] . ",_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,top=200,left=300,width=750,height=565'); } </script></td>";
?>

display info according to the logged-in user by checking the employeeID

I am using active directory to authenticate the user.
Now I am using the below php to store the Employee_id of the user who has logged in :
if(isset($data[$i]["employeeid"][0]))
{
$user= $data[$i]["employeeid"][0];
session_start();
$_SESSION['id']=$user;
}
Now after authentication the user is taken to the profile .
In the profile I want to echo selected information based on the the user who has logged in.
So I am using the below php to select an user according to their employee_id and echo the info:
<div id="co-workers" class="leadboardcontent" style="display:none">
<div class="leaderboard">
<?php if ( $_SESSION['id'] == '1981')?> {
<ol>
<li>
<mark>
<?php while( $toprow77 = sqlsrv_fetch_array( $stmt7) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow77['overallRank'] . "</span><span class='name'>" . $toprow77['EmployeeName'] . "</span><span class='points'>" . $toprow77['Total_points_Rewarded'] . "</span></div>";
} ?>
</mark>
</li>
</ol>
} <?php if ( $_SESSION['id'] == '100739')?> {
<ol>
<li>
<mark>
<?php while( $toprow20 = sqlsrv_fetch_array( $stmt20) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow20['overallRank'] . "</span><span class='name'>" . $toprow20['EmployeeName'] . "</span><span class='points'>" . $toprow20['Total_points_Rewarded'] . "</span></div>";
}?>
</mark>
</li>
</ol>
} <?php if ( $_SESSION['id'] == '603')?> {
<ol>
<li>
<mark>
<?php while( $toprow19 = sqlsrv_fetch_array( $stmt19) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow19['overallRank'] . "</span><span class='name'>" . $toprow19['EmployeeName'] . "</span><span class='points'>" . $toprow19['Total_points_Rewarded'] . "</span></div>";
}?>
</mark>
</li>
</ol>
}
</div>
</div>
problem : when I run the above PHP it echoes for all the three Employee_ids.It is not entering the if else statements and selecting the employee_ID and displaying info of only the user who has logged in.
The logged in user's ID is saved in the first PHP snippet I have mentioned above.
I tried using - if ,elseif ,else also.
I have got a solution using switch statements,which works perfect :
<div class="leaderboard">
<?php switch ($_SESSION['id']) {
case "1981":?>
<ol>
<li>
<mark>
<?php while( $toprow77 = sqlsrv_fetch_array( $stmt7) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow77['overallRank'] . "</span><span class='name'>" . $toprow77['EmployeeName'] . "</span><span class='points'>" . $toprow77['Total_points_Rewarded'] . "</span></div>";
} ?>
</mark>
</li>
</ol>
<?php break;
case "100739" : ?>
<ol>
<li>
<mark>
<?php while( $toprow20 = sqlsrv_fetch_array( $stmt20) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow20['overallRank'] . "</span><span class='name'>" . $toprow20['EmployeeName'] . "</span><span class='points'>" . $toprow20['Total_points_Rewarded'] . "</span></div>";
}?>
</mark>
</li>
</ol>
<?php
break;
}
?>
</div>

making an accordion toggle that works with php

how can i make a simple accordian toggle like in https://www.wireshark.org/download.html that works like getting data from a database i have,which has questions and answers? that works simply for all the rows.
this is the current code i have and want to modify.
<?php
$connection = ($GLOBALS["___mysqli_ston"] = mysqli_connect('localhost', 'root', ''));
((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . 'db'));
$query = "SELECT * FROM AS_Questions";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);
if (!$result) {
printf("Errormessage: %s\n", $mysqli->error);
}
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<div class='span3 tiny'>
<div class='pricing-table-header-tiny'>
<h2>" . $row['Question'] . "</h2>
</div>
<a href='##' id='s'>Show answer</a>
<div class='dq'>
<div class='pricing-table-features'>
<p>" . $row['Answer'] . "</p>
</div>
<div class='Dass'>
<p id='Dassp'>Answered by:" . $row['Doctor'] . "<p>
</div>
</div>
</div>"; //$row['index'] index here is a field name
}
echo "</table>"; //Close the table in HTML
((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
?>

video preroll vimeo in wordpress

Hello guys I would like to integrate an open source code taken from github to my wordpress theme.
link:
Vimeo Pre-roll
This is the code of my video.php, I would like to work with vimeo.
code:
<?php
get_header();
wize_set_views(get_the_ID());
echo '
<div id="wrap" class="fixed">';
if (have_posts())
while (have_posts()):
the_post();
$social = of_get_option('social_sng', '1') == '1';
$venue = get_post_meta($post->ID, 'vd_venue', true);
$youtube = get_post_meta($post->ID, 'vd_youtube', true);
$vimeo = get_post_meta($post->ID, 'vd_vimeo', true);
$date = get_post_meta($post->ID, 'vd_date', true);
$time = strtotime($date);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('d', $time);
/* display */
echo '
<div id="mediasng">
<div class="mediasng-title">
<h1>' . get_the_title() . '</h1>
</div><!-- end .mediasng-title -->
<div class="mediasng-lv">
' . wize_like_info($post->ID) . '
<div class="info-view">' . wize_get_views(get_the_ID()) . '</div>
</div><!-- end .mediasng-lv -->
<div class="mediasng-info">';
if ($venue) {
echo '
<div class="mediasng-venue">' . esc_html($venue, "wizedesign") . '</div>';
}
if ($date) {
echo '
<div class="mediasng-date">' . esc_html($day, "wizedesign") . ' ' . esc_html($month, "wizedesign") . ' ' . esc_html($year, "wizedesign") . '</div>';
}
echo '
</div><!-- end .mediasng-info -->';
if ($youtube) {
echo '
<iframe src="https://www.youtube.com/embed/' . esc_attr($youtube) . '" width="1130" height="620" frameborder="0" allowfullscreen></iframe>';
} elseif ($vimeo) {
echo '
<iframe src="http://player.vimeo.com/video/' . esc_attr($vimeo) . '" width="1130" height="620" frameborder="0" allowFullScreen></iframe>';
}
if ($social) {
echo '
<div class="mediasng-social">
<span>' . esc_html__("share", "wizedesign") . '</span>
<div class="sng-facebook"></div>
<div class="sng-twitter"></div>
<div class="sng-google"></div>
<div class="sng-linkedin"></div>
</div><!-- end .mediasng-social -->';
}
echo '
</div><!-- end .mediasng -->
';
endwhile;
get_footer();
how can I make sure that the code is read by wordpress plugins github!?

Sending form data to page nested in div

This is my third attempt to find an answer to this question, every other time I was downvoted for one reason or another. So I try this again. I am attempting to send data from a hidden input within a form via ajax. The hidden input gets its value from a php script. Now I can not seem to pull the hidden input value on the receiving page. Now the form I am sending from is generated and propagated within php as is the ajax that fires to send the form to the other page.
When I attempt to call the info from the form on the receiving page it does not seem to receive the data from the first page. The reason I assume this is I get no errors and it will not display the data but it does fire the echo that is located before the fetch array.
Here is the code for the first page. It works in all aspects with what I am trying to do except for the form sending portion. I am leaving a lot out but the ajax and form portions are in there.
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM recipes";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo"
<script>
$(document).ready(function() {//start document ready
$('#" . $row['id'] ."').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/pages/receivingpage.php',
data: $(\"f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"" . $row['id'] ."\" class=\"front\">
<form id=\"" . $row['recipe'] ."\" name=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>
" . $row['recipe'] ."<br></h2>
<img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
<p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
<p>Numer of Calories: " . $row['calories'] ."</p>
</div>
</div>
</div>
</div>
</div>
</div>";
}
mysqli_close($con);
?>
Here is the receiving page. It loads but only displays the echo. If I remove the WHERE within the SELECT statement it displays all the database results(not what is desired).
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$r = $_POST["f2"]['recipe'];
$sql = "SELECT * FROM recipes WHERE recipe ='".$r."'";
$result = mysqli_query($con,$sql);
echo " 2 ";
while ($row = mysqli_fetch_array($result)) {
echo " " . $row['recipe'] ." ";
}
mysqli_close($con);
?>
Any help will be much appreciated.
Try posting serialized data using id instead of the name of the form. See below the example code.
data: $(\"#f2\").serialize(),
Hope this will help you.
See below updated working code.
UPDATED ANSWER:
page1.php
</script>
<?php
$rows[0]['id'] = 1;
$rows[0]['recipe'] = "Veg Rec";
$rows[0]['cooking'] = "Hot cooking";
$rows[0]['calories'] = 1000;
$rows[0]['image'] = "image.png";
foreach ($rows as $key => $row) {
# code...
echo"
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"" . $row['id'] ."\" class=\"front\">
<form id2=\"" . $row['recipe'] ."\" id=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>
" . $row['recipe'] ."<br></h2>
<img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
<p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
<p>Numer of Calories: " . $row['calories'] ."</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {//start document ready
$('#" . $row['id'] ."').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'page2.php',
data: $(\"#f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
";
}
?>
page2.php
<?php
print_r($_POST);
?>

Categories