how to bring ajax sorted result into html divs - php

HI i have done some AJAX, PHP&MySQL Sorting and it is giving me result in tables as shown in the code below, my question is how to bring that $result in html divs.
please help
PHP code used
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("security_software", $con);
$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>title</th>
<th>image</th>
<th>description</th>
<th>rating</th>
<th>download</th>
<th>buy</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['rating'] . "</td>";
echo "<td>" . $row['download'] . "</td>";
echo "<td>" . $row['buy'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I want Result In These HTML Div's
<div class="category-container">
<div class="category-image"></div>
<div class="category-link">#</div>
<div class="category-desc"><p>#</p> </div>
<div class="rating5" >Editors' rating: </div>
<div class="category-download-btn">Download </div><
<div class="category-buy-btn">Buy</div>
</div>

I don't know why you are creating table when returning the ajax response. I advice you to create json response as a result of ajax. Using this result JSON you can either create table or you can render them in your html.
in your php code where ajax request is sent: ajax.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("security_software", $con);
$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;
$result = mysql_query($sql);
$response = array();
$i=0;
while($row = mysql_fetch_array($result))
{
$response[$i]['id'] =$row['id'];
$response[$i]['title'] = $row['title'];
$response[$i]['image'] = $row['image'];
$response[$i]['description'] = $row['description'];
$response[$i]['rating'] = $row['rating'];
$response[$i]['download'] = $row['download'];
$response[$i]['buy'] = $row['buy'];
$i++;
}
mysql_close($con);
echo json_encode($response);
?>
In your html file where you are getting this ajax response, I am giving you the hint how can u use this ajax response:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$.ajax({
url: 'ajax.php',
dataType: 'json',
success: function(response){
data = '';
$.each(response,function(i,val){
data = '<div class="category-image">'+val.image+'</div>'+
'<div class="category-link">'+val.id+'</div>'+
'<div class="category-desc"><p>'+val.description+'</p> </div>'+
'<div class="rating5" >'+val.rating+'</div>'+
'<div class="category-download-btn">Download </div>'+
'<div class="category-buy-btn">Buy</div>';
$('<div>').attr('id',i).html(data).appendTo('#response');
});
});
}
});
</script>
</head>
<body>
<div id='response'></div>
</body>
</html>

I'm inclined to view this as a bit of a joke, seeing as the database is called "security_software", and you're putting a GET var directly into a database query without doing any sanitation. You're also making no attempt to clean anything coming from the database before spitting it back onto the page...
Anyway, assuming it's not a joke, the following should point you in the right direction:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("security_software", $con);
$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<div class="category-image"><img src="' $row['image'] . '</div>';
echo '<div class="category-link">' . $row['title'] . '</div>';
echo '<div class="category-desc"><p>' . $row['description'] . '</p></div>';
echo '<div class="rating5" >Editors' rating: ' . $row['rating'] . '</div>';
echo '<div class="category-download-btn">Download</div>';
echo '<div class="category-buy-btn">Buy</div>';
}
echo "</table>";
mysql_close($con);
?>

if you wan to pull the result in these divs then use same divs instead of table/tr/tds or you can bind it by receiving json/xml or any kind of object based data

Related

I need to add a dropdown to a table cell

I need to add two dropdown lists (from a Mysql query) to a table. I have the table that shows the results of r aquery and then I need to have the cells of the two columns with the dropdowns in them. This is going to be a form that will eventually create files.
Here is my code so far:
<?php
$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<p>
<select name="phone">
<?php
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '">' . $row['mac'] . '</option>';
}
?>
</select>
<select name="template">
<?php
while($row = mysql_fetch_array($result1)) {
echo '<option value="' . $row['id'] . '">' . $row['templatename'] . '</option>';
}
?>
</select>
</p>
<?php
mysql_close($link);
?>
I have tried to insert the select into the row but the page doesn't load I get an server error.
Any help is much appreciated
Ok, try this:
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td> <select name='phone'>";
while($rowA = mysql_fetch_array($result)) {
echo '<option value="' . $rowA['id'] . '">' . $rowA['mac'] . '</option>';
}
echo "</select></td>";
echo "<td><select name='template'>";
while($rowB = mysql_fetch_array($result1)) {
echo '<option value="' . $rowB['id'] . '">' . $rowB['templatename'] . '</option>';
}
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
?>
If you get any errors put them here. Keep in mind that the code above is preety bad, you shouldn't code like that. It's a good start to learn how things work, but later you should refactor it. Write it better, more readable, write your own functions getting data from db, use mysqli functions instead mysql or even library like PDO, separate your logic from the view etc.

how can i show returned result from database in same page of the submit form using php

Hello, i made this simple code to show info from my database into a table, the form code is below:
http://pastebin.com/yyzcjshn
the show.php code is:
<html>
<head>
<title>Show Result</title>
<?php
$connection = mysql_connect("localhost","root","");
// Check connection
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//select database to use
$db_select = mysql_select_db("sells",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
$D1 = $_POST['D1'];
//show info
if($D1 != 'Show All'){
$result = mysql_query("SELECT * FROM clients WHERE Status='$D1'", $connection);
}
else $result = mysql_query("SELECT * FROM clients", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Client Name</th>
<th>URL</th>
<th>Quantity</th>
<th>Price[$]</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row["Order_ID"]."</td>";
echo "<td>" . $row["ClientName"]."</td>";
echo "<td><a href=" . $row['Url'] . " target=_blank >" . $row['Url'] . "</a></td>";
echo "<td>" . $row["Quantity"]."</td>";
echo "<td>" . $row["Price"]."</td>";
echo "<td>" . $row["Status"]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connection);
?>
</head>
</html>
How can i show the result in the same page of the submit form using php (without ajax)?
Simple answer: You can't.
In the comments you say people use <?php echo $_SERVER['PHP_SELF']; ?>, but that's not the same. $_SERVER['PHP_SELF'] will echo the current scripts filename. When you use it, you don't have to write the action tag in your form manually.
AJAX is the method you need to update your page without refresh the whole page. You can use the (modern) AJAX method, or a redirect method. Because you don't want to use AJAX here is a redirect method example of your code:
Your form.php. Notice that i replaced your dropdown into a link. You can use links for a easy approuch. You can also keep your dropdown and use Javascript/JQuery to create the right redirection including the GET parameter:
<html>
<head>
<title>Show Info In Table</title>
</head>
<body>
....
Show all
....
Finished
....
<?=include('show.php');?>
</body>
</html>
show.php, notice that $_POST is replaced with $_GET:
<?php
$connection = mysql_connect("localhost","root","");
// Check connection
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//select database to use
$db_select = mysql_select_db("sells",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
$D1 = $_GET['D1'];
//show info
if($D1 != 'Show All'){
$result = mysql_query("SELECT * FROM clients WHERE Status='$D1'", $connection);
}
else $result = mysql_query("SELECT * FROM clients", $connection);
if(!$result){
die("Database query failed: " . mysql_error());
}
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Client Name</th>
<th>URL</th>
<th>Quantity</th>
<th>Price[$]</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row["Order_ID"]."</td>";
echo "<td>" . $row["ClientName"]."</td>";
echo "<td><a href=" . $row['Url'] . " target=_blank >" . $row['Url'] . "</a></td>";
echo "<td>" . $row["Quantity"]."</td>";
echo "<td>" . $row["Price"]."</td>";
echo "<td>" . $row["Status"]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connection);
?>
Good luck.

Delete full row from a form into a database in php

I want to delete full row from a form into a database in php when i click on delete button. But there are "Undefined index on line 39" issues come on my page and when i click on delete button it redirect me on different page and didn't delete a row.
how can i delete a row on one click ??
Please help me.
Thanks,
Nabeel
<body>
<a href="" >delete</a>
<a href="" >create</a>
<label>Read</label>
<?php
$con=mysqli_connect("localhost","root","","firstphp");
if($con)
{
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users`");
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>passward</th>
<th>name</th>
<th>delete</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['passward'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<label>End Read</label>
<br /><br /><hr />
<label>Delete</label>
<?php
if(isset($_POST['delete']))
{
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("Could not connect: " . mysql_error());
}
$id = $_POST["id"];
$sql = "DELETE FROM users WHERE id = $id";
mysql_select_db('firstphp');
$result = mysql_query($sql,$con);
if(!$result)
{
die("Could not delete data: " . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($con);
}
else
{}
?></body>
change
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
in
echo '<th><a href="'.$_SERVER['PHP_SELF'].'?id='. $row['id'] .'&delete=true" >delete</a></td>';
and change
if(isset($_POST['delete']))
//...
$id = $_POST["id"];
in
if(isset($_GET['delete']))
//...
$id = $_GET["id"];

checkbox in table in php

I want to have a table that contains words and their meaning from database, in another column i want to have checkbox for each row, that user will check them and show what words he/she know.
i have two question in this case:
1- how can i hide the meaning in the first and after clicking in show meaning visible them?
2- how can i set checkboxes?
i have this code until now but it doesn't work
please help me if you can
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').item(0).style.visiblility = 'visible';
}
</script>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='visibility:hidden'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
getElementByClassName is an inexistant function. You mean getElementsByClassName, which will however return a list of the elements, so you need to select one.
document.getElementsByClassName('hiding').item(0).style.visibility = 'visible';
For hide a suggestion:
echo "class='hiding' style='display:none'>" . $row['meaninig'];
To show the meaning:
//for Jquery
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
Or
//for plain old javascript
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').style.visibility = 'visible';
}
</script>
Your code Edited:
<html><head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='display:none'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
</body>

click row to fill textboxes in php - array vs ajax

I have a table coded in php which pulls in data from mysql database and displays it in a html table . Now when i click on a row I need certain textboxes to be filled.
What is the best approach for this: is it using an array or ajax,html and php,
<?php
$default = "<img src='http://localhost/on.png' width='24' height='24'/>";
$default1 = "<img src='http://localhost/of.png' width='24' height='24'/>";
$con = mysql_connect("*****","******","******");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db ("*****",$con);
$sql= "select act.*
from audit_activity as act
inner join (
select user_id, max(timestamp) as max_ts
from activity
group by user_id) as a on act.user_id=a.user_id and act.timestamp=a.max_ts";
$mydata = mysql_query($sql,$con);
echo "<table id='tfhover',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){
echo "<td>" . $default1 . "</td>";
}else{
echo "<td>" . $default . "</td>";
}
echo "</tr>";
}
echo "</table>";
;
mysql_close($con);
?>
<html>
<script type="text/javascript">
window.onload=function(){
var tfrow = document.getElementById('tfhover').rows.length;
var tbRow=[];
for (var i=1;i<tfrow;i++) {
tbRow[i]=document.getElementById('tfhover').rows[i];
tbRow[i].onmouseover = function(){
this.style.backgroundColor = '#ffffff';
};
tbRow[i].onmouseout = function() {
this.style.backgroundColor = '#d4e3e5';
};
}
};
</script>
<head>
</head>
<body>
Total exams taken : <br>
<input type="text" name="fname"/>
</body>
</html>

Categories