Javascript function not defined when called on input onclick() - web

I keep getting a
ReferenceError: del is not defined.
Before it was working where it would refresh the page, but I set it up so that it would only refresh the div when clicking.
while($row = $result->fetch_assoc()) {
$fileID = $row['fileID'];
echo "<tbody><tr>";
echo "
<td bgcolor='white'>
<input type='checkbox' checked>
</td>
<td bgcolor='white'>
{$row['name']}
</td>
<td bgcolor='white'>
{$row['type']}
</td>
<td bgcolor='white'>
{$row['size']}
</td>
<td bgcolor='white'>
{$row['created']}
</td>
<td>
<input type='button' name='download' value='Download' onclick='window.location="get_file.php?fileID=$fileID";'>
</td>
<td>
<script type='text/javascript'>
function del() {
console.log('Im working!');
var r = confirm('Do you want to remove this file?');
if (r == true) {
$.ajax({url: 'delete.php',
data: {action: '<?php print $fileID;?>'}
type: 'post';
success: function() {
$('#fileTable').load(document.URL + ' #fileTable');
}
});
}
}
</script>
<input type='button' name='delete' value='Delete' onclick='del()'>
</td>

You're duplicating your function for every row, and when you press a button, the function does not know what ID you're after.
Change it to something like:
echo <<<EOHTML
<script type='text/javascript'>
function del(id) {
console.log('Im working!', id);
var r = confirm('Do you want to remove this file?');
if (r == true) {
$.ajax({url: 'delete.php',
data: {action: id.toString()}
type: 'post';
success: function() {
$('#fileTable').load(document.URL + ' #fileTable');
}
});
}
}
</script>
EOHTML;
while($row = $result->fetch_assoc()) {
$fileID = $row['fileID'];
echo <<<EOHTML
<tbody><tr>
<td bgcolor='white'>
<input type='checkbox' checked>
</td>
<td bgcolor='white'>
{$row['name']}
</td>
<td bgcolor='white'>
{$row['type']}
</td>
<td bgcolor='white'>
{$row['size']}
</td>
<td bgcolor='white'>
{$row['created']}
</td>
<td>
<input type='button' name='download' value='Download' onclick='window.location="get_file.php?fileID={$fileID}";'>
</td>
<td>
<input type='button' name='delete' value='Delete' onclick='del({$fileID})'>
</td>
EOHTML;
}

Related

NextSibling property raises Object variable or With block variable not set

I have a working code on my side in which I have used NextSibling property in excel vba like that
stExecutive = thElements.Item(i).NextSibling.NextSibling.innerText
When a friend takes the code to try it on his side, the code raises the error Object variable or With block variable not set and I have fixed it using just one NextSibling
stExecutive = thElements.Item(i).NextSibling.innerText
Why this difference happen? and how can I make the code work on both sides without raising any errors?
This is part of the code
Set thElements = htmlCaseProcedures.querySelector(".table-striped").getElementsByTagName("th")
For i = 0 To thElements.Length - 1
If thElements.Item(i).innerText = sAvailableBalance Then
stAvailableBalance = thElements.Item(i).NextSibling.innerText
ElseIf thElements.Item(i).innerText = sFileStatus Then
stFileStatus = thElements.Item(i).NextSibling.innerText
ElseIf thElements.Item(i).innerText = sExecutive Then
stExecutive = thElements.Item(i).NextSibling.innerText
End If
Next i
and this is the content of html
<script type="text/javascript">
function doSubmitP(txtType){
document.frmTemp.reqtype.value = txtType;
$('#frmTemp').submit();
}
</script>
<style type="text/css">
th,td{
text-align: center !important;
}
</style>
<table class="table table-striped">
<tbody><tr align="center">
<th width="50%" align="right">الرقم الآلي</th>
<td><small>192833700</small></td>
</tr>
<tr align="center">
<th align="right">تاريخ التسجيل فى ملف التنفيذ</th>
<td><small>2020-03-09</small></td>
</tr>
<tr align="center">
<th align="right">رقم ملف التنفيذ</th>
<td><small>19283370</small></td>
</tr>
<tr align="center">
<th align="right">جهة التنفيذ</th>
<td><small>العاصمة-قسـم التنفيذشركات</small></td>
</tr>
<tr align="center">
<th align="right">الرصيد المتاح</th>
<td><small>0.000 د.ك</small></td>
</tr>
<tr align="center">
<th align="right">حالة الملف</th>
<td><small>مفتـــــوح</small></td>
</tr>
<tr>
<td align="center" colspan="2">
<table align="center" class="linksTbl">
<tbody><tr valign="middle">
<td><a class="hyperText" onclick="doSubmitP(1)" href="javascript:void(0);">الإجراءات العامة والرسوم</a></td>
<td>|</td>
<td><a class="hyperText" onclick="doSubmitP(2)" href="javascript:void(0);">إجراءات التنفيذ المدني</a></td>
<td>|</td>
<td><a class="hyperText" onclick="doSubmitP(3)" href="javascript:void(0);">الإجراءات المالية</a></td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
<div class="row" id="viewPaneDetails" style="display: none;"></div>
<form name="frmTemp" id="frmTemp" action="execReqView.jsp" method="POST" autocomplete="off">
<input name="reqtype" type="hidden">
</form>
<script>
$('#frmTemp').submit(function(){ // catch form submit event
$.ajax({ // create ajax call
data: $(this).serialize(), //get form data
type: $(this).attr('method'), //GET or POST
url: $(this).attr('action'), // the action url
success: function(response){ // on success
$('#viewPaneDetails').html(response); //update DIV
$('#viewPaneDetails').show();
}
});
return false; // cancel original event to prevent form submitting
});
</script>

How do I parse this html with Python lxml & xpath that finds the parent table of a specific span id?

Here is the HTML I don't have any control over. This is condensed HTML of the real page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Little League</title>
</head>
<body>
<table>
<span>lot of unrelated text</span>
</table>
<table>
<span>lot of unrelated text</span>
</table>
<table>
<span>lot of unrelated text</span>
</table>
<table>
<tbody>
<tr>
<td class="rightTD">
<p>
<span id="teams_players">Player Teams</span>
</p>
</td>
</tr>
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="0" class="tableBorder table table-bordered" width="100%">
<tbody>
<tr>
<td>
<table border="0" width="100%" class="tableData">
<tbody>
<tr id="team_listings">
<td colspan="3">Team Listings
<br>
<br>
</td>
</tr>
<tr>
<td>(a) </td>
<td colspan="2">Team Name </td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">Foxes</span>
</td>
</tr>
<tr>
<td>(b) </td>
<td colspan="2">Team Rank</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">1</span>
</td>
</tr>
<tr>
<td>(c) </td>
<td colspan="2">Team Location
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<table width="100%">
<tbody>
<tr>
<td>City:
<br>
<span class="blue_color">Tualatin</span>
</td>
<td>State:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">Oregon</span>
</td>
<td>Country:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">United States</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<br>
<table border="1" cellspacing="0" cellpadding="0" class="tableBorder table table-bordered" width="100%">
<tbody>
<tr>
<td>
<table border="0" width="100%" class="tableData">
<tbody>
<tr>
<td>(a) </td>
<td colspan="2">Team Name </td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">Tigers</span>
</td>
</tr>
<tr>
<td>(b) </td>
<td colspan="2">Team Rank</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">3</span>
</td>
</tr>
<tr>
<td>(c) </td>
<td colspan="2">Team Location
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<table width="100%">
<tbody>
<tr>
<td>City:
<br>
<span class="blue_color">Tigard</span>
</td>
<td>State:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">Oregon</span>
</td>
<td>Country:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">United States</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
I am trying to get to the table tag immediately preceding the span tag with id team_players.
I tried these but failed -
//table/span[#id="teams_players"]
ancestor::table[span[#id="teams_players"][position() = 1]]
This works but is not elegant and I prefer not to hardcode it -
//span[#id="teams_players"]/../../../../..
While //table[#class="tableData"] this might seem like it should work, there are many such tables in the HTML that has the same class with unrelated data. So this is ruled out.
Here is the code so far with my attempts (definitely not efficient, once I find a way of fetching both tables, I plan on looping through them to extract the data -
def parse_team():
# team data structure
teams = []
team_dict = { 'team': '', 'rank': '', 'location': { 'city': '', 'state': '', 'country': '' } }
filename = f'team.html'
f = open(filename, encoding="utf8").read()
parser = etree.HTMLParser()
tree = etree.parse(StringIO(f), parser)
# fetch the table dom and parse each team table
# fetch the parent table that contains teams_players span id
team_tables = tree.xpath('ancestor::table[span[#id="teams_players"][position() = 1]]')
print(team_tables)
root_tables = tree.xpath('//table/span[#id="teams_players"]')
print("root tables", root_tables)
# this provides each team table but in full html, the same class is being used for other unrelated data
name = tree.xpath('//table[#class="tableData"]')
print(name)
eachvaltr = name[0].xpath('.//tr')
teamname = name[0].xpath('.//td[contains(text(),"Team Name")]//parent::tr/following-sibling::tr[1]//span[#class="blue_color"]/text()')
print("teamname", teamname)
teamrank = name[0].xpath(
'.//td[contains(text(),"Team Rank")]//parent::tr/following-sibling::tr[1]//span[#class="blue_color"]/text()')
print("teamrank", teamrank)
city = name[0].xpath(
'.//td[contains(text(),"City")]//span[#class="blue_color"]/text()')
state = name[0].xpath(
'.//td[contains(text(),"State")]//span[#class="blue_color"]/text()')
country = name[0].xpath(
'.//td[contains(text(),"Country")]//span[#class="blue_color"]/text()')
print(city[0], state[0], country[0])
team_dict['team'] = teamname
team_dict['rank'] = teamrank
team_dict['location']['city'] = city[0]
team_dict['location']['state'] = state[0]
team_dict['location']['country'] = country[0]
print(team_dict)
Desired output is a list of teams where each team is a dict.
[{'team': ['Foxes'], 'rank': ['1'], 'location': {'city': 'Tualatin', 'state': 'Oregon', 'country': 'United States'}}]
//table[.//span[#id="teams_players"]]
or
//span[#id="teams_players"]/ancestor::table

Loop Inside node.js module

Here is my module.
module.exports = function (requestUser) {
let content = `
<div id="cpl">
<table id="currentpatientslists">
</table>
</div>
<div id="rp">
<h1>Requested Patients</h1>
</div>
<hr>
<div id="note" >Currently no patients</div>
<div id="rpl">
<table id="requestedpatientslists">
<tr>
<td width="30%"></td>
<td width="30%" class="right"><button>Accept</button></td>
<td width="30%" class="left"><button>Reject</button></td>
</tr>
</table>
</div>`;
return render(content);
}
In the requestedpatientslists table , I want to loop the data in the table row coming from requestUser which is an array. I want to loop it until requestUser.length. How can I do that?
You just need to loop over the users and create the rows for them first
module.exports = function(requestUser) {
// I'm guessing that the user has normal properties like name, etc?
const tableRows = requestUser.map(
user => `
<tr>
<td width="30%">${user.name}</td>
<td width="30%" class="right"><button>Accept</button></td>
<td width="30%" class="left"><button>Reject</button></td>
</tr>
`,
);
const content = `
<div id="cpl">
<table id="currentpatientslists">
</table>
</div>
<div id="rp">
<h1>Requested Patients</h1>
</div>
<hr>
<div id="note" >Currently no patients</div>
<div id="rpl">
<table id="requestedpatientslists">
${tableRows.join('\n')}
</table>
</div>`;
return render(content);
};

How to display specific data in ejs from mongoose?

I am new to NodeJS and MongoDB .I want to specific data from my database using mongoose . Those specific data are decided by a searchIndex and searchValue which is provided as a post request
The ejs page which provides the searchIndex and searchValue
<form action="/searchDetails" method="post">
<table>
<tr>
<td>
Select Search Category:
</td>
<td>
<select name="searchIndex">
<option>Name</option>
<option>Rollno</option>
<option>Branch</option>
<option>Hostel</option>
<option>RoomNo</option>
<option>ContactNo</option>
<option>Semester</option>
<option>FName</option>
<option>MName</option>
<option>Address</option>
</select>
</td>
</tr>
<tr>
<td>Enter the Search Value:</td>
<td><input type="text" name="searchValue"></td>
</tr>
<tr>
<td><input type="Submit" name="Search"></td>
</tr>
</table>
</form>
This is my mongoose schema
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
username: String,
password: String,
});
var studentSchema=new mongoose.Schema({
Name:String,
Rollno:String,
Branch:String,
Hostel:String,
RoomNo:String,
ContactNo:String,
Semester:String,
FName :String,
MName:String,
Address:String,
});
mongoose.model('Students3', studentSchema);
mongoose.model('User', userSchema);
this is my server.js
router.post('/searchDetails', function(req, res, next) {
var searchIndex=req.body.searchIndex;
var searchValue=req.body.searchValue;
students.find({searchIndex:searchValue},function(err,data){
if(err)
{
console.log(err);
}
else if(!data)
{
console.log('no data');
res.redirect('/homepage');
}
else
{
console.log(data);
output=data;
res.redirect('/editStudents')
router.get('/editStudents',function(req,res,next){
res.render('editStudents',{output});
});
}
});
});
this is my ejs where i display the value
<table border="1">
<tr>
<td>
<i>Name</i>
</td>
<td>
<i>Rollno</i>
</td>
<td>
<i>Branch</i>
</td>
<td>
<i>Hostel</i>
</td>
<td>
<i>RoomNo</i>
</td>
<td>
<i>ContactNo</i>
</td>
<td>
<i>Semester</i>
</td>
<td>
<i>Father's Name</i>
</td><td>
<i>Mother's Name</i>
</td><td>
<i>Address</i>
</td>
</tr>
<%
output.forEach(function(name)
{
%>
<tr>
<td><%=name.Name%></td>
<td><%=name.Rollno%></td>
<td><%=name.Branch%></td>
<td><%=name.Hostel%></td>
<td><%=name.RoomNo%></td>
<td><%=name.ContactNo%></td>
<td><%=name.Semester%></td>
<td><%=name.FName%></td>
<td><%=name.MName%></td>
<td><%=name.Address%></td>
</tr>
<%
})
%>
</table>
But the problem is it doesnot display any data.please help

change color value with colorpicker

How do I change the bgcolor of 'colorId'. I tried the code below and I want the bgcolor to change in the value 'val'. But from then on I m doing something wrong.
<script type="text/javascript">
function updatevariable(elm) {
val = elm;
var divElement = document.getElementById(colorId);
divElement.bgcolor = val;
}
</script>
<table width="150" border="0" cellspacing="1" cellpading="0" align="center">
<tr>
<td bgcolor="#190707" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#fa5858" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#F4FA58" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#00FF00" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#fbefef" onclick="updatevariable(this.bgColor)"> </td>
</tr>
<tr>
<td id='colorId' bgcolor="#F4FA58"> </td>
</tr>
</table>
If you switch to jQuery you can do this in stead:
function updatevariable(elm){
var val = elm;
$('#colorId').css('background-color', val);
}
Just found your bugs. Use code below and it would work.
function updatevariable(elm) {
var val = elm;
var divElement = document.getElementById("colorId");
divElement.setAttribute("bgcolor", val);
}

Resources