0

(只是提醒一下,这是一个冗长的问题,但我确定它对于 ajax-php 编码器来说是非常基本的问题)我试图“在一个页面上的某些拖放事件上更新 db”并“在不重新加载的情况下反映其他页面中的更改'。我已经写了几乎所有的代码,需要你的帮助来找出问题所在。这是我写的Html,

First_html_file:

<head>
    <title>Coconuts into Gunnybags</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <script type="text/javascript" src="script.js"></script>
</head>

<body>
    <div id="coconuts" style="float:left">
        <div class="coconut1" ondragover="allowDrop(event)" ondrop="drop(event)">
            <img id="drag1" ondragstart="drag(event)" draggable="true" src="coconut.png">
        </div>
        <div class="coconut2" ondragover="allowDrop(event)" ondrop="drop(event)">
            <img id="drag2" ondragstart="drag(event)" draggable="true" src="coconut.png">
        </div>

    </div>

    <div class="gunnybag" style="float:right">
        <div id="place1" ondragover="allowDrop(event)" ondrop="drop(event)"></div>
        <div id="place2" ondragover="allowDrop(event)" ondrop="drop(event)"></div>

    </div>
</body>

所以有 2 个可拖动的椰子和 2 个占位符(place1 和 place2)。我想要做的是当椰子被拖动并放置在占位符之一上时,应该更新数据库的值。(比如说当椰子放在第一个占位符中时,place_id 1 - true,place_id 2 - false)

为此,我正在像这样从 JS 的 drop 函数对 php 文件进行 ajax 调用。

JS_文件:

function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("coconut");
ev.target.appendChild(document.getElementById(data));
    var state = true;           
    var id = ev.target.id;
$.ajax({
     url: "db_update.php",        //calling db update file.
     type: "POST",
     data: { id: id, state: state },       //2 variables place_id and its     state(True/False)
     cache: false,
     success: function (response) {    //I dont know what to do on success. Can this be left blank like, success:         ?
         $('#text').html(response);
     }
 });
}

这是我的 db_update,db_update:

    <?php

    $state = $_POST['state'];       //getting my variables state 'n ID
    $id = $_POST['id'];

    function begin()
    {
    mysql_query("BEGIN");
    }

    function commit()
    {
    mysql_query("COMMIT");
    }

    $con=mysql_connect("sqlservername","myuname", "mypass") or die(mysql_error());

    mysql_select_db("my_db", $con) or die(mysql_error());

    $query = "UPDATE gunnybag SET state = '{$state}' where id='{$id}'";  //will this work? or am I doing something wrong here??

    begin();

    $result = mysql_query($query);

    if($result)
    {
    commit(); 
    echo "successful";
    }

    ?>

在接收端,我想在不重新加载页面的情况下更新麻袋中的椰子,所以我编写了这个使用 db_fetch.php 的 ajax

ajx.js 文件:

window.onLoad = doAjax;

function doAjax(){
$.ajax({
url: "db_fetch.php",
dataType: "json",
success: function(json){
    var dataArray = JSON.decode(json);
    dataArray.each(function(entry){
        var i=1;
        if(entry.valueName==true){
            $q('place'+i).css( "display","block" );
        }
        else{
            $q('place'+i).css( "display","none" );
        }
        i=i++;
    })
}

}).complete(function(){
      setTimeout(function(){doAjax();}, 10000);
    });
}

这是 db_fetch.php:

<?php
try{
  $con=mysql_connect("sqlservername","myuname", "mypass") or die(mysql_error());
}
catch(Exception $e){
    echo $e;
}
mysql_select_db("my_db", $con) or die(mysql_error());

$q = mysql_query("SELECT 'state' FROM 'gunnybag' ");  //fetching all STATE from db
$query = mysql_query($q, $con);
$results = mysql_fetch_assoc($query);
echo json_encode($results);              //making it JSON obj

?>

最后是我调用这个ajax的另一个页面。second_html_file:

    <head>
    <title>Coconuts into Gunnybags</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    <script type="text/javascript" src="ajx.js"></script>  
               //if i simply include the ajax script here will it be called 
             //automatically? i want this script to keep up with the changes in db.
</head>

<body>
    <div class="gunnybag" style="float:right">
        <div id="place1" style="display: ;"><img id="drag1"  draggable="true" src="coconut.png"></div>
        <div id="place2" style="display: ;"><img id="drag2"  draggable="true" src="coconut.png"></div>

    </div>
</body>

MAP:First_html_file->JS_file->db_update :: Second_html_file->ajx.js->db_fetch。

请指出这段代码有什么问题,同时回复代码中的//注释。非常感谢您的回复。谢谢!#help me get this right# For ref 我在这里托管了文件, http: //www.nagendra.0fees.net/admin.html & http://www.nagendra.0fees.net/cng.html

4

1 回答 1

1

我看到的第一件事是:

你说:

var id = event.target.id;

但是你在 drop(ev) 中 decalare ev

所以改变:

var id = event.target.id;

至:

var id = ev.target.id;

对于初学者。

然后你应该使用mysqli因为 mysql 已被弃用:

您的代码也对 SQL 注入开放,因此请更改:

$state = $_POST['state'];       //getting my variables state 'n ID
$id = $_POST['id'];

至:

$state = ($_POST['state']) ? true : false;       
$id = intval($_POST['id']); //make sure an integer
于 2013-04-12T08:32:17.750 回答