I am currently coding for sorting of names in a table column. When the user clicks the header(link to js func), the table will the names. However, I am having some trouble. I get a notice of an undefined index(which I know many people do and I have searched online for it) which means that the variable is not declared. I can't seem to understand why though.
I did achieve my desired outcome, which is when the end user clicks the delete button(that is available for each row/record) it will delete the record(execture the sql query of delete) in the database. I am currently debugging to find out what's wrong but it'd be great if I'd had some help.
Note: If you notice, I did not use and jQuery or jQuery plugins and I know it will make it easier for me to use but I am a student learning so I want to start or rather use and hone my JavaScript coding so I prefer JavaScript. Besides, I tried learning jQuery once and I found it difficult(ironically). Don't worry about SQL injections as well.
Coding Note: displayTable() is called when a button in my html file(not using any forms) is clicked. Take note of the windows.onload
function I have used for it. Everything is done in a new pop up window.
EDIT: I added which line the undefined index was at.
2nd EDIT: Added in the action
because I forgot to put it in this question, pointed out by @Barmar.
This is in the PHP file:
<?php
// Define database parameters //
DEFINE ('DB_USER' ,'iaqwgvaqn');
DEFINE ('DB_PASSWORD', 'qawf23');
DEFINE ('DB_HOST', 'lqwfqwt');
DEFINE ('DB_NAME', 'hqwfcaqwq');
// Connect to database
$conn = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to Database:'. mysql_error());
@mysql_select_db (DB_NAME) OR die ('Could not select the Database: '.mysql_error());
function selectAll($enableSorting)
{
$table_info = "dummyTest";
if($enableSorting == "sortingEnabled")
{
$query_string = "select * from $table_info order by name";
}
else
{
$query_string = "select * from $table_info";
}
$result = @mysql_query($query_string) or die (mysql_error());
$num_row = mysql_num_rows($result);
if($num_row)
{
$count = 0;
echo "<table id='table2' class='table2' border=1>";
//Table headers
echo "<tr><th>ID</th>";
echo "<th><a href=\"javascript:sortTable();\">Name</a></th>";
echo "<th>Badge Number</th>";
echo "<th>Category</th>";
echo "<th>Action</th>";
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$name = $row['name'];
$badge_number = $row['badge_number'];
$category = $row['category'];
$privilege = $row['privilege'];
$count++;
echo "<tr>";
echo "<td id=\"row$count\">$id</td>";
echo "<td>$name</td>";
echo "<td>$badge_number</td>";
echo "<td>$category</td>";
echo "<td><input type=\"button\" name=\"delete\" value=\"Delete\" onclick=\"deleteThis($count, $privilege)\"/></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No records available. Click 'Add User' to add new user.";
}
}
function deleteRow($id)
{
$table_info = "dummyTest";
$query_string = "delete from $table_info where id='$id'";
$result = @mysql_query($query_string) or die (mysql_error());
}
.
.
//"INSERT QUERY" function, not relevant
.
.
<?php
$action = rtrim($_REQUEST['action']);
$enableSorting = $_REQUEST['enableSorting']; //Here is where I got undefined index, but it only shows up when I delete a record
if($action=="delete")
{
$id = rtrim($_REQUEST['id']);
echo deleteRow($id);
echo selectAll($enableSorting);
}
elseif($action=="insert")
{
$name = $_REQUEST['name'];
$badge_number = $_REQUEST['badge_number'];
$privilege = $_REQUEST['privilege'];
$category = $_REQUEST['category'];
echo insertRow($name, $badge_number, $privilege, $category);
}
elseif($action == "update")
{
echo selectAll($enableSorting);
}
?>
And here are the relevant codes in my external javascript file:
function displayTable()
{
window.onload = function()
{
var page = "database.php"
var parameters = "enableSorting=sortingDisabled&action=update";
var xmlhttp = new XMLHttpRequest();
if(xmlhttp==null)
{
alert("Your browser does not support AJAX!");
return false;
}
xmlhttp.onreadystatechange=function()
{
document.getElementById("divTable").innerHTML=xmlhttp.responseText;
};
xmlhttp.open("GET", page+"?"+parameters, true);
xmlhttp.send(null);
}
}//displayTable()
function sortTable()
{
var page = "database.php";
var parameters = "enableSorting=sortingEnabled&action=update";
var xmlhttp = new XMLHttpRequest();
if(xmlhttp==null)
{
alert("Your browser does not support AJAX!");
return false;
}
xmlhttp.onreadystatechange=function()
{
document.getElementById("divTable").innerHTML=xmlhttp.responseText;
};
xmlhttp.open("GET", page+"?"+parameters, true);
xmlhttp.send(null);
}//sortTable(sort_key)
function deleteThis(count, privilege)
{
var id = document.getElementById("row"+count).innerHTML;
var page = "database.php";
var parameters = "id="+id+"&action=delete";
var xmlhttp = new XMLHttpRequest();
if(confirm('Are you sure you want to delete this?')==true)
{
if(privilege==1)
{
alert("You cannot delete a Super Admin!");
return false;
}
else
{
if(xmlhttp==null)
{
alert("Your browser does not support ajax!");
return false;
}
xmlhttp.onreadystatechange=function()
{
document.getElementById("divTable").innerHTML=xmlhttp.responseText;
};
xmlhttp.open("GET", page+"?"+parameters, true);
xmlhttp.send(null);
}
}
else
{
return false;
}
}//deleteThis(count)