I have multiple divs , show/hide on button click div , inside one of these divs I have a table and to filter this table to search a specific user I use a PHP code and a submit form the problem is after submitting the form the page refreshes and the 1st div appears although the request worked and when I revisit the table div the filtering appears. I used this code to stop the refresh but the form is not being submitted:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('#myform').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'admin.php',
data: $('#myform').serialize(),
success: function () {
alert('table is filtered');
}
});
});
});
</script>
and here is my form:
<div class="in5 n5" id="users">
<br>
<form method="post" action="" id="myform" >
<table style="width:25%;height: 13%;table-layout: fixed; font-family: signika;background-color:white;color:white;border-bottom:1px solid white;">
<tr>
<td width="30%" style="text-align:center;">
<button style="width:100%;height: 100%;border:0; font-size: 20px;font-family: signika;background: transparent;">Show all</button>
</td>
<td>
<div style="height:100%;width: 100%;">
<input type="text" name="valueToSearch" placeholder="Search user" style="height:100%;width: 70%; font-size: 20px;" >
<input value="Search" id="search" name="search" type="submit" style="width:30%; height: 100%; float: right; background-color: gray; color:white; font-size: 20px; ">
</div>
</td>
</tr>
</table>
<table style="width:100%;height:10%; background-color: white; font-size: 20px; border-bottom: 1px solid #cccccc; table-layout: fixed;border-collapse: collapse;">
<tr>
<td style="width:3%;">ID</td>
<td style="width:10%;">Username</td>
<td style="width:10%;">Email</td>
<td style="width:10%;">First name</td>
<td style="width:10%;">Last name</td>
<td style="width:5%;">Gender</td>
<td style="width:10%;">Birthday</td>
<td style="width:8%;">Country</td>
<td style="width:7%;">City</td>
<td style="width:10%;">Bio</td>
<td style="width:5%;">Access</td>
<td style="width:7%;">Picture</td>
<td style="width:5%;">Ban user</td>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr >
<td style="width:3%; overflow: hidden;" title="<?php echo $row[0]; ?>">
<input style="border:0;background: transparent; font-size: 20px;" readonly="readonly" name="id" value="<?php echo $row[0]; ?>">
</td>
<td style="width:10%; overflow: hidden;" title="<?php echo $row[1]; ?>"><?php echo $row[1]; ?></td>
<td style="width:10%; overflow: hidden;" title="<?php echo $row[2]; ?>"><?php echo $row[2]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[4]; ?>"><?php echo $row[4]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[5]; ?>"><?php echo $row[5]; ?></td>
<td style="width:5%; overflow: hidden;"title="<?php echo $row[6]; ?>"><?php echo $row[6]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[7]; ?>"><?php echo $row[7]; ?></td>
<td style="width:8%; overflow: hidden;"title="<?php echo $row[8]; ?>"><?php echo $row[8]; ?></td>
<td style="width:7%; overflow: hidden;"title="<?php echo $row[9]; ?>"><?php echo $row[9]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[11]; ?>"><?php echo $row[11]; ?></td>
<td style="width:5%; overflow: hidden;">
<?php if($row['12']=="moderate"){ ?>
<a href="lock.php?id=<?php echo $row[0]; ?>">
<img class="img2" src="icons/access.png" ></a>
<?php } else{ ?>
<a href="access.php?id=<?php echo $row[0]; ?>">
<img class="img2" src="icons/locked.png" >
</a> <?php } ?>
</td>
<td style="width:7%; overflow: hidden;">
<a href="#image" title="See profile picture" class="cover" >
<?php if($row[10]==""){ ?>
<img class="cimg2 " src="images/profile.png"> <?php
} else { ?>
<img class="cimg2 " src="img/<?php echo $row[10] ?>" >
<?php }?>
</a>
</td>
<td style="width:5%; overflow: hidden;">
<a title="Delete user" href="deleteuser.php?id=<?php echo $row[0]; ?>" style="border:0;background: transparent;">
<img src="icons/ban.png"></a></td>
</tr>
<?php endwhile;?>
</table>
</form>
and the PHP code:
<?
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `r` WHERE CONCAT(`id`, `username`,`email`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `r`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "x");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
the problem is on submit the page is not refreshing and the alert appears but also the table is not filtering.
Use on click not on submit and input type button
$('#myform').on('click', function (e) {
Oops! You missed a concept also ! When using ajax you need to manipulate your DOM(Document Object Model) by yourself on callback functions
You are missing return false; at the end of submit event. The form submit event when customized and e.preventDefault() is used then your implementation overrides the default behavior thus it needs to tell the form if it needs to submit the whole page or not.
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('#myform').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'admin.php',
data: $('#myform').serialize(),
success: function () {
//filter the table using javascript code by changing the elements etc.
alert('table is filtered');
}
});
return false;
});
});
</script>
If this not works then there must be some uncertainty like there may be some **runtime error ** which is preventing your script to propagate.
Related
So im kinda trying to delete row from my mysql database using checkboxes. Here is my code. Would be glad if someone could write me down simple delete code. Tryed many but failed, stuck for hours with this :(
<html>
<head>
<title>Admin, User</title>
</head>
<body>
<?php include 'connect.php';?>
<?php include 'functions.php';?>
<?php include 'title_bar.php';?>
<h3>Sukurti Nauja uzduoti: </h3>
<form method='post'>
<?php
if(isset($_POST['submit2']))
{
$pav = $_POST['pav'];
$uzduotis = $_POST['uzduotis'];
if(empty($pav) or empty($uzduotis)){
echo "<p>Privalomi visi langai!</p>";
}
else {
$sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
}
if($database->query($sql) == TRUE)
{
header('location:kurtisalinti.php');
}
else {
echo "<p>Klaida!</p>";
}
}
?>
<p>Uzduoties pavadinimas:
<p><input type='text' name='pav' />
<p>Uzduotis:
<p><textarea name='uzduotis'></textarea>
<p><input type='submit' name='submit2' value='Sukurti Uzduoti' />
</form>
</p></p></p></p>
</form>
<h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
<?php
$query = mysqli_query($database,"SELECT uzid,pav,uzduotis FROM uzduotys");
$count=mysqli_num_rows($query);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Aprasymas</strong></td>
</tr>
<?php
while($rows = mysqli_fetch_array($query)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" value="<?php echo $row['uzid']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['uzid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['pav']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['uzduotis']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Istrinti Uzduotis"></td>
</tr>
<?php
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Welcome to StackOverflow! Generally as a rule of thumb asking to write you some code doesn't get you very far, however I know that some people learn better from seeing it implemented and being able to modify off of that.
There is a number of ways you can do this. Using something like jquery and ajax you can post your requests without having to reload the page. Keep in mind what I have below may not fully work for you since taking the time to do this much already without a database or connection should give you an idea of what you're looking to do. Good luck!
CAUTION: PSUEDO CODE (This may not fully work and is not intended for production) The idea is to show you an approach to achieving what you are looking for.
<?php
include('connect.php');
include('functions.php');
include('title_bar.php');
if(isset($_POST['submit-uzduotis']))
{
$pav = $_POST['pav'];
$uzduotis = $_POST['uzduotis'];
if(empty($pav) or empty($uzduotis))
{
$result = "<span>Privalomi visi langai!</span>";
}
else
{
$sql = ("INSERT INTO uzduotys VALUES ('','$pav','$uzduotis')");
}
if($database->query($sql) == true)
{
header('location:kurtisalinti.php');
}
else
{
$result = "<span>Įvyko klaida!</span>";
}
}
if(isset($_POST['remove']))
{
$id = mysql_real_escape_string($_POST['remove']);
mysqli_query($database, "DELETE FROM uzduotys WHERE uzid = $id");
}
function showTableData()
{
$query = mysqli_query($database, "SELECT uzid, pav, uzduotis FROM uzduotys");
$count = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query))
{
echo '<tr id="'. $row['uzid'] .'">
<td>
<input type="checkbox" name="checkbox[]" value="'. $row['uzid'] .'">
</td>
<td>'. $row['uzid'] .'</td>
<td>'. $row['pav'] .'</td>
<td>'. $row['uzduotis'] .'</td>
<td>
<button id="remove-single" data-id="'. $row['uzid'] .'">REMOVE</button>
</td>
</tr>';
}
}
?>
<html>
<head>
<title>Admin, User</title>
<style>
.uzduotis-table {
background-color: #ccc;
border-collapse: collapse;
}
.uzduotis-table thead > td {
background-color: #333;
font-weight: bold;
}
.uzduotis-table td {
padding: 3px;
text-align: center;
background-color: #fff;
}
label {
display:block;
position:relative;
font-weight: bold;
}
</style>
</head>
<body>
<h3>Sukurti Nauja uzduoti: </h3>
<form method='post'>
<?php echo $result; ?>
<div>
<label>Uzduoties pavadinimas:</label>
<input type='text' name='pav' />
</div>
<div>
<label>Uzduoties:</label>
<textarea name='uzduotis'></textarea>
</div>
<div>
<input type='submit' name='submit-uzduoties' value='Sukurti Uzduoti' />
</div>
</form>
<h3>Pasalinti pasirinkta uzduoti is uzduociu saraso: </h3>
<table class="uzduotis-table">
<thead>
<tr>
<td>#</td>
<td>Id</td>
<td>Pavadinimas</td>
<td>Aprasymas</td>
</tr>
</thead>
<tbody>
<?php showTableData(); ?>
<tr>
<td colspan="5">
<button id="remove-selected" data-id="'. $row['uzid'] .'">REMOVE SELECTED</button>
</td>
</tr>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
// Remove Single (Button)
$('body').on('click', '#remove-single', function() {
remove($(this).data('id'));
});
// Remove All Checked
$('body').on('click', '#remove-selected', function() {
$('.uzduotis-table tr').filter(':has(:checkbox:checked)').each(function() {
remove(this.id);
});
});
function remove(id)
{
console.log("Remove: " + id);
alert('Removing: ' + id);
$.ajax({
type: "POST",
url: '<?php echo $url; ?>',
data: { remove: id },
success: function (data) {
$('#' + id).remove();
}
});
}
})
</script>
</body>
</html>
I cleaned up a bit of the html and styles, just made it easier for me to read. Also I would recommend looking into a library such as PDO to handle your database queries as there currently isn't much in place to protect or safe guard against sql injection.
I'm trying to delete the selected rows from the list I have in mysql so I won't have to delete the rows in my table one by one. When I press delete, my page refreshes without any php error but I don't get the result desired either. here is what I have:
<?php $product_set = find_all_products(); ?>
<body>
<form action="manage_products.php" method="delete">
<div class="page">
<article>
<div id="page">
<?php echo message(); ?>
<h2>Manage Products</h2>
<table>
<tr>
<th style="text-align: left; width: 125px;">Location</th>
<th style="text-align: left; width: 250px;">URL to the Product</th>
<th style="text-align: left; width: 100px;">First Name</th>
<th style="text-align: left; width: 100px;">Last Name</th>
<th style="text-align: left; width: 100px;">Size</th>
<th style="text-align: left; width: 100px;">Status</th>
<th style="text-align: left; width: 100px;">Checked</th>
<th colspan="2" style="text-align: left;">Actions</th>
</tr>
<?php while($product = mysqli_fetch_assoc($product_set)){ ?>
<tr>
<td><?php echo htmlentities($product["location"]);?></td>
<td><?php echo htmlentities($product["productURL"]); ?></td>
<td><?php echo htmlentities($product["fname"]); ?></td>
<td><?php echo htmlentities($product["lname"]); ?></td>
<td><?php echo htmlentities($product["size"]); ?></td>
<td><?php echo htmlentities($product["status"]); ?></td>
<td><input name="checkbox" type="checkbox" id="checkbox[]" value="<?php echo $product['id']; ?>"></td>
<td>Edit Order</td>
<td>Delete Order</td>
</tr>
<?php } ?>
</table>
<?php
// Check if delete button active, start this
if(isset($_GET['delete']))
{
$checkbox = $_GET['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM product WHERE link_id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
$_SESSION["message"] = "Product deletion failed.";
redirect_to("created_products.php"); }
}
//mysqli_close($dbc);
?>
<br />
</div>
</div>
<br>
<input type="submit" name="submit" value="Delete" onclick="return val();"/>
</form>
<div class="clear-all"></div>
</article>
</div>
</body>
Also place all the code which is for deleting the rows in the starting of your file. Thank you #Traveller for this.
Instead of for loop, use WHERE IN
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM product WHERE link_id='$del_id'";
$result = mysql_query($sql);
}
Replace that with
$ids = implode(",", $_POST['checkbox']);
$sql = "DELETE FROM product WHERE link_id in ($ids)";
I have some problem with position off my tables.
here you can see what is looking now:
http://postimg.org/image/auv0lum57/
My controller:
?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Manage_products extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Products_model');
$this->load->library('form_validation');
}
public function index()
{
session_start();
$this->data['products'] = $this->Products_model->get_all();
$this->data['title'] = 'Product Management';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('main_view', $this->data);
$this->load->view('manage_products', $this->data);
}
view off manage_products
Products
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}
#infoMessage p{
padding: .8em;
margin-bottom: 1em;
border: 2px solid #ddd;
background: #FFF6BF;
color: #817134;
border-color: #FFD324;
text-align: center;
}
</style>
</head>
<body>
<table width="700" border="0" align="center">
<tr>
<td><h2>Parduotuvės </h2></td>
</tr>
<tr>
<td><div id="infoMessage"><?php echo $message;?></div></td>
</tr>
<tr>
<td><input name="New" type="button" value="Pridėti naują" onclick="window.location='product/add'" /></td>
</tr>
</table>
<form name="frmproduct" method="post">
<input type="hidden" name="rid" />
<input type="hidden" name="command" />
<table width="700" align="center">
<tr>
<th width="150"><strong>Parduotuvės pavadinimas</strong></th>
<th><strong>Prekės identifikatorius</strong></th>
<th><strong>Nuoroda</strong></th>
<th><strong>Redaguoti</strong></th>
<th><strong>Ištrinti</strong></th>
</tr>
<?php
foreach ($products as $product){
$product_id = $product['id'];
?>
<tr>
<td><?php echo $product['pavadinimas'] ?></td>
<td><?php echo $product['prekesid'] ?></td>
<td><?php echo $product['linkas'] ?></td>
<td><a href='product/edit/<?php echo $product_id ?>'>Redaguoti</a></td>
<td>
<?php
echo anchor('product/delete/'.$product_id, 'Delete', array('onClick' => "return confirm('Ar tikrai norite ištrinti?')"));
?>
</td>
</tr>
<?php
}
?>
</table>
</form>
</body>
</html>
I think problem is with line 22. I must load main_view, but my table must be in main_view, not in bottom. How to fix this problem?
I think I under stood correct try something like
return $this->load->view('name', null, true) or $this->load->view('name', $data, true)
could try and use null or true, return for tables.
here it showing all records from database but it shows all empty div first then below it shows all records .i want to show each record to be shown in single div in rows.
<div id="winnercontainer">
<h2 style="background-color:#9966FF; width:850px; height:30px; font:bold; font- size:22px; color:#FFFFFF; padding-left:20px;">Winners</h2>
<?php
include('pagination/ps_pagination.php');
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('gunjanbid', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM winners';
$pager = new PS_Pagination($conn, $sql,10, 10);
//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>
<table>
<?php
while($row=mysql_fetch_array($rs)){
?>
<div id="winner">
<tr>
<td ><img src="memberpic/myphoto.jpg" width="150" height="150" alt="001" /></td>
<td > </td>
<td ><table >
<tr>
<td >Auction Item : <?php echo $row['Items']; ?></td>
</tr>
<tr>
<td>Rs. <?php echo $row['Rs']; ?></td>
</tr>
<tr>
<td>Winning Bid Value : Rs. <?php echo $row['WinningBidValue']; ?></td>
</tr>
<tr>
<td>MRP : Rs. <?php echo $row['MRP']; ?></td>
</tr>
<tr>
<td>Auction closed on : <?php echo $row['enddate']; ?></td>
</tr>
<tr>
<td>Winner : <?php echo $row['Winner']; ?></td>
</tr>
<tr>
<td>City: <?php echo $row['City']; ?></td>
</tr>
<tr>
<td >Delivery Status: <?php echo $row['DeliveryStatus']; ?></td>
</tr>
</table></td>
<td > </td>
<td id=save><font color=black>SAVED:</font> Rs.<?php echo $row['MRP']- $row['WinningBidValue']; ?></td>
<td > </td>
<td ><img src=productimage/1/1.1.jpg width="200" height="193" alt="001" /></td>
</tr>
</div>
<?php
}
?>
</table>
<?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</div>
abd my stylesheet s are
#winnercontainer
{
width:870px;
height:auto;
background-color:#CCCCCC;
border:solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin:10px;
position:relative;
}
#winner
{
width:840px;
height:250px;
background-color: #999999;
border: solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 15px;
position:relative;
}
i want all my records to be place in div means each div have one record to be display.plz help me ,i am new here.
I'll give you a start with some old code I have that reads a table from a MySQL database and prints it out on a screen. You can modify it as you see fit. It's a php file that resides on the server.
<?php
echo "<head><title>Read list</title></head>";
$host = "xxxxxxxxxxx";
$user = "xxxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();}
$query = " select * from list where uniqueid >= 0 ";
$result = mysqli_query($cxn, $query) or die ("could not query database");
echo "<table cellpadding=1px border=1>";
while ($row = mysqli_fetch_array($result))
{
$uniqueid = $row['uniqueid'];
$localid = $row['localid'];
$mdid = $row['mid'];
$type = $row['type'];
echo "<tr><td>".$uniqueid."</td><td>".$localid."</td><td>".$mdd."</td><td>".$type."</td></tr>";
}
echo "</table>";
?>
In this below code i am searching a value.if the value is searched it displays the result but if the value is not present it should displays no result. In my case if i execute and not search any value it displays no record .Please any one help me.
<form action="<?php echo site_url('search1_site/index');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" id="opn" value = "Search" />
</form>
<?php
if($results){
?> <div id='hideme'>
CLOSE<a href='#' class='close_notification' title='Click to Close'>
<img src="<?php echo base_url('img/close.png'); ?>" width="15" height="15" alt="Close" onClick="hide('hideme')"/>
</a> <div style="background:#FFFFFF; width: 500px; height: 500px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px" id="modal" >
<table class="display2 table table-bordered table-striped">
<tr>
<th>course_code</th>
<th>course name</th>
</tr>
<tr><?php
foreach ($results as $row) {
?>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
}else{
echo "no results";
}
?>
</table></div></div>
<script>
$('a.modal').bind('click', function(event) {
event.preventDefault();
$('#modal').fadeIn(10);
});
function hide(obj) {
var el = document.getElementById(obj);
el.style.display = 'none';
}
</script>
Try adding name attribute to your submit button and check if it's set before the if...else block.
<!-- HTML -->
<input type="submit" name="submit" id="opn" value = "Search" />
// PHP
<?php
if (isset($_POST['submit'])) { // Here
// Do the search here
if($results){
?> <div id='hideme'>
CLOSE<a href='#' class='close_notification' title='Click to Close'>
<img src="<?php echo base_url('img/close.png'); ?>" width="15" height="15" alt="Close" onClick="hide('hideme')"/>
</a> <div style="background:#FFFFFF; width: 500px; height: 500px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px" id="modal" >
<table class="display2 table table-bordered table-striped">
<tr>
<th>course_code</th>
<th>course name</th>
</tr>
<tr><?php
foreach ($results as $row) {
?>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
}else{
echo "no results";
}
} // If closing
?>
Is this something similar to what you're looking for?
If the variable $results is array of results use something like this:
if (count($results) > 0)
{
// show results
} else
{
echo "No results";
}