Is there a way to toggle show/hide for data that is dynamically populated in a php loop?
I’ve been around and around trying to figure out the best way to do this, but I just don’t think I know enough to make this work and I’m not sure what best practice is.
Here’s the situation:
- Applicants are submitting abstracts via my website, and they go into a database
- Administrators log in, and view the submitted results- this is a table that has some of the basic information, but not the abstract text because that is too long.
- I would like to have a button (or something else!) that will show/hide the abstract text, along with giving the administrator the opportunity to assign the abstract to a session.
I thought I could just do a jquery show/hide button thing, but I can’t get it to work. Here’s the code- and note this is all development and not all security features are here. I say this because there’s inevitably someone that will comment on sessions or escape strings, etc.
<?php
include_once('xxx.php');
$conn = new connectorfunction();
$query = "SELECT * FROM tablename ORDER BY abstract_id";
$result = mysql_query($query);
$numfields = mysql_num_fields($result);
$data = array();
$flist = array();
for($i=0;$i<$numfields;$i++)$flist[] = mysql_field_name($result,$i);
$data[0] = $flist;
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
print '
<tr>
<td>
<span style="text-decoration: underline">Author:</span>
<br />
' . $row['abstract_author'] .'
</td>
<td>
<span style="text-decoration: underline">Title:</span>
' . $row['presentation_title'] . '
<br />
<button>View/Assign</button>
</td>
<td>
';
if ($row['abstract_category'] === NULL ) {
print '
Needs Session
';
}
else {
print '
Assigned
';
}
print'
<tr style="display:none;">
<td colspan="3">
'. $row['abstract_text'] .'
</tr>
<tr style="display:none;">
<td colspan="3">
<form action="assign_session.php" method="post" id="form">
<label for="abstract_category">Assign Session:
<input type="hidden" name="abstract_id" value="'. $row['abstract_id'] .'" />
<input type="radio" name="abstract_category" value="session1" />Session One
<input type="radio" name="abstract_category" value="session2" />Session Two
<input type="radio" name="abstract_category" value="notapplicable" />Not Applicable
<button type="submit" formaction="assign_session.php">Assign</button></label>
</form>
</td>
</tr>
';
}
?>
So you can see, there’s a button: < button >View/Assign< / button >
And there are the two < tr > with the “display: none” style.
The table looks really great when it is populated, I just need a good way for the admins to see the abstract text, but I don’t seem to be able to successfully point to jquery functions within php. Any advice on the best way to handle this?
Something like this? demo
I use jquery to toggle (show/hide) the elements you need to show/hide.
Moreover you have several errors on this code (like you are not closing the first td, not using in the right way labels).