Javascript drag and drop div styling - php

So I'm trying to make a "Simple" php + javascript drag and drop, here's my code:
<title>Web Editor</title>
<?php
mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
if(isset($_POST['submit'])){
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url="http://www.bluejayke.com/edit/uploaded/$name";
}
?>
<?php
if(isset($_POST['submit'])){
mysql_query("INSERT INTO uploadedvideos(id,name,url) VALUES('','$name','$url')");
echo "</br>" . $name . " uploaded";
}
$query="SELECT * FROM uploadedvideos";
$result = mysql_query($query);
?>
<div class='wrapper' id='wrapper'>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td style="width:400px; height:50px;"><form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form></td>
</tr>
<tr>
<td style="width:400px; height:500px;">
<div style="width:100%; height:100%; overflow:auto; ">
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
</script>
<?php
while($row=mysql_fetch_array($result))
{
$id=$row['id'];
$name=$row['name'];
$url=$row['url'];
echo "<div style='position:absolute;cursor:pointer;' id='".$id."'><a href='javascript: click(\"$url\")'>$name</br><embed id='$id' src='$url' width='120' height='120' draggable='true' ondragstart='drag(event)'></embed></a><input type='button' id='delete' value='X' onclick='return Deleteqry($id)' /></div><br> ";
?>
<script type="text/javascript">
var dragging = false
$(function () {
var target<?php echo $id ?> = $('#<?php echo $id; ?>')
target<?php echo $id ?>.mousedown(function() { dragging<?php echo $id; ?> = true })
$(document).mouseup(function() { dragging<?php echo $id; ?> = false })
$(document).mousemove(function(e) {
if(dragging<?php echo $id; ?>){
target<?php echo $id ?>.css('left', e.pageX)
target<?php echo $id ?>.css('top', e.pageY)
}
})
})
</script>
<?php
}
?>
</div>
</table>
</div>
<div class='wactch' id='watching' height='480' width='720'>Loading..</div>
<script>
/*document.onmousemove=mouseMove;
function mouseMove(ev)
{
ev = ev || window.event;
var mousePos=mouseCords(ev);
}
function mouseCords(ev)
{
if(ev.pageX || ev.pageY)
{
return{x:ev.pageX,y:ev.pageY);
}
return
{
x:ev.clientX+document.body.scrollLeft-document.body.clientLeft, y:ev.clientY.document.body.scrollTop-document.body.clientTop
};
}
}*/
function Deleteqry(id)
{
if(confirm("Are you sure you want to delete this picture?")==true)
window.location="index.php?del="+id;
return false;
}
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
window.onload=function()
{
click("http://www.bluejayke.com/edit/uploaded/dpnewslogo.png");
}
function click(url)
{
document.getElementById("watching").innerHTML='<embed src="'+url+'" width=720 height=480></embed>';
}
</script>
<?php
if($_GET['del']){
$delete_id=$_GET['del'];
mysql_query("DELETE FROM `uploadedvideos` WHERE `uploadedvideos`.`id` = $delete_id");
header("location: index.php");
}
?>
<style type="text/css">
.wrapper
{
width:500px;
overflow: hidden;
float: left;
}
#watching {
float: right;
}
.div1 {width:750px;height:120px;padding:10px;border:1px solid #aaaaaa; oveflow: hidden;}
</style>
The important part is in the while loop. The result of this code is all the divs I made in the while loop are piled up on top of each other, when I want them in a vertical line-like structure. Anyone have any insight on this? It's something with the div's styling I think, but I'm not sure how to resolve it while still keeping the drag and drop function.

Your divs are being positioned absolute. You simply need to change them to position:relative;
http://www.w3schools.com/css/css_positioning.asp

Related

Fill table from input from mysql

I have an input, in which a code is entered and filled the table below with information from mysql optenida, the question is that I want every time a code is entered, the table all the data is added (without deleting the previous ). I got the idea to do with Ajax, but do not know where to start. So you see there is an easier way that I'm not seeing (finding on google). I do not like to add this data to a table, I would like it to be temporarily (until the table is confirmed, will be added to the db).
Any ideas?
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
table {
width:100%;
border: 1px solid black;
border-collapse: collapse;
}
td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="input_codigo" placeholder="Codigo del producto" autocomplete="off" autofocus required><br><br>
</form>
<table>
<tr>
<td><b>Codigo</b></td>
<td><b>Descripcion</b></td>
<td><b>Precio</b></td>
<td><b>Cantidad</b></td>
<td><b>Importe</b></td>
</tr>
<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
require ("conectar.php");
$_SESSION["codigo"] = $_POST["input_codigo"];
$sql = "SELECT * FROM $tabla WHERE codigo = ".$_SESSION['codigo']."";
$result = $conexion->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>".$row["codigo"]."</td>
<td>".$row["descripcion"]."</td>
<td>$".$row["venta"]."</td>
<td><input type='number' name='cantidad' value='1' min='1' max='5'></td>
<td>$".$row["venta"]."</td>
</tr>
";
}
} else {
echo "";
}
$conexion->close();
?>
</table>
</body>
</html>
Maybe something like this i write below.
Added jQuery and Ajax request to get the data and then add it to the table.
Changed the PHP a little so that the main HTML is not returned if it is and AJAX request.
Hope it works for you (i didnt test it).
<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$bAjaxRequest = false;
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$bAjaxRequest = true;
}
// if not and ajax request deliver the complete HTML
if(!$bAjaxRequest) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
table {
width:100%;
border: 1px solid black;
border-collapse: collapse;
}
td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<form action="index.php" method="post" id="frmQuery" name="frmQuery">
<input type="text" name="input_codigo" id="input_codigo" placeholder="Codigo del producto" autocomplete="off" autofocus required><br><br>
</form>
<table id="tblData" name="tblData">
<tr>
<td><b>Codigo</b></td>
<td><b>Descripcion</b></td>
<td><b>Precio</b></td>
<td><b>Cantidad</b></td>
<td><b>Importe</b></td>
</tr>
<?php
} // end if(!$bAjaxRequest) {
// we are always going to return the TR's or ""
require ("conectar.php");
// ALWAYS, BUT ALWAYS VERIFY/VALIDATE USER INPUT!!!
$_SESSION["codigo"] = mysql_real_escape_string($_POST["input_codigo"]); // for example
$sql = "SELECT * FROM $tabla WHERE codigo = ".$_SESSION['codigo']."";
$result = $conexion->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>".$row["codigo"]."</td>
<td>".$row["descripcion"]."</td>
<td>$".$row["venta"]."</td>
<td><input type='number' name='cantidad' value='1' min='1' max='5'></td>
<td>$".$row["venta"]."</td>
</tr>
";
}
} else {
echo "";
}
$conexion->close();
// if not and ajax request deliver the complete HTML
if(!$bAjaxRequest) { ?>
</table>
<script type="text/javascript">
function loadData(codigo) {
$.post( "index.php", { input_codigo: codigo }, function( data ) {
$("#tblData").append(data);
});
}
$(function() {
// jQuery POST are never cached, but if you change to GET you'll need this next line
//$.ajaxSetup ({ cache: false });
$("#frmQuery").submit(function(e) {
e.preventDefault();
loadData($("#input_codigo").val());
});
});
</script>
</body>
</html>
<?php
}
?>

run php mysql_query on onclick div

I need to put a mysql_query in the onClick event of a div. I tried this, but it does not work.
notifiche.php
while ($i < $result) {
$id_noti = mysql_result($res, $i, "id");
$msg=mysql_result($res,$i,"msg");
?>
<div class="box" align="left" style="color: #5c5c5c; padding:4px; border-bottom: 1px solid #c0c0c0; width: 170px !important; position:relative;z-index: auto;">
<div onClick="doSomething();" class="close_box" align="right"style="font-weight:bold; position:absolute;right:0; top:0; z-index:1;cursor:pointer;">
x
<input type="hidden" name="del_noti" value="<?echo $id_noti;?>">
</div>
<? echo $msg; ?>
</div>
<? $i++;
}
the script in the head of main page is
<script type="text/javascript">
function doSomething(){
$.post("del_notif.php");
return false;
}
</script>
and the del_notif.php
include 'files/config.php';
mysql_query("UPDATE notifiche SET a='delete' WHERE id='$_POST[del_noti]'");
You have to pass the argument to your doSomething function
<div onClick="doSomething(<?echo $id_noti;?>)"...>
and in your script
<script type="text/javascript">
function doSomething(id){
$.post("del_notif.php", {id: id});
return false;
}
</script>
and on the server
include 'files/config.php';
mysql_query("UPDATE notifiche SET a='delete' WHERE id=". (int) $_POST["id"]);

To redirect the url

in the below code i am searching a data before searching my url will be in coursemaster_site and when i search it will be in coursemaster_site/index1 and when i close the search results the url will be in coursemaster_site /coursemaster_site but i want url to be in coursemaster_site after closing the search results.
controller:coursemaster_site
function index1()
{
$data = array();
$keyword = $this->input->post('keyword');
if($keyword!=""){
$data['results'] = $this->coursemaster_model->search($keyword);
}
$this->load->view('coursemaster_view', $data);
}
view:coursemaster_view
<form action="<?php echo site_url('coursemaster_site/index1');?>" method = "post">
<br/><center>SEARCH:<input type="text" name = "keyword" required/>
<input type="submit" name="submit" id="opn" value = "Search" onClick="hide1('hiddendiv')" class="btn-success btn" /></center>
</form>
<?php
if (isset($_POST['submit'])) { // Here
// Do the search here
if($results){
?> <div id='hideme'>
CLOSE<a href='coursemaster_site' 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:1000px; height: 540px; position: absolute; left: 20%; top: 35%; margin-left: -100px; margin-top: -120px" id="modal" >
<table class="display2 table table-bordered table-striped" id='results'>
<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"<div id='hideme'>
CLOSE<a href='coursemaster_site' class='close_notification' title='Click to Close'>";
echo "<div id='modal' style='background:#FFFFFF; width:1000px; height: 525px; position: absolute; left: 20%; top: 35%; margin-left: -100px; margin-top: -110px'>";
}
echo"no results";
echo'</div>';
echo '</div>'; }
// If closing
?>
</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';
}
function hide1(obj) {
var el = document.getElementById(obj);
el.style.display = 'none';
}
</script>
Change to:
<div id='hideme'>CLOSE
<a href='/coursemaster_site' class='close_notification' title='Click to Close'>
...
on both places. The leading slash makes the href start from root.
Do the following changes and check if it works or not:
<div id='hideme'>
CLOSE<a href='javascript:void(0);' class='close_notification' title='Click to Close'>";

else statement is displaying

In this below code when i execuete it it shows no results.But i place no results in if-else condition .Actual result: textbox ,submit button and no results..My expected result is a textbox and button should be displayed and not no results.Without searching a data it displays no reults.So 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>
You are checking if $results variable is set or not.
Instead you need to check for if $results is empty or not.
Replace
if($results){
With
if(!empty($results)){
and also make sure that $results contains the array. I mean your variable must have value.
As far as I can tell from your code, $results is not set. Therefore if ($results) evaluates as false and else is displayed.
check the request is a post request or a default page load
use
else if($_SERVER['REQUEST_METHOD'] === 'POST')
{
echo "no results";
}
instead of
else
{
echo "no results";
}

Setting backgorund colors to canvas in Jquery

I have my code that displays 92 selectors and each selector has a canvas(where is set a background color depending on the value from selector), in Jquery I set backgorund colors to canvas for the each value form selector, my problem is that when I click other selector it sets the backround color to the first Canvas(form the first selector but not on his own Canvas), I have 92 selector and each have CANVAS, how can I manage to do this in JQUERY...
Code
<html>
<head>
<title>Tests</title>
<style type="text/css">
.table-container {
display: inline-table;
}
table {
width: 230px;
}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('select').change(function() {
var selected = $(this).find(':selected').val();
if (selected == 'Forms') {
$('#myCanvas').css('background','green');
}
if (selected == 'language Syntax') {
$('#myCanvas').css('background','yellow');
}
if (selected == 'Fundamentals') {
$('#myCanvas').css('background','red');
}
if (selected == 'Advanced Concepts') {
$('#myCanvas').css('background','blue');
}
if (selected == 'New Concepts in PHP5') {
$('#myCanvas').css('background','violet');
}
if (selected == 'Operators and Functions') {
$('#myCanvas').css('background','black');
}
if (selected == 'Variables and Datatypes') {
$('#myCanvas').css('background','brown');
}
});
});
</script>
</head>
<body>
<h3>Tests</h3>
<div class="table-container">
<table border="3">
<tr>
<th>
<?php
$con = mysql_connect("localhost","root","sergios.com");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("phptests", $con);
$result1 = mysql_query("SELECT * FROM question");
for($i=1;$i<93;++$i)
{
$result = mysql_query("SELECT * FROM Category");
echo "Number:".$i."<br />";
echo "<select>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<option>" . $line['name'] . "</option>";
}
echo "</select>";
?>
<canvas id="myCanvas" width="20" height="20" style="border:1px solid #c3c3c3;">
</canvas>
From what I observed, you are changing the same canvas element's color (id="myCanvas" => '#myCanvas'). Therefore, you need a way to uniquely associate each unique selector with each unique canvas.
There are more elegant ways of recoding what you have coded. Due to the constraint of time, here is a sample by utilizing html element class names.
// in php //////////
<?php
for($i=1;$i<93;++$i){
$result = mysql_query("SELECT * FROM Category");
?>
<br />
Number: <?php echo $i; ?>
<select class="<?php echo 'myCanvas_' . $i; ?>">
<?php
while($line = mysql_fetch_assoc($result)){
?>
<option value="<?php echo $line['name']; ?>"><?php echo $line['name']; ?></option>
<?php
}
?>
</select>
<canvas id="<?php echo 'myCanvas_' . $i; ?>" width="20" height="20" style="border:1px solid #c3c3c3;"></canvas>
<?php
}
?>
$(document).ready(function(){
$('select').change(function() {
var toChangeCanvasId = '#' + $(this).attr('class');
var selected = $(this).find(':selected').val();
if (selected == 'Forms') {
$(toChangeCanvasId).css('background','green');
}
if (selected == 'Fundamentals') {
$(toChangeCanvasId).css('background','red');
}
});
});​

Categories