I have a simple PHP while loop which executes a list of tabular comments/posts from the SQL database. Each post has a reply hyperlink that that will open a textarea (form) below the post which will allow the user to reply to that specific post. It works fine for one post, but when there are multiple posts the jQuery opens all of the textareas for each post. I assume that this is an issue with me calling identical classes using jQuery. But I am not sure how to deal with it. What I would like to achieve is allowing the user to click on a reply hyperlink within a specific post and only that textarea will appear underneath and not all of them at the same time.
Here is the PHP code to generate the list of posts with the reply feature.
<?php>
echo "<table width=\"100%\" border=\"0\" cellspacing=\"10\">";
while ($row2 = mysql_fetch_assoc($res))
{
echo "
<tr>
<td>
<span class=\"anonname\">Anon" . $row2['uid'] . " </span>
<span class=\"smalltext\"> says:</span>
</td>
</tr>
<tr>
<td>
<span class=\"messagetext\">" . $row2['msg'] . "</span></td>
</tr>
<tr>
<td>
<div class=\"replystyle\"><span class=\"replytext\"><a href=\"#\"
class=\"replybtn\">Reply</a> ⋅ Like </span> <span
class=\"replytext\" style=\"float:right;\"><span class=\"timetext\">" .
$row2['timestamp'] . "</span>Report</span></div>
</td>
</tr>
<tr>
<td>
<div id=\"replymsgbox\">
<form id=\"messaging\" action=\"\" method=\"post\" accept-charset=\"UTF-8\">
<div class=\"tarea\">
<textarea name=\"message\" rows=\"2\" class=\"txtbx\"></textarea>
</div>
<span style=\"float:right;\"><input name=\"submit\" class=\"signupbtn\"
type=\"submit\" value=\"share\" /></span><br/><br/>
</form>
</div>
</td>
</tr>";
}
echo "</table>";
}
?>
Here is the jQuery
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$('.replymsgbox').hide();
$('.replybtn').click(function(){
$('.replymsgbox').slideToggle('fast');
});
});
</script>
Any help would be appreciated.
With $('.replymsgbox') selector, you are getting all the element having such class.
To get just the one related to the button, use:
$('.replybtn').click(function(){
$('.replymsgbox', $(this).parent().parent().next()).slideToggle('fast');
});
You are narrowing research of a .replymsgbox element inside next <tr> this way.
(This isn't an answer, just an observation; comments don't allow such formatting)
This is what I wrote about in my comment to your question:
<table width="100%" border="0" cellspacing="10">
<?php
while ($row2 = mysql_fetch_assoc($res)) {
?>
<tr>
<td>
<span class="anonname">Anon<?php echo $row2['uid'];?></span>
<span class="smalltext"> says:</span>
</td>
</tr>
<tr>
<td>
<span class="messagetext"><?php echo $row2['msg']; ?></span></td>
</tr>
<tr>
<td>
<div class="replystyle"><span class="replytext"><a href="#"
class="replybtn">Reply</a> ⋅ Like </span>
<span class="replytext" style="float:right;"><span
class="timetext"><?php echo $row2['timestamp']; ?></span>Report</span></div>
</td>
</tr>
<tr>
<td>
<div id="replymsgbox">
<form id="messaging" action="" method="post" accept-charset="UTF-8">
<div class="tarea">
<textarea name="message"rows="2" class="txtbx"></textarea>
</div>
<span style="float:right;"><input name="submit" class="signupbtn"
type="submit" value="share" /></span><br/><br/>
</form>
</div>
</td>
</tr>
<?php } ?>
</table>
See how that cleans up much better? Now if you want to make changes to your HTML, you don't have to through all the complex escaping machinations.
Related
On the first page there is a form with a jquery script to add days.
If I just use one day I cant get all the info I need.
But when I add multiple days I just get the last entry.
I am trying to see every day of the event I book in the table on the next page book_gear.php.
But so far I am only see one day of the event .
I think it is because my foreach loop is going thought the array indexs rather than each event in the array.
book_avail.php
<?php require_once('../Include/initialize.php'); ?>
<!doctype html>
<html>
<!--Sets page title in browser-->
<?php $page_title = 'Schedule'; ?>
<!-- Imports header file with necessary scripts -->
<?php include ('../Include/header.php'); ?>
<!-- Imports Top navigation bar -->
<?php include ('../Include/navibar.php'); ?>
<!-- Imports javascript file to allow arrange by column -->
<?php include_once ('../Include/sort_by.php'); ?>
<?php include_once ('../Include/mysql_queries.php');
//loads all classes
include_once ('../Include/class-autoload.inc.php'); ?>
<script>
$(document).ready(function() {
var i = 1;
$("#add_row").click(function() {
$('tr').find('input').prop('disabled',true)
$('#addr' + i).html("<td>" + (i + 1) +
"</td><td><input type='text' value='Day" + (i + 1) +"' name='event[" + i + "][eventNameInitialQuery]' class='form-control input-md'/></td>\n\
<td><input type='date' name='event[" + i + "][dateFromIntialQuery]' placeholder='date' class='form-control' /></td>\n\
<td><input type='time' name='event[" + i + "][timeFromIntialQuery]' placeholder='time' class='form-control' /></td>\n\
<td><input type='date' name='event[" + i + "][dateToIntialQuery]' placeholder='date' class='form-control' />\n\</td>\n\
<td><input type='time' name='event[" + i + "][timeToIntialQuery]' placeholder='time' class='form-control' />\n\</td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
i++;
});
});
</script>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<?php
//sets user location or builing
if (isset($_SESSION['location'])) {
$userLocation = $_SESSION['location']?? '';}
$build_set = build_dropdown($userLocation);
?>
<!--Main Id is the bulk of the page-->
<div id="main">
<!--Sets side navigation bar-->
<div class="sidenav">
<div>
<!--Includes side navigation script -->
<?php include_once ('../Include/side_navi_login.php'); ?>
</div>
</div>
<br><br>
<body>
<!--Sets Title "Schedule Event"-->
<h1 class="book_event_title">Schedule Event</h1>
<!--This table is the form that runs along the top of the page-->
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form action="book_gear.php?" method="post">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr>
<th class="text-center">
Day
</th>
<th class="text-center">
Event Name
</th>
<th class="text-center">
Date From
</th>
<th class="text-center">
Time From
</th>
<th class="text-center">
Date To
</th>
<th class="text-center">
Time To
</th>
<th class="text-center">
Building
</th>
<th class="text-center">
VP Present
</th>
<th class="text-center">
Ticket
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='event[0][eventNameInitialQuery]' placeholder='Event Name' class="form-control" />
</td>
<td>
<input type="date" name='event[0][dateFromIntialQuery]' placeholder='date' class="form-control" />
</td>
<td>
<input type="time" name='event[0][timeFromIntialQuery]' placeholder='time' class="form-control" />
</td>
<td>
<input type="date" name='event[0][dateToIntialQuery]' placeholder='date' class="form-control" />
</td>
<td>
<input type="time" name='event[0][timeToIntialQuery]' placeholder='time' class="form-control" />
</td>
<!-- Gets the dropdown menu from a method in the site view class -->
<td>
<select name="buildingCodeIntialQuery" id="site_value" required>
<option value="" disabled selected>-Building-</option>
<?php
$buildList = new SiteView;
$buildList->showMyBuildings($userLocation) ;
?>
</select>
</td>
<td>
<input type="hidden" name="vpInitialQuery" value="No" class="form-control"/>
<input type="checkbox" name="vpInitialQuery" value="Yes" class="form-control"/>
</td>
<td>
<input type="text" name='ticketInitialQuery' placeholder='Link to Event Ticket' class="form-control" />
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<div align="right" id="schedule_button">
<input type="submit" value="Schedule Event"/>
</div>
</form>
</div>
</div>
<button id="add_row" class="btn btn-primary btn-lg pull-left">Add Day</button>
</div>
</div>
</body>
<!--Includes bottom footer-->
<?php include('../Include/footer.php'); ?>
</html>
book_gear.php
<?php require_once('../Include/initialize.php'); ?>
<!doctype html>
<html>
<!--Sets page title in browser-->
<?php $page_title = 'Schedule'; ?>
<!-- Imports header file with necessary scripts -->
<?php include ('../Include/header.php'); ?>
<!-- Imports Top navigation bar -->
<!-- Imports javascript file to allow arrange by column -->
<?php include_once ('../Include/sort_by.php');
include_once ('../Include/mysql_queries.php'); ?>
<?php
//once the person is logged in their site is stored in this variable
if(isset($_SESSION['logins'])) {
$users_site = $_SESSION['location'];
}
if(isset($_POST['event'])) {
$events = $_POST['event'];
}
foreach($events as $unique =>$event){
?>
<table>
<tr>
<th>event name</th>
<th>datefrom</th>
<th>timefrom</th>
<th>dateto</th>
<th>timeto</th>
</tr>
<td><?php echo $event['eventNameInitialQuery'];?></td>
<td><?php echo $event['dateFromIntialQuery'];?></td>
<td><?php echo $event['timeFromIntialQuery'];?></td>
<td><?php echo $event['dateToIntialQuery'];?></td>
<td><?php echo $event['timeToIntialQuery'];?></td>
</table><?php ;}?>
EDIT
After suggested in the comments ran
echo '<pre>'; var_dump($events);echo '</pre>';die(); before the foreach loop
Result
array(1) {
[1]=>
array(5) {
["eventNameInitialQuery"]=>
string(4) "Day2"
["dateFromIntialQuery"]=>
string(10) "2020-07-23"
["timeFromIntialQuery"]=>
string(5) "18:57"
["dateToIntialQuery"]=>
string(10) "2020-07-11"
["timeToIntialQuery"]=>
string(5) "19:54"
}
}
I've done a quick test and the problem is with disabling the earlier inputs when adding a new event row. What you could do is copy the values of your inputs to hidden inputs before you disable them.
I have created a page for search, and I'm having some problems.
The data is displayed on the page but I also use some fields on the form for a specific field, so when I enter the data in those fields and hit search, it shows the search field on the top of the display page with the rest of the data, but I want only the data which belongs to my search.
CODE:
<?php
include('connect.php');
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include('include/head.php')?>
</head>
<body>
<div id="header-wrapper">
<div id="header">
<div id="menu">
<ul>
<?php include('include/menu.php') ?>
Logout
</ul>
</div>
</div>
</div>
<div id="logo">
<?php include('include/logo.php'); ?>
</div>
<div id="wrapper">
<div id="page">
<div id="page-bgtop">
<div id="content">
<div class="post">
<div class="entry-image">
<?php
$q=mysql_query("SELECT count( php ) AS ph, count( dotnet ) AS dot, count( design ) AS des FROM candidate");
while($row=mysql_fetch_array($q))
{
?>
<form name="" method="post">
<table border="1" cellpadding="10" cellspacing="1" width="280" height="200">
<th colspan="5" bgcolor="orange" >Total Number of Resume by skills</th>
<tr>
<th><B>PHP</B></th>
<th><B>DOTNET</B></th>
<th><B>DESIGN</B></th>
</tr>
<td><?php echo "$row[ph]"; ?></td>
<td><?php echo "$row[dot]"; ?></td>
<td><?php echo "$row[des]"; ?></td>
</table>
</form>
<?php
}
?>
</div>
<h2 class="title">Welcome to Resume </h2>
<div class="entry">
<p>
<form name="search" method="post" action="searchview.php">
<table>
<tr>
Id: <input type="text" name="id" /></br></br>
Name:<input type="text" name="name" /><br/></br>
Expected Salary:<input type="text" name="es" /><br/></br>
Skill Rate: <input type="text" name="sr" /><br/> </br>
Salary:<input type="text" name="cs" /></br></br>
<input type="submit" name="submit" value="seach" /><input type="reset" name="reset" value="reset" />
</tr>
</table>
</form>
</p>
</div>
</div>
<div class="post" >
<?php
$query=mysql_query("select * from candidate");
while($row=mysql_fetch_array($query))
{
$sno=$row['id'];
$image=$row['image'];
$name=$row['fname'];
$es=$row['es'];
$sr=$row['sr'];
$cs=$row['cs'];
?>
<form name="view" method="post" action="searchview.php" >
<table border="1" cellpadding="30" class="centered" width="20" >
<tr>
<th width="150">S.NO</th>
<th width="150">IMAGE</th>
<th width="150">NAME</th>
<th width="150">EXPECTED SALARY</th>
<th width="150">SKILL RATE</th>
<th width="150">SALARY</th>
<th width="150">ACTION</th>
</tr>
<td><?php echo "$row[id]"; ?></td>
<td><div class="rightdiv1a"><img src="<?php echo 'img/'.$row['image']; ?>" style="height:120px; width:145px; "></div></td>
<td><?php echo "$row[fname]"; ?></td>
<td><?php echo "$row[es]"; ?></td>
<td><?php echo "$row[sr]"; ?></td>
<td><?php echo "$row[cs]"; ?></td>
<td><img alt='view' title='view' src='images/view.png' width='15px' height='15px' hspace='20' /></td>
<td><img alt='Edit' title='Edit' src='images/edit.png' width='15px' height='15px' hspace='20' /></td>
<td><a onclick="return confirm('Would you like to delete?');" href="delete.php?id=<?php echo "$row[id]"; ?>"><img alt='delete' title='delete' src='images/delete.png' width='15px' height='15px' hspace='20' /></a></td>
</table>
</form>
<?php
}?>
</div>
</div>
</div>
</div>
</div>
<div id="footer-bgcontent">
<div id="footer">
<?php// include('include/footer.php');?>
</div>
</div>
</body>
</html>
<?php// } ?>
This code displays the data now but I want to filter the data and only show the results which belongs to my search, and have the rest disappear.
Here are some screenshots of my form:
pls help,
thanks in advance..
If I understand the question correctly, you want to display the search results, but no longer show the search form. If this is the case, then move your second "post" div to the top and wrap it in an if:
if (!empty($_POST)) {
// Process POST results here:
...
die(); // end processing so the form doesn't display again
}
I am trying to make multiple buttons in PHP. I am new to html and PHP. I am unable to saw the problem in the code. Its not giving error but do nothing when I click on the button.
Here is the piece of my code
<html>
<head></head>
<body>
<table>
<tbody>
<tr>
</tr>
</tbody>
</table>
<table border="5" cellpadding="0" cellspacing="0" margin-bottom="1px" width="100%" style="color:navy">
<tbody>
<tr>
<td> <b> Teacher's Name </b> </td>
<td> <b> Courses </b> </td>
<td> <b> Feedback </b> </td>
</tr>
<!--?php while ($row = mysql_fetch_row($subjects)) {
$Teacher_Names = mysql_query("SELECT Name FROM teachers where Teacher_ID = $row[1]");
$Name = mysql_fetch_row($Teacher_Names)?-->
<tr>
<td>
<!--?php echo $Name[0];?--></td>
<td>
<!--?php echo $row[0]; ?--> </td>
<td>
<form method="POST" action="CourseFeedbackForm.php">
<img type="button" src="books.jpg" height="30" width="30" id="<?php $button.$count?>" name="submit" value="<?php $row[1]?>">
<!--?php $count = $count + 1; ?-->
<img src="horizontal1.jpg" height="30" style="padding-left:0px">
<img type="button" src="teachers.jpg" height="30" width="30" id="<?php $button.$count?>" name="submit" value="<?php $row[1]?>">
<!--?php $count = $count + 1; ?-->
</form></td>
</tr>
<!--?php } ?-->
</tbody>
</table>
</body>
</html>
It kind of took me a while to understand what you are doing there, but not I still did not understand everything. Why are you calling <img type="button">? In order to get a button, you know, that thing that you can click and it executes a piece of code, use <button type="submit"></button>. Almost everything you've done there is wrong. I'm not an expert, but I really can't see the reason you would try to use the <img> tag to create a button.
Offtopic: Learn to use <div></div>. It is much more flexible than a table, and table's are old and outdated.
How do I create a jQuery hover to show a div when the mouse is over like twitter repla retweet and favorite?
Here is my HTML:
<div style="display:none;" id="blab">
<?php echo $blab_id; ?>
</div>
<a href="blab.php?id=<?php echo $blab_id; ?>">
<div class="blab_body" id="hover" value="Hide">
<table>
<tr>
<td valign="top">
<img src="<?php echo $profile_pic_info; ?>" class="blab_image" width="50" height="50" />
</td>
<td> </td>
<td>
<table>
<tr>
<td valign="top">
<a href="profile.php?id=$mem_id">
<strong><?php echo $added_fullname; ?></strong>
</a>
<?php echo $added_to_fullname; ?>
</td>
</tr>
<tr>
<td>
<?php echo $blab_body; ?>
</td>
</tr>
<tr>
<td>
<text style="color:gray;">
<?php echo $blab_date; ?>|<?php echo $device; ?>
</text>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div style="background:#000; display:none;" id="id"> </div>
</div>
</a>
<hr />
and here is my JavaScript code:
$('#id').hide();
$('#hover').hover(function () {
var blab_id = $('#blab').text();
$('#id').show();
}, function () {
$('#id').hide();
});
It shows the div for the first one only.
You can only use a certain ID once on the whole page! If you insert more than one copy of this div on the page, your HTML code becomes invalid. That's why jQuery only attaches the event handler to the first element.
The solution is to use class="hover_enabled" instead of id="hover" as it is allowed to add the same class to multiple elements. Here is how you would do it in jQuery:
$('.hover_enabled').hover(function() { ...
The same thing applies to #id.
I have a table with some checkboxes in every row. There is a select element in the table footer with several options. When the user chooses an option, i want to pass the value of any checked checkboxes and the selected option to a php script.
This is the form:
<form name="doall" method="POST" action="action.php">
<table>
<tbody>
<?php for($j=0;$j<count($userToRead);$j++){ ?>
<tr>
<td>
<input type="checkbox" id="selection_<?php echo $j ?>" name="objectSelected[<?php echo $userToRead[$j]['id'] ?>]" value="1"/>
</td>
<td align="center">
<?php echo $userToRead[$j]['id'] ?>
</td>
<td align="center" style="text-align:center;padding-left:15px;background-color:<?php echo $SListsStatus[intval($userToRead[$j]['userstatus'])][1]; ?>;" >
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['user']) ?>
</td>
<td>
<?php echo stripaccenti($SUserType[$userToRead[$j]['type']]['name']) ?>
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['first_name']) ?>
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['last_name']) ?>
</td>
<td align="center">
<?php echo stripaccenti($userToRead[$j]['title']) ?>
</td>
<td class="actBtns">
<a href="#" title="Update" class="tipS">
<img src="images/icons/edit.png" alt="" />
</a>
<a href="#" title="Remove" class="tipS">
<img src="images/icons/remove.png" alt="" />
</a>
</td>
</tr>
<?php } // end for ?>
</tbody>
<tfoot>
<tr>
<td colspan="9">
<div class="itemActions">
<label>Con tutte le selezionate:</label>
<select name="whatDoAll">
<option value="1">Attiva</option>
<option value="2">Disattiva</option>
</select>
<input type="submit" value="Esegui" class="button redB" style="margin: 5px;"/>
</div>
</td>
</tr>
</tfoot>
</table>
</form>
So in every line of my table I have a checkbox where the value is the ID of the db object that I need to modify with my php script.
This is my script:
$('input[type="submit"]').click(function() {
var output = '';
$('input[type="objectSelected"]:checked').each(function(index) {
output += $(this).val() + ", ";
});
alert(output + "is checked!");
});
In the php file i try to retrieve the variable with $_POST['objectSelected'] but the variable is always empty. Also, the script alert is empty.
The tables are create with jquery: if I use a pure html table with pure html checkboxes everything works!
You are doing wrong. You are specifying input type then the code should be:
<script type="text/javascript">
$('input[type="submit"]').click(function() {
var output = '';
$('input[type="checkbox"]:checked').each(function(index) {
output += $(this).val() + ", ";
});
alert(output + "is checked!");
});
</script>
Hope this helps you