简体   繁体   English

AJAX将低谷Jquery发布到PHP

[英]AJAX post trough Jquery to PHP

Hi im trying to post some values with jquery to use them in PHP, problem howwever is that I cant get it to work. 嗨,我尝试用jquery发布一些值以在PHP中使用它们,但问题是我无法使其正常工作。

Here's my code: 这是我的代码:

 <script type="text/javascript">
        $(document).ready(function() {
            var max_fields      = 100; //maximum input boxes allowed
            var wrapper         = $(".input_fields_wrap"); //Fields wrapper
            var add_button      = $(".add_field_button"); //Add button ID
            var buttonpressed = $(".Totalform"); //Add button ID

            var x = 1; //initlal text box count

            //first one needs the code to so here.
            var arrayFromPHP = <?php echo json_encode($a); ?>;
            $("#country" + x).select2({
                data: arrayFromPHP
            });

            $(add_button).click(function(e){ //on add input button click
                e.preventDefault();

                if(x < max_fields){ //max input box allowed
                    x++; //text box increment

                    $(wrapper).append('' +
                        '<div>' +
                        '<select id="country'+ x +'" style="width:300px;" value="Selecteer uw artikel">' +
                        '</select>' +
                        '<a href="#" class="remove_field">Remove</a>' +
                        '</div>' +
                        '<div><input type="number" name="quantity'+ x +'" min="1" max="10"><a href="#" class="remove_field">Remove</a></div>'
                    );
                    //same code as above becouse need to apply this to the list again.
                    $("#country" + x).select2({
                        data: arrayFromPHP
                    });
                }

            });

            $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
                e.preventDefault(); $(this).parent('div').remove(); x--;
            });
        });

            // THIS PART IS POST
            $(buttonpressed).click(function(e){ //on add input button click
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "submitsubform.php",
                    data: {teller: x},
                    success: function () {
                    }
                });
            });

    </script>

I put a comment in the code at my Post part. 我在帖子部分的代码中添加了注释。 also I have this code in HTML: 我也有HTML中的这段代码:

<form method="post">
        Bedreifsnaam:<br>
        <input type="text" id="Bedrijfsnaam" name="Bedrijfsnaam" value="Autobanden.nl" disabled><br>
        Email:<br>
        <input type="text" name="lastname" value="voorbeeld@mail.com" disabled><br>
        product:<br>
        <div class="input_fields_wrap">
            <!-- This is the button I count X on. -->
            <button name="counterknop" value="Add item" class="add_field_button">Add product</button>
            <div>
                <select id="country1" style="width:300px;">
                    <!-- Dropdown List Option -->
                </select>
            </div>
            <div><input type="number" name="quantity1" min="1" max="10"></div>
        </div>
        <br><br>
        <input type="submit" id="Totalform" name="Totalform" value="Submit">
    </form>

So theoretically I want On click of button to do x++ and this works. 因此,从理论上讲,我希望单击按钮即可执行x++并且可以正常工作。 then send it trough POST to my PHP file and put the value in an PHP variable there: 然后通过POST将其发送到我的PHP文件,并将该值放在那里的PHP变量中:

// This is in the index.php file like the Script and HTML
if(isset($_POST['Totalform']))
{
    include 'submitsubform.php';       
    echo 'test';
}

// This is in submitsubform.php
<?php
$var = $_POST['teller'];
echo $var;

So at this point I tried a lot of debugging but cant seem to find the problem the only error I get back from all this code is this: 因此,在这一点上,我尝试了很多调试,但是似乎无法找到问题,我从所有这些代码中得到的唯一错误是:

Notice: Undefined index: teller in C:\\xampp\\htdocs\\OIA\\submitsubform.php on line 3 注意:未定义的索引:第3行的C:\\ xampp \\ htdocs \\ OIA \\ submitsubform.php中的柜员

So when I try to click the button multiple times to increment x, and after that submit the form. 因此,当我尝试多次单击按钮以递增x并在之后提交表单时。 It doesnt do anything. 它什么也没做。 It just gives the above error. 它只是给出了以上错误。

After like 2 or 3 hours I thought I would post this here maybe someone sees something I dont see. 大约2或3个小时后,我以为我会在这里发布此消息,也许有人看到了我看不到的东西。

thanks in advance and happy coding! 在此先感谢您,并祝您编程愉快!

ADDITION 1 : 附加1:

When I do a var dump of the posted form, it doesnt see the dropdown menu or the 2 normal text fields. 当我对发布的表单进行var dump时,它没有看到下拉菜单或2个普通文本字段。 It only sees the "aantal" (the number fields) 它只看到“ aantal”(数字字段)

array(3) { ["quantity1"]=> string(1) "3" ["quantity2"]=> string(1) "1" ["Totalform"]=> string(6) "Submit" } array(3){[“ quantity1”] => string(1)“ 3” [“ quantity2”] => string(1)“ 1” [“ Totalform”] => string(6)“ Submit”}

EDIT: 编辑:

was a brainfart from my end. 从我的头开始是个脑袋。 i disabled the 2 text fields. 我禁用了2个文本字段。 however, it still doesnt see the select box as an field so it doest post that with it 但是,它仍然不会将选择框视为字段,因此它不会将其发布

Your ajax success function is empty 您的ajax成功功能为空

success: function () {}

so you don't return anything from the ajaxed page you need to change your php and js to return the result 因此,您无需从ajaxed页面返回任何内容,您需要更改您的php和js以返回结果

try alerting the result of the ajax 尝试提醒ajax结果

success: function (data) {
 alert(data);
}

if the page is redirecting after the click on the submit button then you aren't doing ajax 如果单击提交按钮后页面正在重定向,那么您不是在做ajax

This happens simply because you run two requests. 发生这种情况仅仅是因为您运行了两个请求。 One ajax with the post, and another post which is the html submit action. 一个带有该帖子的ajax,另一个带有html提交操作的帖子。 So the html submit action never gets the teller variable. 因此,html提交操作永远不会获得出纳变量。 You can just insert the teller variable as a hidden input and do not do ajax request at all. 您只可以将出纳员变量作为隐藏输入插入,而根本不执行ajax请求。

I think you can't get the x variable. 我认为您无法获取x变量。 It is not a global variable. 它不是全局变量。

the X variable still on $(document).ready(function(){}) tag X变量仍位于$(document).ready(function(){})标记上

try to remove that tag or put your $(buttonpressed).click(function(e){}); 尝试删除该标签或将您的$(buttonpressed).click(function(e){}); tag inside $(document).ready(function(){}) $(document).ready(function(){}) 内的标签

hope that it is help 希望有帮助

jQuery.ajax({ type: "POST", jQuery.ajax({type:“ POST”,

 data: { "teller" : x},
 success: function () {
 }

}); });

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

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