简体   繁体   English

如何从已拖放到特定区域并插入数据库的图像中获取值?

[英]how can i get the value from image that has been drag and drop to a certain area and insert to database?

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? 我在特定区域具有用于图像拖放的代码,但是如果第一张图像的值为1,我想获取要插入到数据库中的值,然后如何插入数据库中? I 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. 如果您有用于拖放的代码,那么我假设您知道一些Javascript,并且能够获取要保存的图像的x和y坐标。

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. 您将需要使用AJAX与服务器进行通信以存储坐标,在服务器上,您将使用PHP将坐标UPDATE到数据库中。

An example using AJAX from jQuery might look like this 使用jQuery中的AJAX的示例可能看起来像这样

// 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 您的名为save-coords.php PHP脚本可能看起来像这样

<?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. echo结果将由您的AJAX脚本获取,并将使用Javascript中的alert()函数输出。

I hope that helps and gets you started, I haven't run it, but it is a start. 希望对您有所帮助,并且可以帮助您入门,但我还没有开始运行,但这只是一个开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM