Selection option in php loop - php

I have the code below and this code works, but only after clicking on selection option but code doesn't work when i change value when use arrow up and down.
I tried modification script, change option "click" to "change" but this solution doesn't work. Someone can help me ?
$select = $db_connect -> query("SELECT * FROM templates");
if($select -> num_rows > 0)
{
echo '<select id="subject" name="subject" class="form-select">';
while($row = $select -> fetch_assoc())
{
echo '<option id="'.$row["id"].'" value="'.$row['template_subject'].'">'.$row['template_subject'].'</option>';
?>
<script>
$("#subject #<?php echo $row['id']; ?>").on('click', function(){
if((this.id = "<?php echo $row['id'];?>") && (this.id != 1))
{
$("textarea").html("<?php echo $row['template_text']; ?>");
$("input[name='new_subject']").hide();
}
else
{
$("textarea").html("");
$("input[name='new_subject']").show();
}
});
</script>
<?php
}
echo '</select>';
}

Your problem is in the Javascript code.
<script>
$("#subject").change( function(){
var text = $( "#subject option:selected").val();
var id = $(this).children(":selected").attr("id");
if(id != 1)
{
$("textarea").html(text);
$("input[name='new_subject']").hide();
}
else
{
$("textarea").html("");
$("input[name='new_subject']").show();
}
});
</script>
remove the script from the while loop , put it at the end before </body> tag.

Related

Dropdown Options Based on Previous Selection

I'm hoping this is a simple solution. I am trying to make the first drop down determine the options available for the second. In my database, each flavor for the drink type has "type_id" column set as an integer (i.e. 1,2,3). The integers are meant to reflect the category in which they belong. Is it possible/make sense to base the available options for the second drop down off of the "type_id" that I determined?
I was hoping to accomplish this by using PHP, but I am not opposed to jQuery. I am not very well versed in one over the other. Thank you for your help in advance!
<?php
require "db-connect.php";
$dtype = "SELECT name FROM drinktype";
$typedata = mysqli_query($connection, $dtype);
echo "<select id='slctType'>";
if (mysqli_num_rows($typedata) > 0) {
while($row = mysqli_fetch_assoc($typedata)) {
echo "<option value='{".$row['name']."}'>".$row['name']."</option>";
}
}
echo "</select>";
$dflavor = "SELECT type_id,name FROM drinkflavor";
$flavordata = mysqli_query($connection, $dflavor);
echo "<select id='slctFlavor' ";
if (mysqli_num_rows($flavordata) > 0) {
while($row = mysqli_fetch_assoc($flavordata)) {
echo "<option value='{".$row['name']."}'>".$row['name']."</option>";
}
}
echo "</select>";
mysqli_close($connection);
?>
I have sample code for fetching the city according to their state. I have do that code with ajax, jquery and php. I think you have similar type of requirement. Please try below code concept for your requirement.
$(document).on('change','#state',function () {
$('#city').remove();
if($(this).val() != 'none')
{
var state_id = $(this).val();
$.ajax({
type: 'POST',
url: 'page.php',
data: { state_id: state_id },
success: function (city_response) {
if(city_response == '')
{
return false;
}
else
{
var city = '<select name="city" id="city" class="form-control">';
city += '<option value="-----">Please select any City</option>';
$.each(city_response, function (ck, cv) {
city += '<option value="' + cv['city_id'] + '">' + cv['city_name'] + '</option>';
});
city += '</select>';
$("#city_div").css('display', 'block');
$('#cities').append(city);
}
}
})
}
else
{
$("#city_div").css('display', 'none');
}
});

Load the next 10 record via Ajax Search Box using PHP MySQL and JQUERY

I have see the example about search box using JQuery and mysql, But the view more function no work. how to improve the program. When i click the view more i can see the next 10 record. Thanks
<script type="text/javascript">
$(document).ready(function()
{
$("#keywords").keyup(function()
{
var kw = $("#keywords").val();
if(kw != '')
{
$.ajax
({
type: "POST",
url: "search.php",
data: "kw="+ kw,
success: function(option)
{
$("#results").html(option);
}
});
}
else
{
$("#results").html("");
}
return false;
});
$(".overlay").click(function()
{
$(".overlay").css('display','none');
$("#results").css('display','none');
});
$("#keywords").focus(function()
{
$(".overlay").css('display','block');
$("#results").css('display','block');
});
});
</script>
<div id="inputbox">
<input type="text" id="keywords" name="keywords" value="" placeholder="Type Your Query..."/>
</div>
</div>
<div id="results"></div>
<div class="overlay"></div>
we extract the value of that key and send it to the search.php
<?php
include('db.php');
//file which contains the database details.
?>
<?php
if(isset($_POST['kw']) && $_POST['kw'] != '')
{
$kws = $_POST['kw'];
$kws = mysql_real_escape_string($kws);
$query = "select * from wp_posts where post_name like '%".$kws."%' and (post_type='post' and post_status='publish') limit 10" ;
$res = mysql_query($query);
$count = mysql_num_rows($res);
$i = 0;
if($count > 0)
{
echo "<ul>";
while($row = mysql_fetch_array($res))
{
echo "<a href='$row[guid]'><li>";
echo "<div id='rest'>";
echo $row['post_name'];
echo "<br />";
echo "<div id='auth_dat'>".$row['post_date']."</div>";
echo "</div>";
echo "<div style='clear:both;'></div></li></a>";
$i++;
if($i == 5) break;
}
echo "</ul>";
if($count > 5)
{
echo "<div id='view_more'><a href='#'>View more results</a></div>";
}
}
else
{
echo "<div id='no_result'>No result found !</div>";
}
}
?>
press the view more result will not show more result.
If I'm not mistaken, you want to bring next 10 with ajax ?
This situation behaves as a pagination,
You have to store the current click count in javascript . Wİthout clicking more button, the variable of clickCount is 0, when you click more ,then your variable clickCount=1 ,
while sending ajax , send clickCount to the php.
$.ajax
({
type: "POST",
url: "search.php",
data: "kw="+ kw+"&clickCount="+clickCount,
success: function(option)
{
$("#results").html(option);
}
});
You can query with limit&offset (clickCount )*10, itemCountForEachMoreClick = limit 0,10
when click more
limit 10,10
when click one more
limit 20,10. Do not forget to reset clickCount on keyPress !
php Side
$count = isset($_REQUEST["clickCount"])? $_REQUEST["clickCount"]:0;
$limitAndOffset = $count*10.",10";
$query = "select * from wp_posts where post_name like '%".$kws."%'
and (post_type='post' and post_status='publish') limit ".$limitAndOffset ;

Open new window for multiple edit records

What I have here is a table that displays record from my database. It also generates dynamic checkboxes along with the fetched record. What I need to do is to open a new window using window.open, instead for the page to go to editor.php. Any help will be gladly appreciated.
MySQLi Query
while ($row = $result1->fetch_assoc()) {
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' style="cursor:pointer;" class="checkbox"></td>
}
Javascript
<script type="text/javascript" language="javascript">
function setCheckboxes3(act) {
$('.checkbox').not(':disabled').prop('checked', act == 1 ? true : false);
//or $('.checkbox:not(:disabled)').prop('checked', act == 1 ? true : false)
}
</script>
<script>
jQuery(function ($) {
$('a.button.edit, a.button.remove').click(function () {
if ($('input[name="checkbox[]"]:checked').length == 0) {
return;
}
if ($(this).hasClass('edit')) {
if (!confirm('Edit this Record(s)?')) {
return
}
} else {
if (!confirm('WARNING !!! All Purchase Order that included in this Record(s) will be deleted.')) {
return
}
}
var frm = document.myform;
if ($(this).hasClass('edit')) {
frm.action = 'editpr.php';
}
if ($(this).hasClass('remove')) {
if (!confirm('Are you sure want to continue?')) {
return
}
}
frm.submit();
})
})
</script>
HTML
<a class="button edit" style="cursor:pointer;"><span><b>Edit</b></span></a>

php jquery selecting the specific checkbox inside the while fetch_array

I have a problem selecting specific checkbox and putting higlight css for its div inside the mysql_fetch_array
here's my code
$count=1;
$query = mysql_query('SELECT * FROM thread');
while($row = mysql_fetch_array($query))
{
echo "<div class='row".$count."'><input type='checkebox' class='chk_box".$count."'> ".$row['title']."</div>";
$count++;
}
<script>
var count= "<?php echo $count?>";
for(var y=1;y<=count;y++){
$('.chk_box'+y).click(function() {
if(this.checked) {
$('.row'+y).addClass('backcolor');
}
else{
$('.row'+y).removeClass('backcolor');
}
});
}
</script>
Fixed som typos, added a DOM ready function, and changed the jQuery a bit, try that ?
$count=1;
$query = mysql_query('SELECT * FROM thread');
while($row = mysql_fetch_array($query)) {
echo "<div class='row".$count."'><input type='checkbox' class='chk_box".$count."'> ".$row['title']."</div>";
$count++;
}
<script type="text/javascript">
$(function() {
$('[class^="chk_box"]').on('click', function() {
$(this).closest('div').toggleClass('backcolor', this.checked);
});
});
</script>​​​​​​​​​​
as a sidenote, attaching event handlers inside for loops is usually not a very good idea.

mysql_fetch_array's div onmouseover only works for first result

I am new on this website and I hope you'll answer soon.
I want the Remove icon to show up when users hover on their selected friend, but it only appears on mysql_fetch_array's first result. doesn't work for others. can you help? I've heard that it's being done with foreach function but don't know how to use it, because I'm new at php.
Here are the codes.
PHP:
$q = mysql_query("SELECT * FROM `users`");
while ($row = mysql_fetch_array($q)) {
echo '<div id="friends" onmouseover="showRemove();" onmouseout="hideRemove();">';
echo '<div id="fillexit"></div>';
echo '<div id="showexit"></div>';
echo '</div>';
}
Javascript:
function showRemove() {
var l=document.getElementById("showexit");
var s=document.getElementById("fillexit");
l.style.display = 'block';
s.style.display = 'none';
}
function hideRemove() {
var p=document.getElementById("showexit");
var o=document.getElementById("fillexit");
p.style.display = 'none';
o.style.display = 'block';
}
CSS:
#fillexit {
width:12px;
height:12px;
float:right;
}
#showexit {
width:12px;
height:12px;
background-color:#000000;
float:right;
text-align:center;
display:none;
}
I'd really appriciate your help.
It is because the html echoed from php script will have same ids for all row elements.
You should create a unique id using counter or something and change your js accordingly. May be you can pass the id to the js method to help find the elements on the page.
PHP
$q = mysql_query("SELECT * FROM `users`");
$count = 0;
while ($row = mysql_fetch_array($q)) {
echo '<div id="friends'.$count.'" onmouseover="showRemove('.$count.');" onmouseout="hideRemove('.$count.');">';
echo '<div id="fillexit'.$count.'"></div>';
echo '<div id="showexit'.$count.'"></div>';
echo '</div>';
$count = $count + 1;
}
JS
function showRemove(id) {
var l=document.getElementById("showexit" + id);
var s=document.getElementById("fillexit" + id);
l.style.display = 'block';
s.style.display = 'none';
}
function hideRemove() {
var p=document.getElementById("showexit" + id);
var o=document.getElementById("fillexit" + id);
p.style.display = 'none';
o.style.display = 'block';
}
if($q ->num_rows()>0){
foreach ($q ->result()as $row){
echo '<div id="friends" onmouseover="showRemove();" onmouseout="hideRemove();">';
echo '<div id="fillexit"></div>';
echo '<div id="showexit"></div>';
echo '</div>';
}
}

Categories