如何使用PHP将Jquery Add Rows数据提交到mysql [英] How to submit Jquery Add Rows data to mysql using PHP

查看:89
本文介绍了如何使用PHP将Jquery Add Rows数据提交到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经修改了这段代码. jQuery从其他地方进行了修改.我希望能够在此页面上添加产品(当前可以使用)...但困扰的部分是我随后通过提交按钮将所有这些数据提交给了MySql.

So I have this piece of code I've been tinkering with. The Jquery is modified from somewhere else. I'm wanting to be able to add products on this page (which currently works)... but the part im struggling with is I then submitted ALL of that data to MySql through a submit button.

我已经尝试过一些数组/循环,但是到目前为止,我已经画了一个空白.有人可以帮忙吗?

I've tried playing around with a few arrays / loops but so far I have drawn a blank. Can anyone help?

<html>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type='text/javascript'>
        //<![CDATA[
            $(document).ready(function() {
                var currentItem = 1;
                $('#addnew').click(function() {
                    currentItem++;
                    $('#items').val(currentItem);
                    var strToAdd = '<br>product: <select name="product'+currentItem+'" id="product'+currentItem+'" ><option value="aclt">Tobacco</option><option value="aed">Energy Drink</option></select> nicotine: <select name="nicotine'+currentItem+'" id="nicotine'+currentItem+'">      <option value="3">3mg</option><option value="6">6mg</option><option value="12">12mg</option></select> Qty:<input name="qty'+currentItem+'" id="qty'+currentItem+'" type="text" />';
                    $('#data').append(strToAdd);
               });
           });
       //]]>
    </script>

    <form method="POST" action="invoicereg.php" id="data" name="sub">
    product:
        <select name="'product1'" id="product1" >
            <option value="aclt">Tobacco</option>
            <option value="aed">Energy Drink</option>
        </select>
        nicotine:
        <select name="nicotine1" id="nicotine1">
            <option value="3">3mg</option>
            <option value="6">6mg</option>
            <option value="12">12mg</option>
        </select>
        Qty:<input type="text" id="qty" name="qty"></input><br>
    </form>
    <button type="submit" form="data">SUBMIT</button>
    <input type="button" id="addnew" name="addnew" value="Add new item" /> 
    <input type="hidden" id="items" name="items" value="1" />
</html>

推荐答案

我注意到您在代码中犯了一些错误.

I've noticed few mistakes that you've made in your code.

第一个错误是:

<select name="'product1'" id="product1">

name属性的值中删除单引号,它应类似于:

Remove single quotes from the value of name attribute and it should look like:

<select name="product1" id="product1">

这不视为问题.如果您在name属性的值周围使用单引号,那么在PHP中访问其值将很烦人.

This is not considered an issue. If you were using single quotes around the value of name attribute, it would be quite annoying to access its value in PHP.

因此,您将需要使用复杂的表达式来获取值,例如以下语法:

So, you would need to use complex expression to fetch the value like the following syntaxes:

$_POST['\'product1\'']

$_POST["'product1'"]

第一个使用反斜杠的转义序列来转义单引号.

The first one uses escape sequence of backward slash to escape single quotes.

第二个使用双引号来访问包含单引号的键.这比第一个涉及有点复杂的表达式的人要容易得多.

The second one uses double quotes to access keys with single quotes wrapped around. This one is relatively easier than the first one involving a bit complex expression.

第二次错误是:

form标记下的最后三个HTML元素未包装在表单内.最后三个元素中的两个元素submit和隐藏元素属于表单元素.修改代码后,整个代码应如下所示:

The last three HTML elements below the form tag are not wrapped inside the form. The two among the last three elements which are submit and hidden element belong to form element. After modifying the code, the whole code should be like the following:

<form method="POST" action="invoicereg.php" id="data" name="sub">
    product:
    <select name="product1" id="product1" >
        <option value="aclt">Tobacco</option>
        <option value="aed">Energy Drink</option>
    </select>
    nicotine:
    <select name="nicotine1" id="nicotine1">
        <option value="3">3mg</option>
        <option value="6">6mg</option>
        <option value="12">12mg</option>
    </select>
    Qty:<input type="text" id="qty" name="qty"></input><br>
    <button type="submit" form="data">SUBMIT</button>
    <input type="button" id="addnew" name="addnew" value="Add new item" /> 
    <input type="hidden" id="items" name="items" value="1" />
</form>

希望有帮助!

这篇关于如何使用PHP将Jquery Add Rows数据提交到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆