show php and mysql database image - javascript

Hello I have some data in mysql table, like this:
name----cake----image_design
explosion----cheesecake----images/cake/sofia.png
I have all images of the designs in a folder (/cake)
In my php document I get the data:
<?php
session_start();
include "../config/config.php";//Contiene funcion que conecta a la base de datos
if (!isset($_SESSION['user_id'])&& $_SESSION['user_id']==null) {
// header("location: index.php");
}
$id_conect=$_SESSION['user_id'];
$query3=mysqli_query($con, "SELECT * from user where id='$id_conect'");
while ($rs=mysqli_fetch_array($query3))
{
$tipo=$rs['tipo'];
}
$action = (isset($_REQUEST['action']) && $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if (isset($_GET['id'])){
$id_del=intval($_GET['id']);
$query=mysqli_query($con, "SELECT * from pedido where id_pedido='".$id_del."'");
$count=mysqli_num_rows($query);
if ($delete1=mysqli_query($con,"DELETE FROM pedido WHERE id_pedido='".$id_del."'")){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Aviso!</strong> Datos eliminados exitosamente.
</div>
<?php
}else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Error!</strong> Lo siento algo ha salido mal intenta nuevamente.
</div>
<?php
} //end else
} //end if
?>
<?php
if($action == 'ajax'){
// escaping, additionally removing everything that could be (html/javascript-) code
$q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
$aColumns = array('cliente');//Columnas de busqueda
$sTable = "pedido";
$sWhere = "";
if ( $_GET['q'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= $aColumns[$i]." LIKE '%".$q."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
$sWhere.=" order by created_at desc";
include 'pagination.php'; //include pagination file
//pagination variables
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
$per_page = 10; //how much records you want to show
$adjacents = 4; //gap between pages after number of adjacents
$offset = ($page - 1) * $per_page;
//Count the total number of row in your table*/
$count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM $sTable $sWhere");
$row= mysqli_fetch_array($count_query);
$numrows = $row['numrows'];
$total_pages = ceil($numrows/$per_page);
$reload = './expences.php';
//main query to fetch the data
$sql="SELECT * FROM $sTable $sWhere LIMIT $offset,$per_page";
if($tipo==1|| $tipo==3)
{
$query = mysqli_query($con, $sql);
}else
{
$query=mysqli_query($con, "select * from pedido where id_usuario= $id_conect");
}
//loop through fetched data
if ($numrows>0){
?>
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Cliente </th>
<th>Fecha</th>
<th class="column-title">Tienda </th>
<th class="column-title no-link last"><span class="nobr"></span></th>
</tr>
</thead>
<tbody>
<?php
while ($r=mysqli_fetch_array($query)) {
$id=$r['id_pedido'];
$created_at=date('Y-m-d H:i:s', strtotime($r['created_at']));
// $description=$r['description'];
// $nombre=$r['nombre_ticket'];
// $solucion=$r['solucion'];
// $soporte=$r['soporte'];
// $title=$r['title'];
// $priority_id=$r['priority_id'];
$status_id=$r['id_status'];
// $kind_id=$r['kind_id'];
// $category_id=$r['category_id'];
$Cliente=$r['cliente'];
$project_id=$r["project_id"];
$encargada=$r["nombre_pedido"];
$producto_id=$r["id_producto"];
$cantidad=$r["cantidad"];
$biscocho=$r["biscocho"];
$relleno=$r["id_relleno"];
$design=$r["id_design"];
$opcion=$r["opcion"];
$observacion=$r["observacion"];
$dedicatoria=$r["dedicatoria"];
$c_fondo=$r["color_fondo"];
$c_chongo=$r["color_chongo"];
$c_rosas=$r["color_rosas"];
$figura=$r["figura"];
$c_figura=$r["color_figura"];
$hora=$r["hora"];
$cliente=$r["cliente"];
$telefono=$r["telefono"];
$entrega =$r['fecha_entrega'];
$imagen =$r["id_design"];
$sql = mysqli_query($con, "select * from project where id=$project_id");
if($c=mysqli_fetch_array($sql)) {
$name_project=$c['name'];
}
// $sql = mysqli_query($con, "select * from priority where id=$priority_id");
// if($c=mysqli_fetch_array($sql)) {
// $name_priority=$c['name'];
// }
$sql = mysqli_query($con, "select * from status where id=$status_id");
if($c=mysqli_fetch_array($sql)) {
$name_status=$c['name'];
}
?>
<input type="hidden" value="<?php echo $id;?>" id="id<?php echo $id;?>">
<input type="hidden" value="<?php echo $created_at;?>" id="fecha<?php echo $id;?>">
<input type="hidden" value="<?php echo $project_id;?>" id="project_id<?php echo $id;?>">
<input type="hidden" value="<?php echo $Cliente;?>" id="cliente<?php echo $id;?>">
<input type="hidden" value="<?php echo $encargada;?>" id="encargada<?php echo $id;?>">
<!-- me obtiene los datos -->
<input type="hidden" value="<?php echo $producto_id;?>" id="producto_id<?php echo $id;?>">
<input type="hidden" value="<?php echo $cantidad;?>" id="cantidad<?php echo $id;?>">
<input type="hidden" value="<?php echo $biscocho;?>" id="biscocho<?php echo $id;?>">
<input type="hidden" value="<?php echo $relleno;?>" id="relleno<?php echo $id;?>">
<input type="hidden" value="<?php echo $design;?>" id="design<?php echo $id;?>">
<input type="hidden" value="<?php echo $status_id;?>" id="status_id<?php echo $id;?>">
<input type="hidden" value="<?php echo $opcion;?>" id="opcion<?php echo $id;?>">
<input type="hidden" value="<?php echo $observacion;?>" id="observacion<?php echo $id?>">
<input type="hidden" value="<?php echo $dedicatoria;?>" id="dedicatoria<?php echo $id?>">
<input type="hidden" value="<?php echo $c_fondo;?>" id="c_fondo<?php echo $id?>">
<input type="hidden" value="<?php echo $c_chongo;?>" id="c_chongo<?php echo $id?>">
<input type="hidden" value="<?php echo $c_rosas;?>" id="c_rosas<?php echo $id?>">
<input type="hidden" value="<?php echo $figura;?>" id="figura<?php echo $id ?>">
<input type="hidden" value="<?php echo $entrega;?>" id="entrega<?php echo $id ?>">
<input type="hidden" value="<?php echo $hora;?>" id="hora<?php echo $id ?>">
<input type="hidden" value="<?php echo $cliente;?>" id="cliente<?php echo $id ?>">
<input type="hidden" value="<?php echo $telefono;?>" id="telefono<?php echo $id ?>">
<input type="hidden" value="<?php echo $imagen;?>" id="imagen<?php echo $id ?>">
<tr class="even pointer">
<td><?php echo $cliente;?></td>
<td><?php echo $created_at;?></td>
<td><?php echo $project_id;?></td>
<td ><span class="pull-right">
<i class="glyphicon glyphicon-edit"></i>
</span></td>
</tr>
<?php
} //en while
?>
<tr>
<td colspan=6><span class="pull-right">
<?php echo paginate($reload, $page, $total_pages, $adjacents);?>
</span></td>
</tr>
</table>
</div>
<?php
}else{
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Aviso!</strong> No hay datos para mostrar!
</div>
<?php
}
}
?>
Then in another archive.php, I receive the variables like this:
<script>
function obtener_datos(id){
var soporte = $("#soporte"+id).val();
var encargada = $("#encargada"+id).val();
var producto_id = $("#producto_id"+id).val();
var cantidad = $("#cantidad"+id).val();
var biscocho = $("#biscocho"+id).val();
var relleno = $("#relleno"+id).val();
var design = $("#design"+id).val();
var opcion = $("#opcion"+id).val();
var observacion =$("#observacion"+id).val();
var dedicatoria =$("#dedicatoria"+id).val();
var c_fondo = $("#c_fondo"+id).val();
var c_chongo =$("#c_chongo"+id).val();
var c_rosas =$("#c_rosas"+id).val();
var figura =$("#figura"+id).val();
var entrega =$("#entrega"+id).val();
var hora =$("#hora"+id).val();
var cliente =$("#cliente"+id).val();
var telefono =$("#telefono"+id).val();
var kind_id = $("#kind_id"+id).val();
var project_id = $("#project_id"+id).val();
var category_id = $("#category_id"+id).val();
var priority_id = $("#priority_id"+id).val();
var status_id = $("#status_id"+id).val();
var imagen =$("#imagen"+id).val();
$("#mod_id").val(id);
$("#mod_encargada").val(encargada);
$("#mod_producto_id").val(producto_id);
$("#mod_cantidad").val(cantidad);
$("#mod_biscocho").val(biscocho);
$("#mod_relleno").val(relleno);
$("#mod_design").val(design);
$("#mod_opcion").val(opcion);
$("#mod_observacion").val(observacion);
$("#mod_dedicatoria").val(dedicatoria);
$("#mod_c_fondo").val(c_fondo);
$("#mod_c_chongo").val(c_chongo);
$("#mod_c_rosas").val(c_rosas);
$("#mod_figura").val(figura);
$("#mod_entrega").val(entrega);
$("#mod_hora").val(hora);
$("#mod_cliente").val(cliente);
$("#mod_telefono").val(telefono);
$("#mod_soporte").val(soporte);
$("#mod_kind_id").val(kind_id);
$("#mod_project_id").val(project_id);
$("#mod_category_id").val(category_id);
$("#mod_priority_id").val(priority_id);
$("#mod_status_id").val(status_id);
$("#mod_imagen").val(imagen);
}
</script>
Below #mod_name and #mod_cake is the id of text box.
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" style="width:150px; float:left;"name="encargada" id="**mod_cake**"class="form-control">
</div>
</div>
</div>
Now my problem is with the image, because return me images/cake/sofia.png and I don't know how I show that image
I try with this:
<img id ="mod_imagen"src="">
but does not work.

i solve that wiht this:
javascript:
var imagen =$("#imagen"+id).val();
document.getElementById("imagenid").src=imagen;
other php:
<img id="imagenid"src="" />

Having the image itself in you database may slow down your queries. Instead, consider storing the image name in the database and the image itself as you usually would: a file in a directory. Then, with Javascript:
document.getElementById("**mod_cake**").src = "<?php echo $filename; ?>";

Related

how to implement jquery load on scroll

I have got a page in which I'm getting a users all friends posts now I want to get only 15 post first and then get I wanna add a load more button and then load 15 more posts how should I do that.
This is my code
$query = "SELECT ph.likes, ph.image_url,ph.email,ph.username,ph.uid ,ph.id
FROM photos as ph
inner join followers as fol
on fol.user_id = ph.uid
where fol.uid = '$id'
ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
while ($users = mysqli_fetch_assoc($fire)) {
$likes = $users['likes'];
$username = $users['username'];
$uid = $users['uid'];
$pixid = $users['id'];
}
}
?>
<?php
$query = "SELECT ph.ext, ph.likes,ph.desccription, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path
FROM photos as ph
inner join followers as fol
on fol.user_id = ph.uid
where fol.uid = '$id'
ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
while ($users = mysqli_fetch_assoc($fire)) {
$likes = $users['likes'];
$description = $users['desccription'];
$username = $users['username'];
$uid = $users['uid'];
$pixid = $users['id'];
$avatar_path5 = $users['avatar_path'];
?>
<div class="all" >
<div class="card" >
<div class="float" >
<div class="avatar" >
<img src="<?php echo $avatar_path5; ?>" width="100%" class="avatar">
</div>
<div class="username" style="font-weight: 600; size: 14px; text-decoration: none; color: black !important; ">
<p style="color: black !important;"><?php echo "<div><a href='users?id=".$users['uid']."' style='color: black !important; '>
<h3>".$users['username']."</h3>
</div></a>"; ?></p>
</div>
</div>
<?php
if ($users['ext']=='mp4') {
?>
<video class="videos" controlsList="nofullscreen nodownload" style="width: 100%; height:100%;" controls autoplay="true" muted>
<source src="<?php echo $users['image_url']?>" type="video/mp4">
</video>
<?php
}else{
?>
<img src="<?php echo $users['image_url']?>" alt="Avatar" style="width:100%;">
<?php }
?>
<div class="container">
<h4><b><?php echo "<div><a href='users?id=".$users['uid']."'>
</div></a>";?></b></h4>
</div>
<div style="padding: 2px; margin-top: 5px;">
<?php
if (isset($_POST['liked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)
or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "INSERT INTO likes (user_id,username, post_id,avatar_path) VALUES ($id, '$fullname', $postid,'$activeavatar')")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n+1 WHERE id=$postid")or die(mysqli_error($con));
echo $n+1;
exit();
}
if (isset($_POST['unliked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "DELETE FROM likes WHERE post_id=$postid AND user_id=$id")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n-1 WHERE id=$postid")or die(mysqli_error($con));
echo $n-1;
exit();
}
?>
</div>
<script type="text/javascript">
$('video').each(function(){
if ($(this).is(":in-viewport")) {
$(this)[0].play();
} else {
$(this)[0].pause();
}
})
</script>
<div>
<?php
// determine if user has already liked this post
$results = mysqli_query($con, "SELECT * FROM likes WHERE user_id=$id AND post_id=".$users['id']."")or die(mysqli_error($con));
if (mysqli_num_rows($results) == 1 ): ?>
<!-- user already likes post -->
<span class="unlike fas fa-heart " data-id="<?php echo $users['id']; ?>"></span>
<span class="like hide far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<?php else: ?>
<!-- user has not yet liked post -->
<span class="like far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<span class="unlike hide fas fa-heart " data-id="<?php echo $users['id']; ?>"></span>
<?php endif ?>
<a class="com" style="color: #929292 !important; " href="show?post_id=<?php echo $users['id'];?>" style=""> <span class="glyphicon glyphicon-comment trigger" ></span>comments</a>
<div class="wink" style=" color: black;
width: 30px;
height: 30px;
margin-top: -20px;
margin-left: 209px;
" > <?php
?></div>
<div class="float2">
<div class="react2" id="react2" data-rowid="<?php echo $pixid?>"><?php
$query2 = "SELECT * FROM gormint where post_id = $pixid and user_id= $id3";
$fire2 = mysqli_query($con,$query2) or die("can not fetch data from database ".mysqli_error($con));
$query3 = "SELECT * FROM bhai where post_id = $pixid and user_id= $id3";
$fire3 = mysqli_query($con,$query3) or die("can not fetch data from database ".mysqli_error($con));
$query4 = "SELECT * FROM famer where post_id = $pixid and user_id= $id3";
$fire4 = mysqli_query($con,$query4) or die("can not fetch data from database ".mysqli_error($con));
$query5 = "SELECT * FROM muskan where post_id = $pixid and user_id= $id3";
$fire5 = mysqli_query($con,$query5) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire2)>0) {
echo "<img src='gormint.jpg' class='gormint2' style='width:30px; height:30px;' >";
}elseif (mysqli_num_rows($fire3)>0) {
echo "<img src='bhai.jpg' class='bhai2' style='width:30px; height:30px;'>";
}elseif (mysqli_num_rows($fire4)>0) {
echo "<img src='famer.jpg' class='bhai2' style='width:30px; height:30px;'>";
}elseif (mysqli_num_rows($fire5)>0) {
echo "<img src='bancho.jpg' class='bhai2' style='width:30px; height:30px;'>";
} else{
echo "<img src='wink.png' class='wink2' style='width:30px; height:30px;'>";
}?>
</div>
<div class="flipClass" style="display: flex;" data-rowid="<?php echo $pixid?>" id="flip">react</div>
<div class="panelClass" style="" id="panel" data-rowid="<?php echo $pixid?>">
<input type="image" onclick="PlaySound2()" id="display" data-value="<?php echo $users['id'];?>" src="gormint2.jpg" class="close2 display gormint animated bounceIn " >
<input type="image" onclick="PlaySound3()" data-value="<?php echo $users['id'];?>" data-rowid="<?php echo $pixid?>" id="display2" src="bhai.jpg" class="close2 display2 bhai animated bounceIn">
<input type="image" data-rowid="<?php echo $pixid?>" onclick="PlaySound4()" data-value="<?php echo $users['id'];?>" id="display3" src="famer.jpg" class="close2 display3 bhai animated bounceIn">
<input type="image" onclick="PlaySound5()" data-value="<?php echo $users['id'];?>" id="display4" src="bancho.jpg" class="close2 display4 bhai animated bounceIn">
</div>
</div>
<div class="count2 ">
<span id="count" style="margin-top: -5%;" class="likes_count"><?php echo $users['likes']; ?> likes</span><?php
if ($description=='') {
echo "";
}else{ echo '<p style="margin-bottom: 5%" class="written"> <b>'.$users["username"].'</b> '.$description;'</p>';
}
?>
</div>
<form method="POST" action="" >
<div class="commentdiv">
<input type="hidden" name="id" id="id" class="id" value="<?php echo $pixid;?>">
<input type="hidden" name="username" id="username" value="<?php echo $activeusername;?>">
<input type="hidden" name="uid" id="uid" value="<?php echo $id3;?>">
<textarea style="" name="comment" id="comment" class="comment" placeholder=" comment here"></textarea>
<button type="button" style="background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
color: #3897f0; font-weight:600;" class="submit" id="button_id">comment</button>
</div>
</form>
</div>
<div id="comments">
<?php
$sql = "SELECT * FROM comment where post_id='$pixid' limit 3";
$results = mysqli_query($con,$sql);
if (mysqli_num_rows($results)>0) {
while ($row = mysqli_fetch_assoc($results)) {
$commentid = $row['id'];
$comment = $row['comment'];
$string = covertHashtags($comment);
echo "<p class='written'>";
echo "<a href='users2?id=".$row['user_id']."' style='color:black !important;'><b>".$row['username']."</b></a>";
echo " ".$string;
$sql3 = "SELECT * FROM comment where id ='$commentid' and user_id='$id' order by comment desc limit 5 ";
$results3 = mysqli_query($con,$sql3);
if (mysqli_num_rows($results3)>0) {
echo "<div class='dropdown'>
<img src='ellipsis.png' class='dots'>
<div class='dropdown-content'>
<br><p class='delete' data-delete=".$commentid.">delete</p>
</div>
</div>";
}
else{
echo "";
}
echo "</p>";
}
}else{
echo "";
}
?>
</div>
</div>
<br><br>
<?php } ?>
now what should I do to add a load more button after first 15 posts and when user clicks on that button I want to load more 15 posts and there is also one more problem I have a set a refresh functionality through j query whenever a user clicks on like button or comments the page refreshes so would this cause any problem?

Show/hide info using slideToggle on button

Objective Inserting name(input-text) and info(textarea-contains multiple lines) into the database, and after submission of the form, at the same page, 2 columns are for displaying the same data in columns, name & info, but under info column. I have made buttons for each row in front of the names, which is used as slideToggle for showing/hiding which contains the data retrieved from the 'info' column
Problem - when I am clicking the button of 1st row, instead of displaying the info related to 1st entry only, it is sliding and showing all info(s) related to all entries at only click.
*others - one more input has been added to the form but as hidden used for id(auto increment)
----index.php-----
<?php include('php_code.php'); ?>
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM records WHERE id=$id");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$acc = $n['acc_no'];
$info = $n['info'];
}
}
?>
<html>
<head>
<title>JSK</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form').hide();
$('p').hide();
$('#sp').hide();
$("#inf").click(function(){
$("p").slideToggle();
$('#sp').slideToggle();
});
$("#fo").click(function(){
$("form").slideToggle();
});
});
</script>
</head>
<body>
<div class="container">
<div class="left">
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>
<?php $results = mysqli_query($db, "SELECT * FROM records"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Account No</th>
<th>Info</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['acc_no']; ?></td>
<td><button id="inf" onclick="myFunction()">show</button></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
Delete
</td>
</tr>
<tr id="sp"> <td colspan="4"><p> <?php echo $row['info']; ?> </p></td>
</tr>
<?php } ?>
</table>
<div id="fotable" align="center">
<button id="fo">Add New/Edit</button>
</div>
<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" autocomplete="off" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Account No</label>
<input type="text" name="acc" value="<?php echo $acc; ?>">
</div>
<div class="input-group">
<label for="info">Info</label>
<textarea class="form-control" rows="8" name="info" id="info"><?php echo $row['info']; ?></textarea>
</div>
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Add Account</button>
<?php endif ?>
</div>
</form>
</div><!-- left closed -->
<div class="right">
hello
</div> <!-- right closed -->
</div> <!-- container closed -->
</body>
</html>
---php_code.php-----
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'jskrecords');
// initialize variables
$name = "";
$acc = "";
$info = "";
$id = 0;
$update = false;
if (isset($_POST['save'])) {
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "INSERT INTO records (name, acc_no, info) VALUES ('$name', '$acc', '$info')");
$_SESSION['message'] = "Account saved";
header('location: index.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "UPDATE records SET name='$name',acc_no='$acc',info='$info' WHERE id=$id");
$_SESSION['message'] = "Account updated!";
header('location: index.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM records WHERE id=$id");
$_SESSION['message'] = "ACC deleted!";
header('location: index.php');
}
?>
Concept:
If you are creating multiple form from same mysql table using php script, You need to give each form a unique id.
e.g.
<form method="post" action="php_code.php" id="form<?= $id ?>">
Then add data-target="#form" to button with class 'inf'. It will store id of form.
<button class="inf" data-target="#form<?= $id ?>">show</button>
When button is clicked we know which form to open from data-target.
<script>
$('.container').on('click','button.inf',function(e){
e.preventDefault();
var formid=$(this).attr('data-target'); //get value of data-target attribute
//...proceed to play toggle with this form 'formid'

Passing Hidden ID value to another page using javascript php

I am trying to pass hidden value from page to another page and it work fine only for first record however for other records it's showing error
Here is the code:
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_id" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead">
<?php echo $row["jdes"] ?>
</p>
<button type="button" id="requestthis" class="btn btn-primary">
Request
</button>
<?php
}
} else {
echo "Nothing to display Yet";
}
?>
jobs-inner.php
<?php
echo $_GET['hidden_id'];
?>
Javascript:-
$(function() { //ready function
$('#requestthis').on('click', function(e){ //click event
e.preventDefault();
var hidden_id = $('#hidden_user_id').val();
var url = "jobs-inner.php?hidden_id="+hidden_id;
window.location.replace(url);
})
})
Error:-
Undefined index: hidden_id in C:\wamp64\www\project\jobs-inner.php on line 3
It might be a simple problem but I am a beginner and I can't figure it out.
Your value is unique but the id isn't. Make the id of the input unique something like below.
<input type="hidden" id="hidden_user_<?php echo $row["id"] ?>" value="<?php echo $row["id"] ?>">
but you would have to do a count on code below to make it display base on how many rows you have.
<?php
echo $_GET['hidden_id'];
?>
Without JavaScript
$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
$count = 1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<input type="hidden" id="hidden_user_<?php echo $count ?>" value="<?php echo $row["id"] ?>">
<h3><?php echo $row["title"] ?>:</h3>
<p class="lead"><?php echo $row["jdes"] ?></p>
<form id="<?php echo $count ?>" action="jobs-inner.php?hidden_id=<?php echo $row["id"] ?>" method="post">
<input type="submit" vaule="Request">
</form>
<?php
$count++;
}
} else {
echo "Nothing to display Yet";
}
?>

Get patient details on combo box change

I need the code for getting the addr and pnum of the patient when I choose the pname in the combo box.
How can I do that?
<script>
function getVal() {
document.getElementById("text2").value = document.getElementById("model").value;
}
</script>
<body>
//code in opening and getting my addr and pnum in dbase
<?php
include('connect.php');
$pname=$_GET['tpname'];
$result = mysql_query("SELECT * FROM tblnpatient where pname='$pname'");
while($row = mysql_fetch_array($result))
{
$pnum=$row['pnum'];
$addr=$row['addr'];
}
?>
//code for choosing pname
<tr><td>Patient Name:
<div id="ma">
<select name="pname" class="textfields" id="model" style="width:180px;" onchange="getVal();">
<option id="0" >--Select Patient Name--</option>
<?php
$con=mysqli_connect("localhost","root","","dbnpatient");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$pnum=$_GET['pnum'];
$query = mysqli_query($con, "SELECT * FROM tblnpatient");
while($row = mysqli_fetch_array($query)){
$pnum = $row['pnum'];
$pname = $row['pname'];
?>
<option id="<?php echo $pnum; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?> </option>
<?php } ?>
</select>
//code for getting pname and addr
Address:<input type="text" name="ename" size="20" id="ma" value="<?php echo $addr ?>"/>
Name:<input type="text" name="ename" size="20" id="ma" value="<?php echo $pname ?>"/>
In your while loop add the following (if you have that column otherwise replace with something similar)
$paddress = $row['paddress'];
You can store the needed information by using the data attribute in your options
<option id="<?php echo $pnum; ?>" data-pname="<?php echo $pname; ?>" data-paddress="<?php echo $paddress; ?>" value="<?php echo $pname; ?>"><?php echo $pname; ?></option>
Then change your getVal() function
function getVal() {
var options = document.getElementById("model").options;
if (options.selectedIndex != -1) {
var addr = document.getElementById('paddress');
var name = document.getElementById('pname');
addr.value = options[options.selectedIndex].getAttribute('data-paddress');
name.value = options[options.selectedIndex].getAttribute('data-pname');
}
}
Now change the id's of the input fields for the address and the name to paddress and pname. It is important to know to never have duplicate id's
I hope that helped

Split content of select into different fields

I'm trying to send the values that are separated by an pipe to 2 different fields. For some reason it won't work.
Any ideas?
<script type="text/javascript">
function folder(selectVeld)
{
// Stuur informatie terug
var id = (selectVeld.options[selectVeld.selectedIndex].value).split('|');
// Id
document.getElementsByName('folder_id').value = id[0];
// Naam
document.getElementsByName('folder_naam').value = id[1];
}
</script>
<tr>
<td><?php echo $lang['folder']; ?>:</td>
<td><select name="folder" onChange="folder(this)">
<option value="geen"><?php echo $lang['no_folder']; ?></option>
<?php
$sql = "SELECT id, naam FROM 2_folder ORDER BY naam ASC";
$res = mysql_query($sql,$con);
while ($row = mysql_fetch_assoc($res)){
?>
<option <?php if($row['id'].'|'.$row['naam'] == $folder){ echo 'selected="selected"'; } ?> value="<?php echo $row['id'].'|'.$row['naam']; ?>"><?php echo $row['naam'] ?></option>
<?php } ?></select>
</td>
</tr>
<input type="text" name="folder_id" value="<?php echo $folder_id; ?>" size="5" style="background-color: #e7e7e9" readonly="readonly" />
<input type="text" name="folder_naam" value="<?php echo $folder_naam; ?>" size="5" style="background-color: #e7e7e9" readonly="readonly" />
document.getElementsByName returns a array of elements, even if there is only a single element with that name.
document.getElementsByName('folder_id')[0].value = id[0];
document.getElementsByName('folder_naam')[0].value = id[1];
should do the trick. jsfiddle sample
I have avoided this problem by using only the folder_id and find the name with an seperate query after sending the form.

Categories

Resources