I have codes for drag and drop for images in a certain area, but I want to get the value to insert to the database if the first image has a value of 1 then how can I insert into database? I 开发者_JAVA技巧am newbie.
Without an example of your code already this is going to be hard, but here goes...
If you have your code for dragging and dropping I'll assume you know some Javascript and are able to get the x and y coordinates you wish to save for the images in question.
You will need to use AJAX to communicate with a server to store the coordinates, on the server you will use PHP to UPDATE
the coordinates into the database.
An example using AJAX from jQuery might look like this
// Get X and Y and assign to javascript variables, you should do this using your existing drag and drop code
// There is also a variable called imgId this might be an identifier for the image which was dragged and dropped
$.ajax({
// POST the data to the script
type: "POST",
// This is your PHP script
url: "save-coords.php",
// These are the variables you'll be POSTing to the PHP script
data: "x=" + x + "&y=" + y + "&imgId=" + imgId,
// Here is a function which will be called when the script is successful
success: function(msg){
alert( "Data Saved: " + msg );
}
});
Your PHP script called save-coords.php
might look something like this
<?php
// Get the variables posted from the AJAX script
$x = $_POST['x'];
$y = $_POST['y'];
$imgId = $_POST['imgId'];
// Create a connection to a database, there are lots of how-tos for this about, here is a quick example
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link)
die('Could not connect: ' . mysql_error());
// Create query to update the images X and Y coordinates
$query = "UPDATE `images`" .
"SET `x` = '" . mysql_escape_string($x) . "', " .
"`y` = '" . mysql_escape_string($y) . "' " .
"WHERE `imgId` = '" . mysql_escape_string($imgId) . "';"
// Run the query and get the result
$result = mysql_query($query);
// Close database connection
mysql_close($link);
// If the result was OK the echo success
if($result)
echo "Success";
// Else echo Error
else
echo "Error";
The echo
result will be picked up by your AJAX script and will be output using the alert()
function in Javascript.
I hope that helps and gets you started, I haven't run it, but it is a start.
精彩评论