简体   繁体   English

如何捕获使用Javascript创建的DOM节点并使用JQuery AJAX和PHP保存在MySQL中

[英]How to capture DOM nodes created with Javascript and save in MySQL using JQuery AJAX & PHP

I have a simply Javascript client script that consist of an an HTML button and when clicked it creates new DOM nodes, each with their own ID per an incrementing counter. 我有一个简单的Javascript客户端脚本,其中包含一个HTML按钮,单击该脚本时会创建新的DOM节点,每个DOM节点都有自己的ID。 For each click the dom node name ( div1, div2, div3, etc) gets pushed to an array that holds one div at a time. 对于每次单击,dom节点名称(div1,div2,div3等)将被推送到一次包含一个div的数组。

When the user clicks #orange-button I want each DOM node to be stored to mysql via PHP for later recall. 当用户单击#orange-button时,我希望每个DOM节点都通过PHP存储到mysql,以便以后调用。

Below is what I have thus far & I commented what I do not understand. 以下是我到目前为止的内容,并评论了我不了解的内容。

Javascript Java脚本

var temp = [];

$('#orange-button').click(function(){
        $.ajax({
            type: 'POST',
            url: 'add.php',
            data: temp,             // Not sure if this is right !
            success: function(){
                $('#success').html();
            }
        });
    });

PHP PHP

$gimme = $_POST[temp];   // Not sure how to do this line

$sql="INSERT INTO synths (domID)
VALUES ('{$gimme}')";

Assuming your "temp" variable will hold an array of IDs 假设您的“临时”变量将包含一个ID数组

$.ajax({
    type: 'POST',
    url: 'add.php',
    data: {mydata: temp},             // Pass the data as json
    success: function(){
        $('#success').html();
    }
});

$gimme = $_POST['mydata'];   // I called the POST variable "mydata"

foreach($gimme as $value){
    $sql="INSERT INTO synths domID = $value";
    //then execute it
}

Don't insert them all in one query. 不要将它们全部插入一个查询中。 Instead do it in a loop. 而是循环执行。

But really should use PDO instead: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers 但实际上应该改用PDO: http : //wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

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

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