Jump to content

Use Cookies or Database?


RottenBananas

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.