Im trying to develop a edit function, where you can edit different of notes by clicking on a simple link "Edit". When u click on this, the note beside the link should tranform from a div, to a editable textarea.
The code:
$(".edit").click( function(){
$('.edit_note').replaceWith(function() {
return $("<textarea>").text(this.innerHTML);
});
});
The problem is that when I click edit on a note, instead of just make that note editable, ALL the notes in the list are editable. How can I make sure of that only the note that I want to edit, is editable?
The notes are looped out from a table in MySQL.
foreach($byanat_notering as $key => $value){
echo "<tr><td width='160' valign='top'><font size='2'>";
echo substr($value["created"],0,16);
echo "<br />".$user[$value["users_id"]]["namn"];
echo "<br/><a href='#' class='edit'>Redigera</a>";
echo "</td><td width='550' valign='top'><font size='2'>";
echo "<div class='edit_note'>" . $value["text"] . "</div>";
echo "</td></tr>";
}
You need to separate it out using unique ids, I'm using a variable $i for it:
$i = 0;
foreach($byanat_notering as $key => $value){
echo "<tr><td width='160' valign='top'><font size='2'>";
echo substr($value["created"],0,16);
echo "<br />".$user[$value["users_id"]]["namn"];
echo "<br/><a href='#' class='edit' id='edit_". $i ."'>Redigera</a>";
echo "</td><td width='550' valign='top'><font size='2'>";
echo "<div class='edit_note' id='edit_note_". $i ."'>" . $value["text"] . "</div>";
echo "</td></tr>";
$i++;
}
$i = 0;
And in JavaScript refer by these unique ids only:
$(".edit").click( function(){
var id = parseInt($(this).attr('id').split('_')[1]); //returns the unique integer
//edit only the div with that integer assigned
$('#edit_note_'+id).replaceWith(function() {
return $("<textarea>").text(this.innerHTML);
});
});
I'm using the for loop's index ($i) to match a hrefs against the divs.
Try putting javascript inside for each loop
<?php
foreach($byanat_notering as $key => $value){
?>
<script type="text/javascript">
$(".edit-<?php echo $value["users_id"]; ?>").click( function(){
$('.edit_note-<?php echo $value["users_id"]; ?>').replaceWith(function() {
return $("<textarea>").text(this.innerHTML);
});
});
</script>
<?php
echo "<tr><td width='160' valign='top'><font size='2'>";
echo substr($value["created"],0,16);
echo "<br />".$user[$value["users_id"]]["namn"];
echo "<br/><a href='#' class='edit-".$value["users_id"]."'>Redigera</a>";
echo "</td><td width='550' valign='top'><font size='2'>";
echo "<div class='edit_note-".$value["users_id"]."'>" . $value["text"] . "</div>";
echo "</td></tr>";
}
?>
Related
I'm working for a system by the use of php. We have an admin.php page where all the list of applicants were listed. And we want that when we click on a specific applicant's name, a lightbox would appear that will show more information about the applicant. But, these codes below seems to be not working and I don't know why. I am a newbie in php btw.
<?php
include('config.php');
$result = mysql_query("SELECT * FROM employee as t1")
or die(mysql_error());
echo "<table border='0' cellpadding='15' text-align = 'center' >";
echo "<tr>";
echo " <caption><b><font size = '5'> <h2>List of Applicants </h2> </b> </font><hr></caption>";
echo "<th>Applicant ID</th>";
echo "<th>Application Date</th>";
echo "<th>Name</th>";
echo "<th>Job</th>";
echo "</tr>";
while($row = mysql_fetch_array( $result ))
{
echo "<tr class='gradeC'>";
echo '<td><b><font color="#663300">' . $row['employee_id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['date'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['fname'] . '</font></b></td>';
echo '<td><td>';
echo "</tr>";
}
echo "</table>";
function runMyFunction() {
echo "<div onclick =\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\"></div>";
echo "<div id=\"light\" class=\"white_content\">This is the lightbox content. Close</div>";
echo "<div id=\"fade\" class=\"black_overlay\"></div>";
}
if (isset($_GET['hello'])) {
runMyFunction();
}
?>
I'm not sure but I think the problem is in the function, or the way I'm using the codes for lightbox to appear.
Can you rewrite this line
function runMyFunction() {
echo "<div onclick =\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\"></div>";
echo "<div id=\"light\" class=\"white_content\">This is the lightbox content. Close</div>";
echo "<div id=\"fade\" class=\"black_overlay\"></div>";
}
to
function runMyFunction() {
?>
<div onclick ="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"></div>
<div id="light" class="white_content">This is the lightbox content.
Close</div>
<div id="fade" class="black_overlay"></div>
<?php }
By eliminating the slashes could help make the code easier to process.
I hope this help.
I have this query:
$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid ORDER BY WEEK_ID");
$current_week_id = -1;
while($row = mysqli_fetch_array($result))
{
if($current_week_id != $row['WEEK_ID'])
{
if($current_week_id != - 1)
{
echo "</table>";
}
echo "<table>";
echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";
echo "<tr>";
echo "<th width='100'>Day</th>";
echo "<th width='75'>Start</th>";
echo "<th width='75'>End</th>";
echo "<th width='100'>Billable Hours</th>";
echo "<th width='100'>Non Billable Hours</th>";
echo "</tr>";
$current_week_id = $row['WEEK_ID'];
}
echo "<tr>";
echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
echo "<td class='tdclass'>" . $row['START'] . "</td>";
echo "<td class='tdclass'>" . $row['END'] . "</td>";
echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
echo "</tr>";
}
if($current_week_id != - 1)
{
echo "</table>";
}
This provides me with seperate tables for each week ID. However I am looking to get buttons to display below each table which are associated with the results above. Is it possible to add a button with the value of WEEK_ID. Currently if I add a button to the top and the bottom with value:
<input type='image' name='submit' src="image/button.jpg" value=" . $row['WEEK_ID'] . ">
It doesn't show the right ID for the top tables and it shows nothing for the bottom. I do understand why this is but is there anyway I can associate this button underneath the table?
Use this outside the PHP tag:
<input type="image" name="submit" src="image/button.jpg" value="<?php echo $row['WEEK_ID'] ?>">
Use this inside the PHP tag.
echo '<input type="image" name="submit" src="image/button.jpg" value="' . $row['WEEK_ID'] . '">';
In my PHP file, I have an echo statement, that outputs some HTML, within which i want to do some assignment based on onclick event.
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='". $_SESSION['dakno'] = $r[$j]; ."' >".$r[$j]."</a></td>";
I have tried a lot of combinations, but still getting syntax error because of the onclick section.
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='"<?php $_SESSION['dakno'] = $r[$j]; ?> "' >".$r[$j]."</a></td>";
EDITS:
I am an output field in a table to be a hyperlink. On clicking the link, the value of the clicked item is passed to another PHP file using a SESSION variable.
$sno = 1;
while($r = mysqli_fetch_array($rs)){
echo "<tr>";
echo "<td style='padding:10px; text-align:left;'>".$sno."</td>"; $sno++;
for( $j=1; $j<6; $j++){
if($j == 1){
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='". $_SESSION['dakno'] = $r[$j]; ."' >".$r[$j]."</a></td>";
continue;
}
else
echo "<td style='padding:10px; text-align:left;'>".$r[$j]."</td>";
}
echo "</tr>";
}
Please, help me to remove the syntax error I am making.
As specified in the question, I needed to pass the value to another PHP file when someone clicked the link. I did not want to use AJAX here as because I do not expect to update any content dynamically. After two hours of brainstorming, I solved my problem with an extremely basic solution.
$sno = 1;
while($r = mysqli_fetch_array($rs)){
echo "";
echo "<tr>";
echo "<td style='padding:10px; text-align:left;'>".$sno."</td>"; $sno++;
for( $j=1; $j<6; $j++){
if($j == 1){
echo "<td style='padding:10px; text-align:left;'><form action='stat.php' method='POST'> <input type='hidden' name='dakno' value='".$r[$j]."' > </input> <button class='dakbutton' type='submit'>".$r[$j]."</button></form></td>";
continue;
}
else
echo "<td style='padding:10px; text-align:left;'>".$r[$j]."</td>";
}
echo "</tr>";
echo "</form>";
}
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php' onclick='" .
$_SESSION['dakno'] = $r[$j] .
"' >" .
$r[$j].
"</a></td>";
remove ; in $_SESSION['dakno'] = $r[$j];
I want to set or assign id / rel inside my table code, take a look on my code below this so every table td has id and rel so I can give statement and manipulate the date within the jQuery, any helps will be much more appreciated:
function printdata()
{
if(is_array($_SESSION['chart']))
{
echo "Here's your chart" . "<br>";
$max = count($_SESSION['chart']);
$th1 = "<th>" . "No" . "</th>";
$th2 = "<th>" . "Nama Barang" . "</th>";
$th3 = "<th>" . "Harga Barang" . "</th>";
$th4 = "<th>" . "Stok Barang" . "</th>";
echo "<table border=1>";
echo "<tr>";
echo $th1 ;
echo $th2;
echo $th3;
echo $th4;
echo "</tr>";
for ($indexo = 0; $indexo < $max; $indexo++) {
echo "<tr>";
echo "<td>" . $indexo."</td>";
echo "<td >" . $_SESSION['chart'][$indexo]['namabarang']."</td>";
//i want to assign td rel inside of this echo
echo "<td>" . $_SESSION['chart'][$indexo]['hargabarang']."</td>";
echo "<td>" . $_SESSION['chart'][$indexo]['stokbarang']."</td>";
echo " <td><button id=".$indexo." name=\"button\">Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Chart is still Empty";
}
}
echo "<table border=1 id=\"{$some_php_variable_with_an_id_value_in_it}\">"
first your code is too much line over there, that is no need to do such as that, it can be more simple to build some output with the same result, here may be could to help ya
function printdata(){
if(is_array($_SESSION['cart'])){
echo "Here's Your Chart ".count($_SESSION['cart'])."<br />";
echo '<table><tr>th>No</th><th>Nama Barang</th><th>Harga Barang</th><th>Stok Barang</th><th>Action</th></tr>';
for($indexo =0; $indexp < count($_SESSION['cart']); $indexo++){
echo "<tr>
<td id=".$indexo.">".$indexo."</td>
<td id=".$_SESSION['chart'][$indexo]['namabarang'].">".$_SESSION['chart'][$indexo]['namabarang']."<td>
<td id=".$_SESSION['chart'][$indexo]['hargabarang'].">".$_SESSION['chart'][$indexo]['hargabarang']."<td>
<td id="$_SESSION['chart'][$indexo]['stokbarang']">".$_SESSION['chart'][$indexo]['stokbarang']."<td>
<td><button id=".$indexo." name=\"button\">Edit</button></td>
</tr>";
}
}else{
echo "Chart is still Empty";
}
}
You can Change <td id=".$indexo."> to what ever you want to, seems like the other, id or rel will be the same implementation i guest
I'm a javascript newbie so please, any help would be appreciate. I've been trying to get selected values from a drop down list on my page in order to swap them . I've tried looking for solutions online but I'm stumped. For some reason, s1[i].selected doesn't work at all. Here's my javascript code:
<script type="text/javascript">
function swaprows(s1, s2)
{
var items = "";
var len = s1.length;
for(i=0;i<len;i++)
{
if(s1[i].selected)
{
}
}
}
</script>
The thing is I'm generating these listboxes on the fly using php, they're not hard coded into my HTML. Here's the code I use to create them and call the javascript function:
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<ul>
<?php
do
{
echo "<li>";
echo "<h2 class=\"expand\" >Choose the sizes for " . $product_array['Product Name'] . "\n</h2>";
echo "<div class=\"collapse\">";
echo "<table border=\"0\">";
echo "<tr>";
echo "<td rowspan=\"2\"><select size=\"8\" multiple=\"true\" name=\"selectsizes[]\" id=\"selectsizes\">\n";
for($i=0; $i < sizeof($sizes_data); $i++)
{
echo "<option value=" . $sizes_data[$i]['Size ID'] . ">" . $sizes_data[$i]['Size Name'] . "</option>\n";
}
echo "</select></td>\n";
echo "<td valign=\"top\"><input type = \"button\" OnClick=\"swaprows(this.form.selectsizes, this.form.selectedsizes);\" value=\"-->\" /></td>";
echo "<td rowspan=\"2\"><select multiple=\"true\" name=\"selectedsizes[]\" id=\"selectedsizes\">\n";
echo "</select></td>\n";
echo "<td rowspan=\"2\" valign=\"bottom\"><input type=\"submit\" id=\"btnSubmit\" name=\"SaveSizes[" . $product_array['Product ID'] . "]\" value=\"Save\" /></td>";
echo "</tr><tr>";
echo "<td valign=\"top\"><input type = \"button\" OnClick=\"swaprows(this.form.selectedsizes, this.form.selectsizes);\" value=\"<--\" /></td>";
echo "</tr>";
echo "</table>";
echo "</li>";
}while($product_array = mysql_fetch_assoc($product_result));
?>
</ul>
</form>
Because I'm doing a post action with the submit buttons I need to name my lists as selectsizes[] and selectedsizes[] respectively so that I can then pass their selected values via the post variable. Can anyone help me out here?
I'm pretty sure you don't need javascript for this.. just the [] in the name of the field. then on the other side php will auto make the array in the post variable of the selected fields.
$sizes = $_POST['select_sizes'];
$quantity = sizeof($sizes);
$inc = 0;
while ($inc < $quantity) {
echo "User selected:".$sizes[$inc];
$inc++;
}
Try
if(s1[i].selected == true)
or
var sPool = s1[i];
if(sPool.selected == true)