RottenBananas Posted September 30, 2008 Share Posted September 30, 2008 Hey guys, For my site im using javascript to allow users to customize their profiles by dragging content around. Now I need a way to save the positions of each divider so that when anyone visits user X's profile they see the position user X saved everything to last. I was thinking of using javascript to return offsetLeft and offsetTop and save the values into a mysql database and then spit them back out when that users page is visited. Is their a better way or is the db idea good? Can I save a cookie when user X saves positions and send the cookie with saved positions to anyone that visits his page so they see his customization? Hope this isnt too vague Thanks Quote Link to comment Share on other sites More sharing options...
svivian Posted September 30, 2008 Share Posted September 30, 2008 If these users will always have accounts then you may as well use the database. And if you want other users to see his customisations then it's the only method. If not, then you'd have to store the IP in the database in order to recognise him when he comes back - but users can change IP pretty regularly (e.g. if their connection goes down) so the cookie approach is probably better in that case. Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted September 30, 2008 Author Share Posted September 30, 2008 Thanks for the reply, The users will always have an account, Ok I dont know how to code at all and im kinda pullin all this outta nowhere and hoping it works...reading through random tutorials and such. If im approaching it the wrong way please tell me (I dont know any better) Here's what I tried... 2 Javascript fucntions function getpos() { var Top = document.getElementById('mydiv').offsetTop ; var Left = document.getElementById('mydiv').offsetLeft; return {Top:Top,Left:Left} } function init() { var pos = getpos(); document.myform.Top.value = pos.Top; document.myform.Left.value = pos.Left; } Then the html hidden fields in my form <form name="myform" action="savepos.php" method="post"> <input type="hidden" name="Top"> <input type="hidden" name="Left"> <input type="submit" name="submit" value="Save" onClick=init()> </form> And finally savepos.php which writes it to my db $Top = $_POST['Top']; $Left = $_POST['Left']; $sql = "SELECT profile_id FROM positions WHERE profile_id=".$_SESSION['uid'].";"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) { $sql2 = "INSERT into positions (profile_id,Top,Left) VALUES(".$_SESSION['uid'].",'$Top','$Left');"; $res2 = mysql_query($sql2) or die(mysql_error()); } else { $sql3 = "UPDATE positions SET Top='$Top',Left='$Left' WHERE profile_id=".$_SESSION['uid'].";"; $res3 = mysql_query($sql3) or die(mysql_error()); } echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your profile layout has been updated\") document.location.href='home.php'</script>"; } This example only shows one divider that you can drag but you get the idea for adding more. The problem is that if the user doesnt drag it and leaves it as is it resets the positions to 0. For example I have 2 divs, mydiv and yourdiv...if the user moves mydiv to top=250; left=250; but doesnt touch yourdiv and clicks save...its sets yourdiv to top=0; left=0; Any suggestions? Am I approaching this the right way? Thanks alot Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted October 2, 2008 Author Share Posted October 2, 2008 Anyone ??? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted October 2, 2008 Share Posted October 2, 2008 the way I would do it is like so: 1. javascript function(s) for drop and drag functionality 2. after the drop do an ajax call to a php file that updates the database 3. In the php you called make sure the user is the profile owner using sessions Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted October 3, 2008 Author Share Posted October 3, 2008 Thanks for the reply, I have all this code written already though does anyone know whats wrong with it? How can I do it using ajax? Could you give me some sample code? Im not understanding why my method isn't working Thanks again Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted October 7, 2008 Author Share Posted October 7, 2008 Should I post this in a different category? Quote Link to comment Share on other sites More sharing options...
RottenBananas Posted October 8, 2008 Author Share Posted October 8, 2008 Nobody knows? :'( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.