How to display mysql data in different HTML elements by using AJAX - php

I'm new to web development and I'm following a couple of tutorials. I'm learning that how to display mySQL data into HTML by using HTML, PHP, jQuery and AJAX. Below example is my practice work where I'm successfully loading data into HTML table.
<table id="main" border="1px" cellspacing="0">
<tr>
<td id="header">
<h1>PHP with AJAX</h1>
</td>
</tr>
<tr>
<td id="table-form">
<form id="addform">
Student First Name: <input type="text" id="st-first-name">
Student Last Name: <input type="text" id="st-last-name">
<input type="submit" value="Save Data" id="save-data"><br>
</form>
</td>
</tr>
<tr>
<td id="table-data">
<table border="1px" width="100" cellspacing="0px" cellpadding="10px">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
<td align="center">1</td>
<td>Abid Durrani </td>
</tr>
</table>
</td>
</tr>
</table>
Here is my AJAX Code.
$("document").ready(function(){
function loadData(){
$.ajax({
url: "ajaxload.php",
type: "post",
success: function(data){
$("#table-data").html(data);
}
});
} });
And this is my ajaxload.php code:
$myConn = mysqli_connect("localhost", "root", "", "test") or die("Your Connection Failed");
$sqlQuery = "select * from students";
$result = mysqli_query($myConn, $sqlQuery) or die ("Your Query Execution Failed");
$output = "";
if(mysqli_num_rows($result) > 0) {
$output = "
<table border='2' cellpadding='5px' cellspacing='5px'>
<tr>
<th width='100px'>ID</th>
<th>Name</th>
<th width='100px'>Delete</th>
</tr>";
while($row = mysqli_fetch_assoc($result)){
$output .= "
<tr>
<td>{$row['st_id']}</td>
<td>{$row['st_first_name']}</td>
<td><button class='btn-delete' data-myid={$row['st_id']}>Delete</button></td>
</tr>";
}
$output .= "</table>";
mysqli_close($myConn);
echo $output;
} else
{
echo "sorry, no record found";
}
Question: Here my data is directly loading into HTML table through its css ID. What I want is to set my database data into a textBox and a dropDownMenu along with this table. For table, I've created the table in ajaxload.php but what do I need to do for dropDownMenu and textBox?
Thanks.

Related

Add a button with elseif $row?

I need to place a button that says "Yes", but I need that when clicking on the button take the user to a web page, here is the php code for them to review:
<table cellpadding="1" cellspacing="1" id="member">
<thead>
<tr>
<th colspan="10">Players Not Activated</th>
</tr>
<tr>
<td class="on">#</td>
<td class="on">ID</td>
<td class="on">Username</td>
<td class="on">Email</td>
<td class="on">Tribe</td>
<td class="on">Activation Code</td>
<td class="on">Act 2??</td>
<td class="on">Time</td>
<td class="on">PLAY</td>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM ".TB_PREFIX."activate";
$result = mysqli_query($GLOBALS["link"], $sql);
while($row = mysqli_fetch_assoc($result))
{
$i++;
if($row['tribe'] == 1) {$tribe = "Roman"; }
elseif($row['tribe'] == 2) {$tribe = "Teuton"; }
elseif($row['tribe'] == 3) {$tribe = "Gaul"; }
echo "
<tr>
<td>".$i."</td>
<td>".$row['id']."</td>
<td>".$row['username']."</td>
<td>".$row['email']."</td>
<td>".$tribe."</td>
<td>".$row['act']."</td>
<td>".$row['act2']."</td>
<td class=\"hab\">".date('d:m:Y H:i', $row['timestamp'])."</td>
</tr>";
}
?>
</tbody>
</table>
I need the button to appear where it says "PLAY"
and the destination url i want is this link:
<a href="activate.php?id=<?php echo $_GET['id']; ?>
I have tried 4 days ago to be able to make it work but whenever I modify the file I receive a blank error page.

Php MysQl Search script returns blank page

I have written a simple page script to loop through a dynamic table that gets data from the database. But when I click on search the page returns blank with no error. I'm trying to understand what's going on without success.
display_table.php
<?php
include('session.php');
if ($_SESSION['login_user']){
include 'includes/header.php';
$searchQ = "SELECT * FROM companytable";
if(isset($_POST['search'])){
$search_term = mysqli_real_escape_string($db, $_POST['search_box']);
$searchQ .="WHERE title ='{$search_term}' ";
$searchQ .="OR country ='{$search_term}' ";
$searchQ .="OR description ='{$search_term}' ";
$searchQ .="OR timezone ='{$search_term}' ";
}
$query = mysqli_query($db, $searchQ) or die(mysqli_error());
}
form
<form class="form" name="search_form" method="POST" action="display_table.php">
<input id="search_box" style="padding: 2px;" class="" type="text" name="search_box">
<input class="btn btn-default" type="submit" name="search" value="🔍">
</form>
table
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</tr>
<?php while($company=mysqli_fetch_array($result)){ ?>
<tr>
<td data-th="ID"><?=$company['id'];?></a></td>
<td data-th="Name"><?=$company['title'];?></td>
<td data-th="Description"><?=$company['description'];?></td>
<td data-th="Type"><?=$company['type'];?></td>
<td data-th="Address"><?=$company['address'];?></td>
<td data-th="Country"><?=$company['country'];?></td>
<td data-th="Time Zone"><?=$company['timezone'];?></td>
</tr>
<?php };?>
</table>
You need to change
<?php while($company=mysqli_fetch_array($result)){ ?>
to reference the mysqli result you created, $query:
<?php while($company=mysqli_fetch_array($query)){ ?>
You can use object-oriented
<?php
include('session.php');
if ($_SESSION['login_user']){
include 'includes/header.php';
$query = "SELECT * FROM companytable ";
if(isset($_POST['search_box'])){
$search_term = $db->real_escape_string($_POST['search_box']);
$query.=" WHERE title ='{$search_term}'
OR country ='{$search_term}'
OR description ='{$search_term}'
OR timezone ='{$search_term}';";
}
if(!$s = $db->query($query)){
die($db->error);
}
}
Table
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</tr>
<?php while($m = $s->fetch_object()){ ?>
<tr>
<td data-th="ID"><?=$m->id;?></a></td>
<td data-th="Name"><?=$m->title;?></td>
<td data-th="Description"><?=$m->description;?></td>
<td data-th="Type"><?=$m->type;?></td>
<td data-th="Address"><?=$m->address;?></td>
<td data-th="Country"><?=$m->country;?></td>
<td data-th="Time Zone"><?=$m->timezone;?></td>
</tr>
<?php
};
$s->free();
?>
</table>
So turn out I decided to go with sortable.js It not only takes care of search but also takes care of sorting the rows by clicking on headers. So if you are looking for a clean fix, that would be it.
form
<form class="form" name="search_form"> <input id="search" style="" class="form-control" type="text" name="search" placeholder="🔍 Search...">
table
<table id="table" class="sortable" >
<thead style="cursor:pointer;">
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</thead>
<tbody>
<?php while($company=mysqli_fetch_array($result)){ ?>
<tr>
<td data-th="ID" sorttable_customkey="2"><?=$company['id'];?></a></td>
<td data-th="Name"><?=$company['title'];?></td>
<td data-th="Description"><?=$company['description'];?></td>
<td data-th="Type"><?=$company['type'];?></td>
<td data-th="Address"><?=$company['address'];?></td>
<td data-th="Country"><?=$company['country'];?></td>
<td data-th="Time Zone"><?=$company['timezone'];?></td>
</tr>
<?php };?>
</tbody>
<tfoot></tfoot>
</table>
Search script
<script src="js/sorttable.js"></script> //load sortable.js
<script type="text/javascript">
var $search_rows = $('#table tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$search_rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
</script>

How to add value to existing value using MySQL?

hi dear friends yesterday I asked about a teachers rating system but did not get any idea please help me. i am making a teacher rating system where i want to rate every teacher by making text boxes in front of the name of each teacher .the number that the user will add in text box for a specific teacher is to be added to a current rate of teacher.but the code is not working properly.when i add values through text boxes it adds the value from the last text box to all rows.for example for the first teacher i want to add 2 ,for the second i want to add 4 and for the 3rd teacher i want to rate 3. so instead of 2 and 3 all the rating of all teachers are incremented by 3.how to solve the problem?please help i am giving code of 2 pages m.php and n.php
m.php
<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
<th class="style5">Teacher ID</th>
<th width="90" class="style5">Teacher Name</th>
<th width="127" class="style5">Teacher Registration</th>
<th width="135" class="style5">Teacher Qualification</th>
<th width="92" class="style5">Teacher Subject</th>
<th width="92" class="style5">Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$id=$row['tid'];
?>
<tr>
<td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
<td align="center" class="style5"><?php echo $row['tname'];?></td>
<td align="center" class="style5"><?php echo $row['treg'];?></td>
<td align="center" class="style5"><?php echo $row['qualification'];?></td>
<td align="center" class="style5"><?php echo $row['subject'];?></td>
<td align="center"> <input type="text" name="rating">
</td>
</tr>
<?php
}
}else{
echo "<center><p><font size=10/> No Records</p></center>";
}
$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
n.php
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project") or die ("couldnt connnect to database");
include 'conn.php';
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM teacher";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_array()){
$id=$row['tid'];
$newvalue=$_POST['rating'];
$sql_update="UPDATE teacher set hits = hits +$newvalue where tid=".$id."";
mysql_query($sql_update) or die(mysql_error());
header("location:m.php");
}
}
}
Like this ... give rating name array with id from table and update the record with the right value and id ... but never do this way for username and password case ...
<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
<th class="style5">Teacher ID</th>
<th width="90" class="style5">Teacher Name</th>
<th width="127" class="style5">Teacher Registration</th>
<th width="135" class="style5">Teacher Qualification</th>
<th width="92" class="style5">Teacher Subject</th>
<th width="92" class="style5">Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$id=$row['tid'];
?>
<tr>
<td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
<td align="center" class="style5"><?php echo $row['tname'];?></td>
<td align="center" class="style5"><?php echo $row['treg'];?></td>
<td align="center" class="style5"><?php echo $row['qualification'];?></td>
<td align="center" class="style5"><?php echo $row['subject'];?></td>
<td align="center"> <input type="text" name="rating[<?php echo $id; ?>]">
</td>
</tr>
<?php
}
}else{
echo "<center><p><font size=10/> No Records</p></center>";
}
$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project") or die ("couldnt connnect to database");
include 'conn.php';
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM teacher";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_array()){
$id=$row['tid'];
$newvalue=$_POST['rating'];
if(isset($newvalue[$id])) {
$new_temp_value = $newvalue[$id];
$sql_update="UPDATE teacher set hits = hits +$new_temp_value where tid=".$id."";
mysql_query($sql_update) or die(mysql_error());
}
}
}
header("location:m.php");
}

search data from database with multiple textbox using php

search with multiple textbox using php
I am try this code for search with multiple textbox using php and i am using orcle database but this code is not work. and any one let me know what is the problem why this code not search with multiple text box field and also i have getting error Warning: oci_execute(): ORA-00920: invalid relational operator in
plz check .....my code..
<form action="Optr_Search.php" method="get">
<table width="500" border="0" align="center">
<tr>
<td width="203"><div align="right"><strong>Operator ID:</strong>
</div></td>
<td width="148"><input type="text" name="OPRID" /></td>
</tr>
<tr>
<td><div align="right"><strong>Operator Name:</strong></div></td>
<td><input type="text" name="OPRDEFNDESC" /></td>
</tr>
<tr>
<td><div align="right"><strong>Person ID:</strong></div></td>
<td><input type="text" name="EMPLID" /></td>
</tr>
<tr>
<td><div align="right"><strong>Email ID:</strong></div></td>
<td><input type="text" name="EMAILID" /></td>
</tr>
<tr>
<td colspan="2">
<div align ="center">
</br>
<input type="submit" align ="middle"value="Search" name ="submit" />
</div>
</td>
</tr>
</table>
</form>
<?php
$ora_conn = oci_connect('system','oracle','//localhost/XE');
if(!$ora_conn)
{
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else
{
print "You are connected to the database!<br/>";
}
if(isset($_POST['submit']))
{
$optid = $_POST['OPRID'];
$optdec = $_POST['OPRDEFNDESC'];
$empid = $_POST['EMPLID'];
$empmail = $_POST['EMAILID'];
$query = "SELECT * FROM OPERATOR WHERE 1";
if(isset($optid) && $optid != '') {
$query .= " And OPRID Like'%$optid%'";
}
if(isset($optdec) && $optdec != '') {
$query .= " OR OPRDEFNDESC Like'%$optdec%'";
}
if(isset($empid) && $empid != '') {
$query .= " OR EMPLID Like '%$empid%'";
}
if(isset($empmail) && $empmail != '') {
$query .= " OR EMAILID Like '%$empmail%'";
}
$objParse = oci_parse($ora_conn,$query);
$objResult = oci_execute ($objParse, OCI_DEFAULT);
?>
<table width="500" border="1" align="center">
<tr>
<th width="98"> <div align="center">Operator ID</div></th>
<th width="98"> <div align="center">Operator Name</div></th>
<th width="98"> <div align="center">Person ID</div></th>
<th width="98"> <div align="center">Email ID</div></th>
<th width="98"> <div align="center">Edit</div></th>
</tr>
<?php
if(oci_execute($objParse,OCI_DEFAULT)){
while($objResult = oci_fetch_array($objParse, OCI_RETURN_NULLS+OCI_ASSOC))
{
?>
<tr>
<td><div align="center"><?=$objResult["OPRID"];?></div></td>
<td><?=$objResult["OPRDEFNDESC"];?></td>
<td><?=$objResult["EMPLID"];?></td>
<td><div align="center"><?=$objResult["EMAILID"];?></div></td>
<td align="center">Edit
</td>
</tr>
<?
}
?>
</table>
<?
oci_free_statement($objParse);
oci_close($ora_conn);
}
?>
You are mixing ands and ors in SQL and this is not a good idea. If you set them all to or, you will have an issue because where 1 will always be true. I would make your or conditional on the where clause not being empty.

Cannot fix the select/delete function. PHP/MySQL/JavaScript

guys I have the following script for adding, editing and deleting content in mysql, and showing it at index.php file. So here is my index.php file:
<script>function goDel()
{
var recslen = document.forms[0].length;
var checkboxes=""
for(i=1;i<recslen;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("Are you sure you want to delete");
if(con)
{
document.forms[0].action="delete.php?recsno="+checkboxes
document.forms[0].submit()
}
}
else
{
alert("No record is selected.")
}
}
function selectall()
{
// var formname=document.getElementById(formname);
var recslen = document.forms[0].length;
if(document.forms[0].topcheckbox.checked==true)
{
for(i=1;i<recslen;i++) {
document.forms[0].elements[i].checked=true;
}
}
else
{
for(i=1;i<recslen;i++)
document.forms[0].elements[i].checked=false;
}
}
</script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td><hr size="1" noshade></td>
</tr>
<tr>
<td>
<form action="" method="post" name="" id="">
<table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All </td>
<td colspan="3" align="center">Add New Branch </td>
</tr>
<tr>
<td><strong>Delete</strong></td>
<td><strong>Branch Name </strong></td>
<td><strong>Short Name </strong></td>
<td><strong>Update</strong></td>
</tr>
<?
include("conn.php");
$sql="select sn,branchname,shortname from $branch order by sn";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td><input name="<? echo $row['sn']; ?>" type="checkbox" class="check"></td>
<td><? echo $row['branchname']; ?></td>
<td><? echo $row['shortname']; ?></td>
<td>Update</td>
</tr>
<? } ?>
</table>
</form></td>
</tr>
</table>
Here is my delete.php file:
<?php
include("conn.php");
$recsno=$_GET["recsno"];
$data=trim($recsno);
$ex=explode(" ",$data);
$size=sizeof($ex);
for($i=0;$i<$size;$i++) {
$id=trim($ex[$i]);
$sql="delete from $branch where sn='$id'";
$result=mysql_query($sql,$connection) or die(mysql_error());
}
header("location: index.php");
?>
The problem is when I check 1 row, and click delete, it is deleting all of the rows, like I've clicked select all (which I didn't do). Thank you in advance.
It seems Delete query not getting value of $id, you have to store values in $id.
just GET id values
$id = $_REQUEST["sn"];
then your delete query
$sql="delete from $branch where sn='$id'";

Categories