如何将带有Ajax的jQuery生成的输入框传递给PHP以插入MySQL?

时间:2022-12-05 11:48:59

Even-though I have tried the workarounds proposed on similar solutions, it still gives me 'Array' on the receiving PHP.

尽管我已经尝试过在类似解决方案上提出的解决方法,但它仍然在接收PHP上给我“数组”。

One thing I have to mention that is different from other postings that I've seen regarding this very topic, is that in my case, I am using a and jQuery to populate input text boxes for location and systems.

我必须提到的一点是,与我在这个主题上看到的其他帖子不同,就是在我的情况下,我使用a和jQuery来填充位置和系统的输入文本框。

The way it should work is that the returned data should be inserted into a mySQL db. Each input text box has a unique name and id, but I have been struggling with just passing the information from jQuery to PHP.

它应该工作的方式是返回的数据应该插入到mySQL数据库中。每个输入文本框都有一个唯一的名称和ID,但我一直在努力将信息从jQuery传递给PHP。

So, I have some input boxes generated by click, and the idea is to pass them back after the form is validated. These locations and systems don't need to be validated, that is why the url of the ajax function is different from the action on the original form that holds it up and is unknown the amount of locations nor systems that the user is going to input in.

因此,我有一些通过点击生成的输入框,并且想法是在验证表单后将它们传回。这些位置和系统不需要进行验证,这就是为什么ajax函数的url与保持它的原始表单上的操作不同的原因,并且未知用户要输入的位置或系统的数量在。

I have tried to insert an image to show but the system said that I need more reputation to do that. So any questions please feel free to ask

我试图插入一个图像来显示,但系统说我需要更多的声誉来做到这一点。所以任何问题请随时问

This function should take all the input texts with class 'locations'

此函数应采用类“位置”的所有输入文本

            function updatenewval()
            {
            $("input.locations").each(function(){
            locations[$(this).attr("id")] = $(this).val();
                    $.ajax({
                        type: 'POST',
                        //dataType: 'text/html',
                    url: 'index.php?pag=project&op=list',
                    data: locations
                    });
            });
            }

This function should take all the input texts with class 'systems'

这个函数应该使用类'系统'的所有输入文本

                function updatenewvalsys()
                {
                $("input.systems").each(function(){
                systems[$(this).attr("id")] = $(this).val();
                    $.ajax({
                            type: 'POST',
                            //dataType: 'text/html',
                            url: 'index.php?pag=project&op=list',
                            data: systems
                        });

                });
                }

On the PHP file there is a <div id="content">Add a Location</div> calling add_locations with parameters 0,0 first location, first system

在PHP文件上有一个

添加位置 调用add_locations参数0,0第一个位置,第一个系统

            function add_location(num,sys)
            {
            num++;
            sys++;
            $('#content').append("<div id=location"+num+"></div>");
            $('#location'+num).append("<br>Location:&nbsp;<input type='text' name=location"+num+" onchange=\"updatenewval()\" onfocus=\"clearMe(this);\" class='locations' value=Location&nbsp;"+num+"  id=location"+num+" size=31 maxlength=31><br><br><div id='system1' class=\"add\"><a href='javascript:add_system("+num+","+sysorg+");' >Add a System&nbsp;<img src=\"images/plus1.gif\" style=\"border:0px\" alt=\"Add a System\"></a></div>");
            $('#inslocation').remove();
            $('#content').before("<div id='inslocation' class=\"add\"><a href=\"javascript:add_location("+num+","+sysorg+");\" class=\"add\">Add a Location&nbsp;<img src=\"images/plus1.gif\" style=\"border:0px\" alt=\"Add a Location\"></a></div>");        
            }

The function to bring call systems, I know the code is not elegant at all but I care right now for functionality mainly as I have been struggling to pass the data correctly.

带来呼叫系统的功能,我知道代码并不优雅,但我现在关心功能主要是因为我一直在努力正确地传递数据。

            function add_system(num,sys)
            {
            newsystem=sys+1;
            $('#location'+num).append("<br>System:&nbsp;<input type='text' name=system"+num+newsystem+" class='systems' id=system"+num+newsystem+" value=System&nbsp;"+newsystem+"&nbsp;@&nbsp;Location"+num+" onchange=\"updatenewvalsys()\"  onfocus=\"clearMe(this);\" size=32 maxlength=32><div id='insystem' class=\"add\"><a href='javascript:add_system("+num+","+newsystem+");' >Add a System&nbsp;<img src=\"images/plus1.gif\" style=\"border:0px\" alt=\"Add a System\"></a></div>");
            $('#system1').remove();
            $('#insystem').remove();
            $('#insystem'+num+sys).hide();
            $('#location'+num).append("<div id=insystem"+num+newsystem+" class=\"add\"><a href='javascript:add_system("+num+","+newsystem+");' >Add a System&nbsp;<img src=\"images/plus1.gif\" style=\"border:0px\" alt=\"Add a System\"></a></div>");
            }

I was trying the approach mentioned above, and it does not seem to be passing any value. On the PHP, op=list

我正在尝试上面提到的方法,它似乎没有传递任何价值。在PHP上,op = list

    if (isset($_POST['locations']))
    {
    parse_str($_POST['locations'], $locations);

                foreach ($locations as $key => $value) {
                echo "$value<br>\n";
                }
        }

Any pointers would be greatly appreciated and as this is related to jQuery -> PHP` serialize object. Thanks in advance!

任何指针都将非常感激,因为这与jQuery - > PHP`序列化对象有关。提前致谢!

1 个解决方案

#1


1  

I think when you do your post you want to do something along the lines of:

我想当你做你的帖子时,你想要做的事情是:

function updatenewval() {

    var locations = $("input.locations").serializeArray();

     $.ajax({
        type: 'POST',
        url: 'index.php?pag=project&op=list',
        data: locations
     });
}

which would serialize all input textboxes and their values and pass them in the http parameter data. At that point your PHP code should deserialize the array... to process it.

它将序列化所有输入文本框及其值,并将它们传递给http参数数据。此时,您的PHP代码应该反序列化数组...来处理它。

#1


1  

I think when you do your post you want to do something along the lines of:

我想当你做你的帖子时,你想要做的事情是:

function updatenewval() {

    var locations = $("input.locations").serializeArray();

     $.ajax({
        type: 'POST',
        url: 'index.php?pag=project&op=list',
        data: locations
     });
}

which would serialize all input textboxes and their values and pass them in the http parameter data. At that point your PHP code should deserialize the array... to process it.

它将序列化所有输入文本框及其值,并将它们传递给http参数数据。此时,您的PHP代码应该反序列化数组...来处理它。