使用AJAX将变量传递给PHP数组

时间:2020-12-20 00:02:07

I have looked around various other questions relating to this topic but I am still struggling to find a solution to my query.

我查看了与此主题相关的各种其他问题,但我仍在努力寻找我的查询的解决方案。

In my system I am allowing the admin to append extra form nodes to the file so they may input any extra required data, this data is then submitted to the DB to generate pages in the main portion of the site.

在我的系统中,我允许管理员将额外的表单节点附加到文件中,以便他们可以输入任何额外的所需数据,然后将这些数据提交给DB以在站点的主要部分中生成页面。

I am new to the concept of passing the data back via AJAX so I may be misunderstanding something but here is my code.

我不熟悉通过AJAX传回数据的概念所以我可能误解了一些东西,但这里是我的代码。

Add Node Control

添加节点控制

<div id="nodeControl">
    <div id="addNode">
        <a id="nodeLink" href="#">+</a>
    </div>
</div>

<div id="slideOut">Click to add a new section</div> 

Add Node click response function

添加节点单击响应功能

var nodeCount = 2;
        $("#nodeLink").click(function(){
            // Local Variables
            var newSection = '<br/><div id="sectionInfo"><div id="sectionWrap"><div id="sectionInner"><label class="inst head">Section ' + nodeCount + '</label><input class="textOver" type="text" name="sectionTitle' + nodeCount + '" value="<?php $title; ?>"> <label class="inst ib">Please enter any text you would like associated with the section.</label> <textarea style="margin-top: 3px" name="sectionContent' + nodeCount + '" value="<?php $body; ?>"></textarea> <label class="inst ib">Please upload the image associated with this section, .PNG reccomended.</label> <input type="file" style="visibility:hidden;" name="img' + nodeCount + '" id="upload" /> <input type="button" id="fileStyle" class="fSOver" value="Upload Pump Image!" /> </div> </div> </div>' ;

            // Append the new section to the document and increment the node count
            $('#sections').append(newSection);
            nodeCount += 1;


            // Call AJAX to pass to PHP
            $.ajax({
                type: "POST",
                url:  "../section.php",
                data: { setSection : newSection },
                success: function() {
                    alert("Success, ajax parsed");
                }
            });

            $(".successDiv").slideDown(150);
            $(".successDiv").delay(1500).slideUp(150);

            return false;
        }); 

Node count is instantiated to "2" here, when a node is added the number is appended to the name attribute of any input elements on my form i.e. header2, header3 etc. This is so when my form is eventually submitted I can loop through each data field and submit it to the database.

节点计数在这里被实例化为“2”,当添加一个节点时,数字被附加到我表单上任何输入元素的name属性,即header2,header3等。这样,当我的表单最终提交时,我可以遍历每个数据字段并将其提交到数据库。

As I currently understand the AJAX data variable uses key pairs { a : b } where a = return key and b = return value, I want PHP to be able to access that returned value and store it into an array which can be looped through at the end. Currently the success message is triggered on my AJAX call but I am unable to access the values in PHP, instead I am thrown an "Undefined index of sections defined".

正如我目前所理解的那样,AJAX数据变量使用密钥对{a:b},其中a =返回键和b =返回值,我希望PHP能够访问该返回值并将其存储到一个可以循环的数组中。结束。目前成功消息是在我的AJAX调用上触发的,但是我无法访问PHP中的值,而是抛出了“未定义的段定义索引”。

$section = array();
$setSection = $_POST['setSection'];
$section[] = $setSection; 

1 个解决方案

#1


5  

PHP won't store every setSection variable you pass it over time; each time your script is called, its variables are reset. You probably want to store the variable in a database or session variable.

PHP不会存储随时间传递的每个setSection变量;每次调用脚本时,都会重置其变量。您可能希望将变量存储在数据库或会话变量中。

Here's one approach, using session variables:

这是一种使用会话变量的方法:

session_start(); // unless you already started one
if (!isset($_SESSION['section'])) {
    $_SESSION['section'] = array();
}
array_push($_SESSION['section'], $_POST['setSection']);

#1


5  

PHP won't store every setSection variable you pass it over time; each time your script is called, its variables are reset. You probably want to store the variable in a database or session variable.

PHP不会存储随时间传递的每个setSection变量;每次调用脚本时,都会重置其变量。您可能希望将变量存储在数据库或会话变量中。

Here's one approach, using session variables:

这是一种使用会话变量的方法:

session_start(); // unless you already started one
if (!isset($_SESSION['section'])) {
    $_SESSION['section'] = array();
}
array_push($_SESSION['section'], $_POST['setSection']);