I have this code:
<?php
session_start();?>
<!DOCTYPE html>
<html>
<head>
<title>Narcis Bet</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css">
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<header>
<div id="top">
<img src="images/logo.png" width="400" height="140" alt="logo"/>
<?php
if(!isset($_SESSION['utilizator']))
{
?>
<div id="topright">
<form action="api/login.php" method="POST" id="toprightt">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" value="Login"/>
</form>
</div>
<img src="images/facebook.png" alt="facebook"/>
Contact
Termeni și condiții
</div>
</header>
<?php
if(isset($_SESSION['eroare']))
{
echo $_SESSION['eroare'];
unset($_SESSION['eroare']);
}
}
else
{
echo "<h1 id="."topright".">Bun venit,".$_SESSION['utilizator']."</h1>";
?>
<form action="api/logout.php" id="toprightl">
<input type="submit" value="Logout"/>
</form>
<nav class="nav">
<ul>
<li>Pariuri</li>
<li>Loto</li>
<li>Despre</li>
<li>Regulament</li>
</ul>
</nav>
<section class="main">
<table class="tableright" id="talon">
<tr>
<th>Cod</th>
<th>Eveniment</th>
<th>Data</th>
<th>Cota</th>
</tr>
</table>
<table class="table1" >
<thead>
<tr><th colspan="6" class="th1">FOTBAL</th></tr>
<tr>
<th>Cod.</th>
<th>Eveniment</th>
<th>data</th>
<th >1</th>
<th >X</th>
<th >2</th>
</tr>
</thead>
<?php
$handle = #fopen("../res/fotbal.txt", "r");
if ($handle)
{
while (($buffer = fgets($handle, 4096)) !== false)
{
$pariu=json_decode($buffer,true);
echo "<tr>";
echo "<td id='cod'>".$pariu["id"]."</td>";
echo "<td id='ev'>".$pariu['eveniment']."</td>";
echo "<td id='data'>".$pariu['data']."</td>";
echo "<td class='td1' onclick='FUNCTIE(".$pariu["id"].",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",'1')>".$pariu['1']."</td>";
echo "<td class='td1' onclick='FUNCTIE(".$pariu["id"].",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",'X')>".$pariu['X']."</td>";
echo "<td class='td1' onclick='FUNCTIE(".$pariu["id"].",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",'2')>".$pariu['2']."</td>";
/*echo "<td class='td1'><button onclick='FUNCTIE(".$pariu["id"].",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",'1')".$pariu['1']."</button></td>";
echo "<td class='td1'><button onclick='FUNCTIE(".$pariu["id"].",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",'X')".$pariu['1']."</button></td>";
echo "<td class='td1'><button onclick='FUNCTIE(".$pariu["id"].",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",'2')".$pariu['1']."</button></td>";
*/
echo "</tr>";
}
if (!feof($handle))
{
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
} ?>
</table>
</section>
</body>
</html>
and the function in javascript:
function FUNCTIE(id,eveniment,data,tip_pariu)
{
var table=document.getElementById('talon');
if(table)
{
table.innerHTML+="<tr><td>"+id+"</td><td>"+eveniment+"</td><td>"+data+"</td><td>"+tip_pariu+"</td><tr>";
}
}
and i get this error(uncaught syntaxerror unexpected token }) when i press on the td with onlick function.Please help me !I dont know why i get this error!.......................................................................................................................................................................................................................................................................................................................................................
unless they are all numbers, keywords, functions, or variable names, you should wrap your calls in quotes. so code becomes... also there is a ' left out.
... ... ...
echo "<td class='td1' onclick='FUNCTIE(\"".$pariu["id"]."\",\"".$pariu["eveniment"]."\",\"".$pariu["data"]."\",1)'>".$pariu["1"]."</td>";
... ... ...
maybe that will work
Related
I have a table on my website, which gets data from a database, in the table I have delete buttons on a row, I have just made the table searchable with jQuery, but now I can't figure out how to keep the delete buttons.
index.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta charset="utf-8">
<meta name="description" content="Inventar">
<meta name="author" content="Martin Eide Bjørndal">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="/src/css/style.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<button class="back" onclick="window.location='../'">
<img src="/src/icons/back.png">
</button> <!-- Tilbake knapp -->
<button class="ny_inventar" onclick="window.location='/admin/brukere/ny'">
<img src="/src/icons/add.png">
</button> <!-- Ny Bruker knapp -->
<div class="container-fluid" id="utskjekkcontainer">
<div class="row justify-content-center">
<div class="col-mv-10 bg-light mt-5 rounded p-3">
<h1 class="text-primary p-2">Brukere</h1>
<hr>
<div class="form-inline">
<label for="search" class="font-weight-bold lead text-dark">Søk</label>
<input type="text" name="search" id="search_text" class="form-control form-control-lg rounded-0 border-primary" placeholder="Søk...">
</div>
<hr>
<?php
include "../../src/fn/init.php";
$stmt=$conn->prepare("SELECT * FROM brukere");
$stmt->execute();
$result=$stmt->get_result();
?>
<table class="table table-hover table-light table-striped" id="table_data">
<thead>
<tr>
<th>#</th>
<th>Fornavn</th>
<th>Etternavn</th>
<th>Utskjekket</th>
<th>Admin</th>
<th>Slett</th>
</tr>
</thead>
<tbody>
<?php while($row=$result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["fornavn"]; ?></td>
<td><?php echo $row["etternavn"]; ?></td>
<td><?php echo $row["utskjekket"]; ?></td>
<td><?php echo $row["is_admin"]; ?></td>
<td>Slett</td>
</tr>
<?php }
$conn->close();
?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#search_text').keyup(function(){
var search = $(this).val();
$.ajax({
url:'action.php',
method:'post',
data:{query:search},
success:function(response){
$('#table_data').html(response);
}
});
});
});
</script>
</body>
</html>
action.php:
<?php
include "../../src/fn/init.php";
$output = "";
if(isset($_POST["query"])){
$search = $_POST["query"];
$stmt = $conn->prepare("SELECT * FROM brukere WHERE fornavn LIKE CONCAT('%',?,'%') OR etternavn LIKE CONCAT('%',?,'%') OR id LIKE CONCAT('%',?,'%')");
$stmt->bind_param("sss",$search, $search, $search);
} else {
$stmt=$conn->prepare("SELECT * FROM brukere");
};
$stmt->execute();
$result=$stmt->get_result();
if($result->num_rows>0){
$output = "<thead>
<tr>
<th>#</th>
<th>Fornavn</th>
<th>Etternavn</th>
<th>Utskjekket</th>
<th>Admin</th>
<th>Slett</th>
</tr>
</thead>
<tbody>";
while($row=$result->fetch_assoc()){
$output .= "
<tr>
<td>".$row["id"]."</td>
<td>".$row["fornavn"]."</td>
<td>".$row["etternavn"]."</td>
<td>".$row["utskjekket"]."</td>
<td>".$row["is_admin"]."</td>
<td>".Slett."</td>
</tr>";
};
$output .= "</tbody>";
echo $output;
} else {
echo "<h3>No match found!</h3>";
};
?>
This is the search script, the problem is in the while loop where the link is added, I can't figure out how to make it work.
The a tag is not added as a plain-text to the output string, you need to rewrite that line, concatenate strings parts to variables parts correctly.
Try replacing:
$output .= "
<tr>
<td>".$row["id"]."</td>
<td>".$row["fornavn"]."</td>
<td>".$row["etternavn"]."</td>
<td>".$row["utskjekket"]."</td>
<td>".$row["is_admin"]."</td>
<td>".Slett."</td>
</tr>";
with:
$output .= "
<tr>
<td>".$row["id"]."</td>
<td>".$row["fornavn"]."</td>
<td>".$row["etternavn"]."</td>
<td>".$row["utskjekket"]."</td>
<td>".$row["is_admin"]."</td>
<td><a href='delete.php?id=".urlencode($row['id'])."' onclick='return confirm('Er du sikker?');'>Slett</a></td>
</tr>";
To allow PHP to echo the id from the table result you need to just simply do a php echo
Slett
You don't have to url encode at this stage as you are just simply building the href of an a:link
You can use it
<?php
include "../../src/fn/init.php";
$output = "";
if(isset($_POST["query"])){
$search = $_POST["query"];
$stmt = $conn->prepare("SELECT * FROM brukere WHERE fornavn LIKE CONCAT('%',?,'%') OR etternavn LIKE CONCAT('%',?,'%') OR id LIKE CONCAT('%',?,'%')");
$stmt->bind_param("sss",$search, $search, $search);
} else {
$stmt=$conn->prepare("SELECT * FROM brukere");
};
$stmt->execute();
$result=$stmt->get_result();
if($result->num_rows>0){
$output = "<thead>
<tr>
<th>#</th>
<th>Fornavn</th>
<th>Etternavn</th>
<th>Utskjekket</th>
<th>Admin</th>
<th>Slett</th>
</tr>
</thead>
<tbody>";
while($row=$result->fetch_assoc()){
$output .= "
<tr>
<td>".$row["id"]."</td>
<td>".$row["fornavn"]."</td>
<td>".$row["etternavn"]."</td>
<td>".$row["utskjekket"]."</td>
<td>".$row["is_admin"]."</td>
<td>Slett </td>
</tr>";
};
$output .= "</tbody>";
echo $output;
} else {
echo "<h3>No match found!</h3>";
};
What I want to be able to do:
I have a page setup so that a table is populated with values from the database. I also generate a button for each of the rows on the table. The button is connected to the database to a field called status. It is initially at 0. When the button is clicked, I want to be able to update this value by incrementing it by 1.So after the button is clicked on the webpage, the database status value is incremented by 1.
I have tried using AJAX for this, but I havent seemed to have much luck.
view.php (Where the table is populated from the database)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>View</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<?php
include 'php/connect.php';
include 'php/status.php';
function getStatus($num){
$status = "";
switch ($num) {
case ($num == 0):
$status = "Pending";
case ($num == 1):
$status = "Completed";
default:
$status = "Pending";
}
return $status;
}
?>
<div class="container">
<div class="row">
<div class="col-md-12 center-block">
<h1>View the Commissions</h1>
<div class="panel panel-default">
<div class="panel-heading">Commissions</div>
<div class="panel-body">
<p></p>
</div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Alias</th>
<th>Description</th>
<th>Price</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($connect, "SELECT * FROM orders") or die(mysql_error());
while ($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<th scope=\"row\">";
echo $row['orderID'];
echo "</th>";
echo "<th>".$row['alias']."</th>";
echo "<th>".$row['description']."</th>";
echo "<th>$".$row['price']."</th>";
echo "<th><button type=\"button\" class=\"btn btn-default btn-info\" name=\"" .$row['orderID']. "\">".getStatus($row['status'])."</button></th>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
</html>
As you can see, for each row in the database the table is populated. Here is an image of the webpage:
I did have this code for the AJAX. It did give me the correct output in the console of the browser, but I wasnt able to get this value into the process.php page
$.ajax({
url: 'php/process.php',
type: 'POST',
data: {
orderID: obj.id
},
success: function (data) {
console.log(data);
}
});
How would I have it so that as soon as the button is clicked, it updates the value and refreshes the page. Any help would be greatly appreciated
TLDR; When I click the "Pending" button, a value in the database is incremented by 1.
Your getStatus() function didn't have break statement. for simple POST use $.post. I have added .status class to your button to use it as selector with jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>View</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<?php
include 'php/connect.php';
include 'php/status.php';
// fixed this function by adding break and removing $num from case statement
function getStatus($num)
{
$status = "";
switch ($num) {
case 0:
$status = "Pending";
break;
case 1:
$status = "Completed";
break;
default:
$status = "Pending";
}
return $status;
}
?>
<div class="container">
<div class="row">
<div class="col-md-12 center-block">
<h1>View the Commissions</h1>
<div class="panel panel-default">
<div class="panel-heading">Commissions</div>
<div class="panel-body">
<p></p>
</div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Alias</th>
<th>Description</th>
<th>Price</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($connect, "SELECT * FROM orders") or die(mysql_error());
while ($row = mysqli_fetch_array($query)) : ?>
<tr>
<th scope="row"><?php echo $row['orderID']; ?></th>
<th><?php echo $row['alias']; ?></th>
<th><?php echo $row['description']; ?></th>
<th><?php echo $row['price']; ?></th>
<th>
// added .status class here
<button type="button" class="btn btn-default btn-info status"
name="<?php echo $row['orderID']; ?>"><?php echo getStatus($row['status']); ?></button>
</th>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
// the AJAX for updating the status
$('.status').on('click', function () {
var id = $(this).attr('name');
var btn = $(this);
$.post('php/process.php', {orderId: id}, function (response) {
var res = JSON.parse(response);
if (res.results == true) {
btn.html('Completed');
}
})
})
</script>
</html>
In php/process.php
<?php
include 'php/connect.php';
if (isset($_POST['orderId'])) {
$orderId = $_POST['orderId'];
$sql = "UPDATE `orders` SET `status`= 1 WHERE `orderID` = $orderId";
if (mysqli_query($connect, $sql)) {
echo json_encode(['results' => true]); // when update is ok
}else {
echo json_encode(['results' => false]); // when update fail
}
}
My current code has a checkbox sum function which takes values from database.
I am also trying to apply another Javascript code to my table that allows me to sort the data in the rows.
For example, after sorting the rows to display all 5 ACER assets on the top. i want tick the checkboxes of the top 5 displayed ACER assets and sum them.
This is my current code:
<?php
include('connect1.php');
$retrieve = $db->prepare("SELECT count(*) FROM Asset");
$retrieve->execute();
$fetchrow = $retrieve->fetch(PDO::FETCH_NUM);
$calculated=$fetchrow[0];
?>
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<<?php echo $calculated ?>; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>
<link rel="stylesheet" href="style.css" />
Total Cost : <input type="text" name="sen" id="totalcost" value="" />
<table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
<thead>
<tr>
<th><h3>Asset ID</h3></th>
<th><h3>Vendor</h3></th>
<th><h3>Hardware </h3></th>
<th><h3>Cost</h3></th>
<th><h3>Date Of Purchase</h3></th>
<th><h3>Select Values</h3></th>
</tr>
</thead>
<tbody>
<?php
// include('connect1.php'); you already included this file
$result = $db->prepare("SELECT * FROM Asset");
$result->bindParam(':userid', $res);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><?php echo $row['Asset_ID']; ?></td>
<td><?php echo $row['Vendor_Name']; ?></td>
<td><?php echo $row['Hardware_ID']; ?></td>
<td><?php echo $row['Asset_Cost']; ?></td>
<td><?php echo $row['DateOfPurchase']; ?></td>
<th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th>
</tr>
<?php
}
?>
</tbody>
</table>
<br />
<div id="controls">
<div id="perpage">
<select onchange="sorter.size(this.value)">
<option value="5">5</option>
<option value="10" selected="selected">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<span>Entries Per Page</span>
</div>
<div id="navigation">
<img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
<img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
<img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
<img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
</div>
<div id="text">Displaying Page <span id="currentpage"></span> of <span id="pagelimit"></span></div>
</div>
This is the Javascript :
var TINY={};
function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}
TINY.table=function(){
function sorter(n){this.n=n; this.pagesize=20; this.paginate=0}
sorter.prototype.init=function(e,f){
var t=ge(e), i=0; this.e=e; this.l=t.r.length; t.a=[];
t.h=T$$('thead',T$(e))[0].rows[0]; t.w=t.h.cells.length;
for(i;i<t.w;i++){
var c=t.h.cells[i];
if(c.className!='nosort'){
c.className=this.head; c.onclick=new Function(this.n+'.wk(this.cellIndex)')
}
}
for(i=0;i<this.l;i++){t.a[i]={}}
if(f!=null){var a=new Function(this.n+'.wk('+f+')'); a()}
if(this.paginate){this.g=1; this.pages()}
};
sorter.prototype.wk=function(y){
var t=ge(this.e), x=t.h.cells[y], i=0;
for(i;i<this.l;i++){
t.a[i].o=i; var v=t.r[i].cells[y]; t.r[i].style.display='';
while(v.hasChildNodes()){v=v.firstChild}
t.a[i].v=v.nodeValue?v.nodeValue:''
}
for(i=0;i<t.w;i++){var c=t.h.cells[i]; if(c.className!='nosort'){c.className=this.head}}
if(t.p==y){t.a.reverse(); x.className=t.d?this.asc:this.desc; t.d=t.d?0:1}
else{t.p=y; t.a.sort(cp); t.d=0; x.className=this.asc}
var n=document.createElement('tbody');
for(i=0;i<this.l;i++){
var r=t.r[t.a[i].o].cloneNode(true); n.appendChild(r);
r.className=i%2==0?this.even:this.odd; var cells=T$$('td',r);
for(var z=0;z<t.w;z++){cells[z].className=y==z?i%2==0?this.evensel:this.oddsel:''}
}
t.replaceChild(n,t.b); if(this.paginate){this.size(this.pagesize)}
};
sorter.prototype.page=function(s){
var t=ge(this.e), i=0, l=s+parseInt(this.pagesize);
if(this.currentid&&this.limitid){T$(this.currentid).innerHTML=this.g}
for(i;i<this.l;i++){t.r[i].style.display=i>=s&&i<l?'':'none'}
};
sorter.prototype.move=function(d,m){
var s=d==1?(m?this.d:this.g+1):(m?1:this.g-1);
if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.pagesize)}
};
sorter.prototype.size=function(s){
this.pagesize=s; this.g=1; this.pages(); this.page(0);
if(this.currentid&&this.limitid){T$(this.limitid).innerHTML=this.d}
};
sorter.prototype.pages=function(){this.d=Math.ceil(this.l/this.pagesize)};
function ge(e){var t=T$(e); t.b=T$$('tbody',t)[0]; t.r=t.b.rows; return t};
function cp(f,c){
var g,h; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase();
var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));
if(!isNaN(i)&&!isNaN(n)){g=i,h=n}
i=Date.parse(f); n=Date.parse(c);
if(!isNaN(i)&&!isNaN(n)){g=i; h=n}
return g>h?1:(g<h?-1:0)
};
return{sorter:sorter}
}();
this is the actual working sortable table:
<?php
include('connect1.php');
$retrieve = $db->prepare("SELECT count(*) FROM Asset");
$retrieve->execute();
$fetchrow = $retrieve->fetch(PDO::FETCH_NUM);
$calculated=$fetchrow[0];
?>
<script type="text/javascript">
function UpdateCost() {
var sum = 0;
var gn, elem;
for (i=0; i<<?php echo $calculated ?>; i++) {
gn = 'sum_m_'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost' ).value = sum.toFixed(0);
}
window.onload=UpdateCost
</script>
<input type="button" value="Search" onclick="window.location.href='http://mp04.bit-mp.biz/Ashvin/SearchACost.php'" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Costing</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
<thead>
<tr>
<th><h3>Asset ID</h3></th>
<th><h3>Vendor</h3></th>
<th><h3>Hardware </h3></th>
<th><h3>Cost</h3></th>
<th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th>
</tr>
</thead>
<tbody>
<?php
mysql_connect("localhost","user","pass");
mysql_select_db("db");
$result = mysql_query("SELECT * FROM Asset");
while($row = mysql_fetch_array($result))
for($i=0; $row = $result->fetch(); $i++){
{
echo "<tr>";
echo "<td>" . $row['Asset_ID'] . "</td>";
echo "<td>" . $row['Vendor_Name'] . "</td>";
echo "<td>" . $row['Hardware_ID'] . "</td>";
echo "<td>" . $row['Asset_Cost'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
</tbody>
</table>
<div id="controls">
<div id="perpage">
<select onchange="sorter.size(this.value)">
<option value="5">5</option>
<option value="10" selected="selected">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<span>Entries Per Page</span>
</div>
<div id="navigation">
<img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
<img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
<img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
<img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
</div>
<div id="text">Displaying Page <span id="currentpage"></span> of <span id="pagelimit"></span></div>
</div>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
var sorter = new TINY.table.sorter("sorter");
sorter.head = "head";
sorter.asc = "asc";
sorter.desc = "desc";
sorter.even = "evenrow";
sorter.odd = "oddrow";
sorter.evensel = "evenselected";
sorter.oddsel = "oddselected";
sorter.paginate = true;
sorter.currentid = "currentpage";
sorter.limitid = "pagelimit";
sorter.init("table",1);
</script>
</body>
</html>
I work in wordpress with WPMVC.
I am adding table rows dynamically in the form to add a record to the table.
Code:
views/admin/edit.php
<h2>Edit Geozone</h2>
<?php echo $this->form->create($model->name); ?>
<?php echo $this->form->input('geozone_name'); ?>
<?php echo $this->form->input('state'); ?>
<?php echo $this->form->input('ordering'); ?>
<?php echo $this->form->end('Update'); ?>
<!DOCTYPE html>
<html>
<head>
<link data-require="bootstrap#3.2.0" data-semver="3.2.0" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" />
<script data-require="bootstrap#3.2.0" data-semver="3.2.0" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js" data-semver="2.1.1" data-require="jquery#2.1.1"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<table class="table table-striped" id="geozoneRule">
<thead>
<tr>
<th colspan="3">
<input type="button" class="btn btn-success" value="Add Rule" onclick="addGeozoneRule();" />
</th>
</tr>
<tr>
<th>Country</th>
<th>Zone</th>
<th>Remove</th>
</tr>
</thead>
</table>
</body>
</html>
<script>
var count = 0;
function addGeozoneRule() {
var html = '';
html += '<tr>';
html += '<td><select name="rule[' + count + '][country_id]"><option value="99">India</option><option value="100">Indonesia</option></select></td>';
html += '<td><select name="rule[' + count + '][zone_id]"><option value="299">Chennai</option><option value="1100">some</option></select></td>';
html += '<td><span class="btn btn-danger" onclick="removeTr(this)">Remove <i class="icon icon-remove"></i></span></td>';
html += '</tr>';
jQuery("#geozoneRule").append(html);
count++;
}
function removeTr(element) {
if (count > 1) {
jQuery(element).closest('tr').remove();
count--;
} else {
alert('Keep one row');
}
</script>
I tested the code with plnkr.co and it was successful. But here in my application, it is not even entering the script. What could be the flaw in this code?
Solved the issue by removing the html start and end tags for:
'html' 'head' and 'body'
It works fine now.
Thanks all.
I tried to look into the similar topics but couldn't find a solution, done everything I learned but its giving me same error always, & here's the code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable();
} ); //this is where its pointing for error
</script>
I don't think I'm missing something, but there must be any other mistake...can anyone of you please point out ?
This is the error details:
Parse error: syntax error, unexpected '}', expecting ',' or ';' in /var/www/vhosts/tradeglobus.com/httpdocs/companies_list.php on line 41
#Alex: here's the full code
$isSeller = ((isset($PageType) && $PageType == "sellers") ? "1" : "0");
if(isset($searchType) && $searchType == "companies")
{
//--------- Show Products Here -----------//
$Query = clsCompanies::searchCompany($keyword, $Limit, $Offset, $countryID);
$companies_list = $objDatabase->runStoredProcedure($Query);
//----------------------------------------//
}
else
{
//--------- Show Products Here -----------//
$Query = clsCompanies::getCompaniesForDetailPages($IndustryId, $isSeller, $Limit, $Offset);
$companies_list = $objDatabase->runStoredProcedure($Query);
//----------------------------------------//
}
if(count($companies_list))
{
$Cnt = 0;
foreach($companies_list as $company)
{
$Cnt++;
echo '<tr>
<td ' . ((($Cnt % 2) == 0) ? "class='Even'" : "class='Odd'") . ' ">
<style type="text/css" title="currentStyle">
#import "http://www.tradeglobus.com/search/demo_page.css";
#import "http://www.tradeglobus.com/search/demo_table.css";
</style>
<script type="text/javascript" language="javascript" src="http://www.tradeglobus.com/search/jquery.js"></script>
<script type="text/javascript" language="javascript" src="http://www.tradeglobus.com/search/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
<body id="dt_example">
<div id="container">
<div id="demo">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th>Company</th>
<th>Description</th>
<th>Membership Status</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<!-- TD for Company Logo STARTS -->
<td rowspan="3" valign="top" width="75px" valign="middle" align="center">
<img src="' . clsPresent::checkImageExistance(BASE_HOST . DEFAULT_COMPANY_LOGO_PATH . $company['company_logo_thumb']) . '" width="75px" height="75px" alt="productImage" />
</td>
<!-- TD for Company Logo ENDS -->
<!-- TD for Company Name & Description STARTS -->
<td rowspan="2" width="450px" align="left" style="line-height: 15px;">
<h4 ><a style="font-size:16px;" href="' . BASE_HOST . 'companies_details/' . $company['MemberID'] . '/' . clsPresent::removeSpecialChars($company['company_name']) . '.htm">' . $company['company_name'] . '
</a></h4><br/>' . ((strlen($company['company_introduction']) > 165) ? substr(strip_tags($company['company_introduction']), 0, 162) . '...' : $company['company_introduction']) .'
</td>
<!-- TD for Company Name & Description ENDS -->
<!-- TD for Company Flag & Package STARTS -->
<td valign="top" >
<div style="float: left; line-height: 25px;"><div class="flags f_' . $company['CountryID'] . '"></div> ' . $company['Country'] .'</div><br/>
</td>
<!-- TD for Company Flag & Package ENDS -->
<td class="center"><img src="' . BASE_HOST . DEFAULT_PACKAGE_IMAGE_PATH . $company['imagePath'] . '" alt="Package Image" width="101px" height="24px" /></td>
</tr>
</table>
<tr>
<td valign="top" style="line-height: 15px;">
<div>';?>
<?php
if(isset($_SESSION['userID']) && $_SESSION['userID'] != ""){ ?>
<a style="vertical-align: top;" class="contactNow" href="<?php echo BASE_HOST; ?>contactmember.php?to=<?php echo $company['MemberID']; ?>&from=<?php echo $_SESSION['userID']; ?>&subject=Contact Request"></a>
<?php } else { ?>
<a style="vertical-align: top;" class="contactNowB" href="<?php echo BASE_HOST; ?>signin.php"></a>
<?php }
echo '</div>
</td>
</tr>
</table>
</td>
</tr>';
}
echo '<tfoot>
<tr>
<th>';
if(isset($PageType) && $PageType != ""){
echo' <span style="float: left; padding-left: 10px;">' . (( $PageNo > 1 ) ? '<-- Back' : "" ) . '</span>
<span style="float: right; padding-right: 10px;">Next --></span>';
}else{
echo' <span style="float: left; padding-left: 10px;">' . (( $PageNo > 1 ) ? '<-- Back' : "" ) . '</span>
<span style="float: right; padding-right: 10px;">Next --></span>';
}
echo ' </th>
</tr>
</tfoot>';
}
else
{
echo '<tr>
<td align="center" valign="top">
<h1 style="font-size: 16px; color: #003466;">No companies were found for this category.</h1>
</d>
</tr>';
}
?>
You need to quote out '
$('#example').dataTable();
Should be
echo '<tr>
/*...further code...*/
$(\'#example\').dataTable();
';
Since its inside a PHP string.
You have no error in your JavaScript code. JavaScript does not compile, it gets interpreted by the browser. You won't see Parse error displayed in the page for JavaScript, you'll see errors logged to the browser console (WebInspector/Firebug etc.).
Your error is somewhere in your PHP code, and I doubt that the line number is pointing to the correct location. If you need more help debugging that, post the entire PHP source code in your question.