The following code segment is used by me to load the contents in the database as a table!(each row has a button with value load!
while($row=mysqli_fetch_array($result))
{
echo"
<tr id='rowNum'>
<td >".$row['No.']."</td>
<td >".$row['NIC']."</td>
<td >".$row['DP']."</td>
<td >".$row['DPTime']."</td>
<td >".$row['Telephone']."</td>
<td> <input type='button' id='load' class='btn btn-success' value='Load number' disabled=' '></td>
</tr>";
}
I also have the following text box on the same web page(outside the above table)!
echo "<input type=text name="display number">"
when i press the load button of the the relevant row of the table i want to display the No. of that row in the text box! how can i achieve this?
There are several problems with the code you posted. I'll do my best to get you on your way... You will want to do what you have described with JavaScript which runs on the client. Many people use libraries these days to work with JavaScript but to avoid added confusion I will recommend looking into jQuery and write my solution in the native code.
while($row=mysqli_fetch_array($result)) {
echo("
<tr id='rowNum".$row['No.']."'>
<td>".$row['No.']."</td>
<td>".$row['NIC']."</td>
<td>".$row['DP']."</td>
<td>".$row['DPTime']."</td>
<td>".$row['Telephone']."</td>
<td>
<input type='button' id='load".$row['No.']."' onClick='javascript:document.getElementById(\"displayNumber\").value=this.parentNode.parentNode.firstChild.innerHTML;' class='btn btn-success' value='Load Number'>
</td>
</tr>"
);
}
One big talking point is that you need to make sure that if you assign an id to any HTML element, unless it is a collection then that ID must be unique, otherwise your HTML will be just as invalid as your id. Also: Don't use spaces in names or ids. Just don't.
Finally, to fix up the input which is outside the table where you want the number to dynamically appear:
echo("<input type='text' name='displayNumber' id='displayNumber'>");
Related
I have a mysql database. In a table i have some entries of uploaded file. I want to display that on a php page. But those files are categorized by one column. So what i want is to have that categories name on that page. In front of each name i want to have a button. Initially when page loads it should not show the files. when some one click on a button then that corresponding category should expand.
But i am unable to do it. I am giving you a link to sample code.
HTML code
<span class="show"></span>
<p>category</p>
<table width='100%' class='tree'>
<tr>
<td width='70%'><b>Name</b></td>
<td width='30%'><b>Last Updated</b></td>
</tr>
</table>
http://jsfiddle.net/SumitRathore/FqcSM/
Edit
this is my sample html code where i am showing the tables. This code have some variables name and sql query don't go for that. I have found the answer here http://jsfiddle.net/JGLaa/2/ but that is not working here. I dont know why?
<span class="show"></span>
<p><b>category<b></p><br/>
<table width='100%' class='tree'>
<tr>
<td width='35%'><b>Name</b></td>
<td width='25%'><b>Last Updated</b></td>
</tr>
while($row = $result_sql->fetch_assoc())
{
<tr>
<td width='50%'><a href='http://127.0.0.1/wordpress/?page_id=464&name_file={$row['name']}&cat={$cat}&sec={$sec}' target='_blank'>{$row['title']}</a></td>
<td width='25%'>{$row['created']}</td>
<td width='25%'><input type='checkbox' class='checkbox'><input type='button' class= 'input1 input_box' value='delete' name='delete' id= '{$row['id']}'><input type='button' class='input2 input_box2' value='edit' name='edit' id= '{$row['id']}'></td>
</tr>
}
</table>
Your issue is that .next() searches the siblings, so it does not find a table. Try traversing up to the buttons parent (span), and then find the span's sibling table -
$(this).parent().nextAll('table:first').find('td').toggle();
updated JSFiddle - http://jsfiddle.net/JGLaa/1/
here is an updated JSFiddle that has multiple buttons/tables - http://jsfiddle.net/JGLaa/2/
Looking at you code the use of the next selector is flawed. From Jquery.com:
.next( [selector ] ) Description:
Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.
I would suggest to sourround your button and table with a div and use the following code:
$(document).ready(function(){
$(".show").html("<input type='button' value='Show All' class='treebtn'>");
$('.tree td').hide();
$('.treebtn ').click( function() {
$(this).parent().parent().find('table td').toggle();
if (this.value=="Show All") this.value = "Hide All";
else this.value = "Show All";
});
});
http://jsfiddle.net/FqcSM/3/
$(document).ready(function(){
$(".show").html("<input type='button' value='Show All' class='treebtn'>");
$('.tree').hide();
$('.treebtn ').on('click', function() {
$(this).parent('span').next('table').toggle();
if (this.value=="Show All") this.value = "Hide All";
else this.value = "Show All";
});
});
fiddle
I'm trying to make a dropdown when the reject checkbox is ticked. I have done this simply with Jquery but however because I need the function on all of the data that is pulled in, it copies the classes to the next row and so on, so when i check the reject button it opens all the dropdown text areas to input a reason. I was wondering how i would go about making this for an unlimited amount of data????? so it only ever opens one dropdown textarea.
I have read online that I can do this with php parenting? As my PHP skills are weak to say the least is there any simpler ways of going about this?
HTML
Name
Username
Email
Verify
Reject
if ($supplier['Id'] != '3') { ?>
<tr class="unverifiedSuppliers">
<td width="150"><?php echo $supplier['Name']; ?></td>
<td width="150"><?php echo $supplier['Username']; ?></td>
<td width="250"><?php echo $supplier['Email']; ?></td>
<td width="40"><input type='checkbox' name='verifySupplier[]' value='<?php echo $supplier['Id']; ?>' /></td>
<td width="40"><input class="checked" type='checkbox' name='rejectSupplier[]' value='<?php echo $supplier['Id']; ?>' /></td>
</tr>
<tr class="dropdown" style="display: none;">
<td colspan="5">
<label>Reason:</label>
<textarea name="rejectReason[<?php echo $supplier['Id']; ?>]"></textarea>
</td>
</tr>
<?php } } ?>
</table>
Jquery
$(function() {
$('.checked').change(function() {
$('.dropdown').toggle();
});
});
Try :
$('.checked').change(function() {
$(this).closest('tr').next('.dropdown').toggle();
});
$(this) is the DOM element you changed.
.closest('tr') is looking for the closest parent with the jQuery selector 'tr', wich mean the closest <tr></tr>.
.next('.dropdown') is looking for the immediate next sibling with the jQuery selector '.dropdown'.
Side note - using the selector here is facultative, it will just return an empty element if the next element doesnt have the class dropdown.
I am new here and i trying to make some PHP page working but failed miserably.
I am not very good with logic nor PHP, so please bear with my stupid question and my messy codes :)
I have array of textboxes with value and there are array of buttons next to it.
what i want is each button capture specific value what i typed into the corresponding textbox
here's the piece of my code
<?php
$data = mysql_query("SELECT * from tempimg");
while($hasil = mysql_fetch_assoc($data)){
$i++;
echo "<tr>
<td align=center><input type= checkbox name=check[] value=$hasil[idFoto]</td>
<td align=center><img src=$hasil[thumbPath]></td>
<td align=center>$hasil[imgName]</td>
<td align=center>$hasil[thumbPath]</td>
<td align=center>$hasil[Path]</td>
<td align=center>
<input type=text align=center value=$hasil[imgLink] name=link[{$hasil['idFoto']}] id=link />
<td align=center>
<button type=submit onClick=\"return confirm('you clicked button with ID: $hasil[idFoto] '+'value: '+(document.getElementById('link').value))\">
<img src=images/sav.png alt=search-btn id=img />
</button>
</td>
<td align=center><img src=images/del.png></img></td>";
}
?>
and here's the link of image for the PHP page I'm talking about
so I humbly request of help from people here, please help me.
EDIT: thanks to Mr. Barmar i manage to pop out the value of the text box inside the dialog box with the corresponding button,
here a new question, how to save the value from the text box that i got from clicking the corresponding button into the database?
or more simple, how to capture the value from the text box by using the button next to it and then post it on the screen using "echo"
You need to give all the id=XXX attributes unique values, by including $i in the ID. Then your onclick code can get the value of the input from the same row.
<?php
$data = mysql_query("SELECT * from tempimg");
while($hasil = mysql_fetch_assoc($data)){
$i++;
echo "<tr>
<td align='center'><input type='checkbox' name='check[]' value='$hasil[idFoto]'</td>
<td align='center'><img src='$hasil[thumbPath]'></td>
<td align='center'>$hasil[imgName]</td>
<td align='center'>$hasil[thumbPath]</td>
<td align='center'>$hasil[Path]</td>
<td align='center'>
<input type='text' align='center' value='$hasil[imgLink]' name='link[{$hasil['idFoto']}]' id='link$i' />
<td align='center'>
<button type='submit' onclick='return confirm(\"you clicked button with ID: $hasil[idFoto] \"+\"value: \"+(document.getElementById(\"link$i\").value))'>
<img src='images/sav.png' alt='search-btn' id='img$i' />
</button>
</td>
<td align='center'><img src='images/del.png' /></td>";
}
?>
You should also put quotes around all attribute values.
I am working on an php file, winch shows an table with values of the database. every row has an imagebutton at the end, which should change the status, displayed with an image.
so I think short function of it all must be, on click go to script go to php or jQuery?
the buttons also have to give the id of the user to the next script or function, or os there a better way?
here is my basic approach, with no functionality, for the moment
in index.php
include("function.php");
while ($row_user = mysql_fetch_assoc($result_user)) {
$sqle = mysql_query("
SELECT COUNT(*) FROM
places2user
WHERE
user = '".mysql_real_escape_string($row_user['ID'])."'
");
$res23 = mysql_fetch_array($sqle);
echo <table>
echo <tr>
echo "<td>".$row_user['ID']." </td><td><input type=\"image\" src=\"images/".htmlentities($row_user['receive'], ENT_QUOTES)."\" id=\"status\" name=\"status\" ></td>
echo </tr>
echo </table>
}
there is also a function.php, with a changestatus($id) function, which updates the database values, but I think, this is not the right way to code this.
While programming, don't rely on javascript only. I always create form using just html and css, make sure it works, and than add fancy stuff with jQuery or so. This way you know the form will also work without javascript (great for users without it).
But to come to your question:
if you use this, it should do what you want:
<?php echo "<form action='?' method='post'>
<table>
<tr>
<td>".$row_user['ID']." </td>
<td><input type='hidden' name='userId' value='".$row_user['ID']."'/>
<input type=\"image\" src=\"images/".htmlentities($row_user['receive'], ENT_QUOTES)."\" id=\"status\" name=\"status\" ></td>
</tr>
</table></form>";
I am having one form. I am submitting the values to database of table test fields something .I want to fetch those existing values to text field so that I can edit and update again.
I am using below code but I am not getting valid output.
Can anyone help thanks in advance.
//these values iam passing from one form
$bre=$_POST['bf'];
$bres=$_POST['bs'];
$bree=$_POST['be'];
$lun=$_POST['lu'];
$luns=$_POST['ls'];
$lune=$_POST['le'];
$dinn=$_POST['din'];
$dins=$_POST['sd'];
$dine=$_POST['se'];
echo $bre;
echo $bres;
echo $bree;
echo $lun;
echo $luns;
echo $lune;
echo $dinn;
echo $dins;
echo $dine;
echo "<table width=791 border=1><tr><td width=243>Breakfast<input type=text name=bfh value= $bre></td>
<td width=239>Stat Time<input type=hidden name=bsh value=$bres/>
</td>
<td width=244>
End Time
<input type=text name=beh value=$bree />
</td>
</tr>
<tr>
<td>
Lunch
<input type=hidden name=luh value=$lun />
</td>
<td>Stat Time
<input type=hidden name=lsh value=$luns/>
</td>
<td>End Time
<input type=hidden name=leh value=$lune/>
</td>
</tr>
<tr>
<td>Dinner
<input type=hidden name=dinh value=$dinn/></td>
<td>Stat Time
<input type=hidden name=sdh value=$dins/></td>
<td>End Time
<input type=hidden name=seh value=$dine/></td>
</tr>
</table>";
You did not escape your input properly. Use the function htmlentities on every variable that is displayed on the HTML page. Otherwise you might get invalid text (as you’ve noticed) or worse, a big security hole, because a malicious user can enter arbitrary HTML/JavaScript combinations that will be included in your HTML.
You're missing quotes around the values of the attributes on the input-elements.
I would recommend using a html validator to find such issues.
You can also print out each variable initially with
print_r($_REQUEST);
to be sure they enter the script ok.
<input type=text name=beh value=$bree />
also this would be better like this
<input type='text' name='beh' value='{$bree}' />
you have to put ' or " for each HTML attributes, and putting php replaces between {} would help you to see what is replaced PHP and would help if you access a variable in an object i.e. {$foobar->bar}
also like noted on other answer you have to use htmlentities the HTML is escaped properly.
Also your input are inside a <form> tag ? your table should be around a tag like that
<form method="POST">