【问题标题】:POST submit to mysql using php the value from append javascriptPOST 使用 php 将附加 javascript 中的值提交到 mysql
【发布时间】:2019-03-13 17:20:38
【问题描述】:

我正在尝试使用附加 javascript 中的 php 将数据 POST 提交到 mysql。

这是 php 中数据来自 append 的模式。

                            <tr onload="calculate()">

                                <?php

                                foreach ($conn->query("SELECT * FROM panapricelist") as $info){

                                    echo "<td><input type='checkbox' id='promotitle' name='check' value='".$info['ProductId']."' ></td>";
                                    echo "<td><textarea rows='4' cols='7' maxlength='60'  name='pcode' class='pcode' id='ProductCode' disabled>".$info['ProductCode']."</textarea></td>";
                                    echo "<td><br><textarea rows='5' cols='40' maxlength='50' name='puse' class='productuse' id='productuse' disabled>".$info['ProductUse']." </textarea></td>";
                                    echo "<td><br><textarea rows='4' cols='50' maxlength='50' name='pdesc' class='description' id='productDesc' disabled>".$info['ProductDesc']."</textarea></td>";
                                    echo "<td id='msrp'><textarea rows='4' cols='10' maxlength='50' name='Msrp' class='msrp' id='productMsrp' disabled>".$info['Msrp']."</textarea></td>";
                                    echo "<td style='width: 10%;'><textarea rows='4' cols='10' name='cost' maxlength='50' class='cost' id='cost' disabled>".$info['DealerPhp']."</textarea></td></tr>";
                                }
                                ?>

                            </tbody>

这是从模态到表格的附加 js。

        $(document).ready(function() {
            $("#button_add").click(function() {
                var favorite = [];
                $.each($("input[name='check']:checked").each( function() {
                    // favorite.push($(this).val());

                    var getRow = $(this).parents('tr'); //variable for the entire row
                    var value =  (getRow.find('td:eq(1)').html()); // Product Code
                    var value1 = (getRow.find('td:eq(2)').html()); // for Suggested Product Use
                    var value2 = (getRow.find('td:eq(3)').html()); // for product Description
                    var value3 = (getRow.find('td:eq(4)').html()); // for MSRP PHP
                    var value4 = (getRow.find('td:eq(5)').html()); // for Dealer PHP
                    var value5 = (getRow.find('td:eq(0)').html()); // for Dealer PHP

                    $('#item-row').append('<tr><td class="item-name"><textarea value="'+ value +'</textarea></td><td class="item-name"><textarea class="check" name="check[]" value= "' + value1 + ' </textarea> </td><td class="item-name"><textarea value= "' + value2 +' </textarea></td><td class="item-name"><textarea value= "' + value3 + ' </textarea>  </td><td><textarea name="Msrp" value="' + value4 + '</textarea></td><td class="item-name"><textarea class="qty" id="qty" name="qty[]"> </textarea></td><td class="item-name"><textarea id="price" class="price" name="price[]" disabled></textarea></td></tr>');    
                }));


            });


        });

问题是。我如何使用 PHP PDO POST 提交到 mysql 值来自附加 javascript??

【问题讨论】:

    标签: javascript php mysql append


    【解决方案1】:

    您需要将任何输入数据放入表单中,然后创建一个 PHP 脚本来处理 POST 请求并将数据插入到您的 MySQL 数据库中。

    您的表单标签应如下所示:

    &lt;form class="post-form" action="script_that_handles_post.php" method="post"&gt;

    因此,您需要使用$_POST['']。例如,第一个表格行中的namename="check",您将使用$_POST['name'] 来获取该字段的值。对要存储的每个字段执行此操作。最好将它们中的每一个存储在它们自己的变量或数组中。

    然后,对于您的 AJAX,您必须以某种方式包含该库。如果您还没有,请将其放在 jquery 源代码下方的页脚中:

    &lt;script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"&gt;&lt;/script&gt;

    现在您可以添加您的 AJAX POST 请求。下面是一些应该这样做的代码:

    $(".post-form").submit(function (event) {
        var $form = $(this);
        var serializedData = $form.serialize();
        request = $.ajax({
            url:$form.attr('action'),
            type:'POST',
            data: serializedData
        });
        request.fail(function (jqXHR, errorThrown){
            console.error(
                "Error: " + errorThrown
            );
        });
        request.always(function () {
            $inputs.prop("disabled", false);
        });
        event.preventDefault();
    });
    

    这段代码所做的是获取表单的action 路径并从那里的字段输入中发布数据。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 2011-08-02
      • 2016-10-06
      相关资源
      最近更新 更多