简体   繁体   English

通过PHP将JQuery生成的元素连接到数据库

[英]Connecting JQuery generated elements to a database via PHP

I have a JQuery script which dynamically generates HTML elements on button click. 我有一个JQuery脚本,它可以在单击按钮时动态生成HTML元素。

I am connecting to a mysql database using PDO and PHP but am wondering how you can target dynamically generated variables in an external JS file directly from PHP. 我正在使用PDO和PHP连接到mysql数据库,但想知道如何直接从PHP中定位外部JS文件中动态生成的变量。

I have tried using AJAX but as far as i can understand it seems to only get data from PHP pages and not the opposite. 我试过使用AJAX,但据我了解,它似乎只能从PHP页面获取数据,而并非相反。

The JQuery code is like this... jQuery代码是这样的...

//Where i want to use indexes from a lookup table to populate the select element //我要使用查找表中的索引填充select元素的位置

$("#main").append('<select><option value=""></option></select>'); 

I tried AJAX in this way 我以这种方式尝试过AJAX

function getData(dataToPass)
{
    $.ajax({
        type: "POST",
        url: "getData.php",
        data: dataToPass,
        success: function (returnedData) 
        {
             $("#main").html(returnedData);
        }
    });    
}

But you cant pass variables this way can you? 但是您不能通过这种方式传递变量吗?

Im very new to PHP and JQuery and would appreciate the help. 我对PHP和JQuery非常陌生,希望能提供帮助。

First off, PHP has nothing to do with JavaScript. 首先,PHP与JavaScript无关。 They are totally different languages, in their own separate domain. 它们是完全不同的语言,位于各自独立的域中。 They co-work however if you want to. 但是,如果您愿意,他们会合作。 PHP can generate JavaScript code, which will then run on the client browser. PHP可以生成JavaScript代码,然后将其在客户端浏览器上运行。 The client browser though has no idea that the JavaScript code that is running was generated by PHP. 客户端浏览器虽然不知道正在运行的JavaScript代码是由PHP生成的。 Neither does PHP have any idea how to work with a browser. PHP也不知道如何使用浏览器。 PHP doesn't even have any idea about HTML. PHP甚至对HTML都不了解。

Thus, you can't pass to PHP a complex JavaScript object, the same way as you can't pass an object from PHP to JavaScript. 因此,您无法将复杂的JavaScript对象传递给PHP,就像无法将对象从PHP传递给JavaScript一样。 However you CAN pass values and data structures (arrays, data objects). 但是,您可以传递值和数据结构(数组,数据对象)。

In this case, you can either get the inner HTML code of the jQuery object in your JavaScript, by running 在这种情况下,您可以通过运行以下命令来获取JavaScript中jQuery对象的内部HTML代码:

dataToPass = {html: $("#main").html()}; //it will be a string

or by constructing an object that will have just the data you need, for example: 或通过构造仅包含所需数据的对象,例如:

dataToPass = {elements: [
    {node: "select", options: [{node: "option", value: ""}]},
    {node: "input", value: "someValue", type: "text"},
]};

As you have this JavaScript, it will send this data via the script you already have, to the server. 有了此JavaScript,它将通过您已有的脚本将此数据发送到服务器。 On the server side you will have to intercept the $_POST superglobal, and ask for either the HTML string, which will be $_POST['html'] if you chose the first example, or access an array of elements by $_POST['elements'] if you used the second example. 在服务器端,您将必须截获$_POST超全局变量,并询问HTML字符串(如果选择了第一个示例, $_POST['html'] ,或者通过$_POST['elements']访问元素数组$_POST['elements']如果您使用的是第二个示例。

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

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