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];
Related
I'm getting all rows from mysql database. Now I want to highlight only first row in php while loop with class name keywordHighlight.
How do I highlight only first row in php while loop result ?
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($resGetSearch = mysql_fetch_array($getSearch)){
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
echo "<tr onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Do it like this
$i = 1;
while($resGetSearch = mysql_fetch_array($getSearch)){
$highlight = $i == 1 ? 'keywordHighlight' : '';
echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
---------------
-------------
-------------
$i++;
}
Only with CSS
or you can highlight it only with css
#highlight tbody tr:nth-child(1){
background: #ff6600;
}
There is more and elegant way to highlight only first row with only css not need to code, consider the example http://jsbin.com/soravuzakahu/1/
You could just add a boolean outside the loop. Like so:
$first = true;
while($resGetSearch = mysql_fetch_array($getSearch)){
if(first == true){
// Add code here that only applies to the first iteration.
}
$first = false;
}
<?php
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$i = 0;
while($resGetSearch = mysql_fetch_array($getSearch)){
++$i;
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
if($i == 1)
echo "<tr onclick='getDetails($SearchCdid);' >";
else
echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Use a flag and set its value to true and while in loop check its value , if its true then print class name and set its value to false. Like $flag=true; then inside while loop check
if($flag==true) {
<td class='yourclassname';
$flag= false;
}
put some flag $f=0;
if f==0 then do something like this:
$highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
$f=$f+1;
and rest in else part.
$first = true;
while ( $resGetSearch = mysql_fetch_array($getSearch) )
{
if ( $first == true )
{
// Add code here that only applies to the first iteration.
$first = false;
}
else
{
// Add code here that only applies to the 2, 3, and so on.
}
}
I'm making a "delete" function on my form. I'm not quite happy because it doesn't work! I have tried this code:
while($row = $result->fetch_assoc())
{
$ordernr = $row['ordernr'];
$klantnaam = $row['klantnaam'];
$productnaam = $row['productnaam'];
$productid = $row['productid'];
echo "<tr>";
echo "<td width='150px'>" . $ordernr . "</td>";
echo "<td width='150px'>" . $klantnaam . "</td>";
echo "<td width='300px'>" . $productnaam . "</td>";
echo "<td width='100px'>" . $productid . "</td>";
echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><input type='submit' name='verwijderen' value='Verwijderen'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
$delete = $_POST['delete'];
if (isset($_POST['verwijderen'])) {
foreach($delete as $ordernr => $delete)
{
$ordernr = mysqli_real_escape_string($ordernr);
$query = mysqli_query("DELETE FROM overzicht WHERE ordernr= $ordernr");
}
}
Do you guys know what I've done wrong?
Thanks in advance!
1)
echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";
transforms to
echo "<td align='center' width='50px'><input name='delete' type='checkbox' value='$ordernr'></td>";
2)
foreach($delete as $ordernr => $delete)
do it:
foreach($delete as $ordernr => $del)
you are overwriting the $_POST
mysqli_query syntaxt is
mysqli_query($con,"your query");
I'd go and say that your input checkbox needs a value. Like
<input type="checkbox" name="delete[1]" value="1" />
On the other hand, then it would be easier to have
<input type="checkbox" name="delete[]" value="$ordernr" />
And then you loop with
foreach ($_POST["delete"] as $id) delete(id)
where con variable in your query...
$query = mysqli_query($con,"DELETE FROM overzicht WHERE ordernr= $ordernr");
You're iterating while reusing your variable here:
foreach($delete as $ordernr => $delete)
Where the second $delete will overwrite the value of the first, and throw a spanner in the works.
I suggest to rename the prior instances of $delete to $deleteList and do:
foreach($deleteList as $ordernr => $delete)
Keep in mind that much of your code could be done in different ways that may be more suitable. You will find those as you follow your path in learning PHP.
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>";
}
?>
I need some help regarding pagination, the problem is that I want to show my database records first time on button click with pagination but every time I click on pagination link it requires click on button
Here is my code I know I am making some mistake please check and kindly do me a favor by correcting my mistake ..
if (isset($_POST["search"])){
$result=mysql_query("select count(*) from reg_phone");
$row=mysql_fetch_row($result);
$tr=$row[0];
$rpp=4;
$pn=1;
if(isset($_GET['pn']))
{
$pn=$_GET['pn'];
}
$tp=($tr/$rpp);
if($tr/$rpp>=0)
{
$tp++;
}
$from=(($pn-1)*$rpp)+1;
$to=($pn)*($rpp);
$show = "SELECT * FROM reg_phone where Id between $from and $to";
$rs = mysql_query($show) or die(mysql_error());
#****results in Grid****
echo "<table width='100%' border='1' cellpadding='2' border-color='#000' id='tbl'>";
echo "<tr>";
echo "<td style='background: white;'><b>IMEI #</td>";
echo "<td style='background: white;'><b>Phone #</td>";
echo "<td style='background: white;'><b>From Date</td>";
echo "<td style='background: white;'><b>Status</td>";
echo "</tr>";
$rowID = 1;
while ($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td id='imeinum" . $rowID . "'>$row[imei]</td>";
echo "<td id='phnum" . $rowID . "'>$row[phonenum]</td>";
echo "<td id='datepicker" . $rowID . "'>$row[fdate]</td>";
echo "<td id='rad" . $rowID . "'>$row[status]</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
echo "<ul id='pages'>";
for($i=1;$i<=$tp;$i++)
{
echo "<li><a href='phonereg.php?pn=$i'>$i</a></li>";
}
echo "</ul>";
#**********************
mysql_free_result($rs);
}
http://www.hscripts.com/scripts/jquery/pagination.php
the script here is really helpful to me. Exactly what i want to do. hope anyone one in future will need this.
thanks all for suggestions.
Regards.
BKay
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)