I am doing a sticky note web site where users can post msg etc onto post it notes. I am using jQuery UI with PHP. I was hoping to use AJAX to make it responsive but am having some issues with the page updating via ajax.
The main issue is my jQuery appears to only work when the user enters the site for the first time, when they add some info through a form (and to database) my jQuery UI behaviour (i.e. drag and drop, getting attributes and position elements with CSS) stops working until a page refresh. It is like my ajax is passing new looped PHP content and jQuery is no longer recognising the .class I have applied!
Hopefully this or my code makes sense, I am still new to ajax and coding in general.
Main page – pert.php
<html>
<header>
<link rel="stylesheet" href="style.css" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css"/>
<script type="text/javascript"
src="js/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript"
src="js/fancybox/jquery.easing-1.3.pack.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
$("#addtask").submit(function() {
//$("#form").serialize();
$.post('update.php', $("#addtask").serialize(),function(data){
$("#result").html(data);
});
return false; //stop browser refresh
});
//looping through Post it Notes to apply to all within class
$(".task").each(function(index){
//store x, y from looped php
var x = $(this).attr("x");
var y = $(this).attr("y");
//assign looped php to css
$(this).css("left",x);
$(this).css("top",y);
//attach draggable behaviour
$(this).draggable({
cursor: 'move',
stack: '.post',
opacity: '0.5',
containment: '#container'
});
//store variables x, y and id for database via mouse move and ajax
$(this).mouseup(function(){
var coord=$(this).position();
var h = $(this).height();
var w = $(this).width();
var coordLeft = coord.left;
var coordTop = coord.top;
var noteid = $(this).attr("taskid");
})//loop done
})//jquery end
</script>
</header>
<body>
<h4>Draggable Stickies</h4>
<!--<p> Click here to <a class = "addimage" href="addimage.html">Add image</a> </p>-->
<form id="addtask">
Task Desc:<input type="text" name="taskdesc"/>
<input type="submit" value="Enter"/>
</form>
<div id="result">
<?
include "connectdb.php";
//looping through stored desc
$querySticky=mysql_query("SELECT * FROM tasks");
while($r=mysql_fetch_array($querySticky)){?>
<div class="task" x="<? echo $r["x"]; ?>" y="<? echo $r["y"]; ?>"
id="<? echo $r["taskid"]; ?>">
x=<? echo $r["x"]; ?> y=<? echo $r["y"]; ?><p><? echo $r["taskdesc"];?></p>
</div>
<?}?>
</div>
</body>
</html>
accessing database – update.php
<? include("connectdb.php");
if(ISSET($_POST['taskdesc'])){
$taskdesc = $_POST['taskdesc'];
$queryinsert1="INSERT INTO tasks(taskdesc)VALUES('$taskdesc')";
$result=mysql_query($queryinsert1);
}
//for updating coordinates in database not in use y
if(ISSET($_POST['taskid'])){
$x = $_POST['x'];
$y = $_POST['y'];
$id = $_POST['taskid'];
$addxyquery="UPDATE tasks SET x = '$x', y ='$y' WHERE taskid='$id'";
$result=mysql_query($addxyquery);
}
//looping through stored desc for updated results
$querySticky=mysql_query("SELECT * FROM tasks");
while($r=mysql_fetch_array($querySticky)){?>
<div class="task" x="<? echo $r["x"]; ?>" y="<? echo $r["y"]; ?>" id="<? echo $r["taskid"]; ?>">
x=<? echo $r["x"]; ?> y=<? echo $r["y"]; ?><p><? echo $r["taskdesc"];?></p>
</div>
<?}?>
When the Ajax call returns, you set
But then there is new html in the result-div which isn’t draggable anymore.
You should execute the javascript to make things draggable again, after the Ajax call.