javascript - 我在 jquery ajax post 和 php 中遇到一些问题

标签 javascript php jquery ajax

我在使用 jQuery AJAX post 和 PHP 时遇到问题。

我尝试使用 .serialize() 发送数据并添加一些附加数据。

但是在 PHP 中,我收到了 .serialize() 数据,但没有收到其他数据。

在我的代码 jquery ajax 中,

         function load() {
            var form_data = $('#frm2').serialize();
            form_data.push({name:'year',value:'2016'});
            alert(form_data);
            $.ajax({
                type : "POST",
                url : "insert.php",
                cache : false,
                async : false,
                data : form_data,

                success : function(html) {
                    alert(form_data);
                }
            });
          }

而frm2(表单的id)是:

          <form name='frm2' action="./BOMinsert.php" id = 'frm2' method='post'>
            <table width="30%" id="keytable" style="border: 1px gray solid; table-layout: fixed">
                <thead style="background: #EAEAEA">
                    <th>year</th>
                    <th>brand</th>
                    <th>season</th>
                    <th>Styleno</th>
                </thead>
                <tbody style="text-align: center">
                    <?php 
                    for($i =1;$i<5;$i++)
                        echo "<td>".$_POST['name'.$i]."</td>";
                    ?>
                </tbody>
            </table>
            <BR>
            <BR>
            <table id="bomtable" style="width: 100%;table-layout: fixed">
                <thead style="background: #EAEAEA">
                    <th>item1</th>
                    <th>item2</th>
                    <th>item3</th>
                    <th>item4</th>                          
                    <th>item5</th>
                    <th>item6</th>
                    <th>item7</th>
                    <th>item8</th>
                    <th>item9</th>
                    <th>item10</th>
                    <th>item11</th>
                    <th>item12</th>
                </thead>
                <tbody style="text-align: center">
                    <?php echo "<td>".$_POST['name5']."</td>" ?>
                    <?php echo "<td>".$_POST['name6']."</td>"?>
                    <?php echo "<td>".$_POST['name7']."</td>"?>
                    <td><input type="text" class="ui-widget ui-corner-all" name='item' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='description' size="8" maxlength="30"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='supplycompany' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='colorway' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='unit' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='size' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='consume' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='unit' size="5" maxlength="10"/></td>
                    <td><input type="text" class="ui-widget ui-corner-all" name='etc'/></td>
                </tbody>
                <input type='submit' value='save' class="ui-button ui-widget ui-corner-all" style="background: #3498DB; color: white;float: right" onclick="load();">
          </form>

最后是我的 PHP 代码:

  if(isset($_POST['year'])){
    echo $_POST['year'];
  }else{
   var_dump($_POST);
  }

PHP 结果:

array(8) { ["item"]=> string(0) "" ["description"]=> string(0) "" ["supplycompany"]=> string(0) "" ["colorway"]=> string(0) "" ["unit"]=> string(0) "" ["size"]=> string(0) "" ["consume"]=> string(0) "" ["etc"]=> string(0) "" } 
Notice: Undefined index: year in C:\Apache24\htdocs\ptest\BOMinsert.php on line 19

附:我还尝试了 .serializeArray().push().serialize()+"&year=2016"

上面的代码已测试但不起作用。

最佳答案

首先,服务器端 php 代码如下..

<?php

if(isset($_POST['year'])){

    #Now the POST['year'] Request is available to be checked;
    #Go ahead and try it..

    $data=$_POST['year'];
    echo  $data;
    exit;
}
?>

使用 url 进行简单的跟踪

<script>
    function load() {
        var form_data = $('#frm2').serialize();
        var year='2016';
        //As you can see this will work perfectly and is already tested!
        //Just append the required values to the url ((in Url form like below)) and it will send everything to the same page


        $.ajax({
            type : "POST",
            url : "demo.php?year="+year,
            cache : false,
            async : true,//not false for the best user experience...
            data : form_data,

            success : function(html) {
                console.log(html)
            }
        });
    }

    //remember to bind an event on an element to call this function to work.
    //I hope it helps...

</script>

关于javascript - 我在 jquery ajax post 和 php 中遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878439/

相关文章:

javascript - 模板中不需要 $parent 绑定(bind)

javascript - 根据第一个下拉列表的值创建额外的 react 选择下拉列表

javascript - 单击事件命中框功能

php - 无法通过 php PDO 中的引用错误传递参数 2

php - MySQL SSL远程连接错误: Unable to get Private Key

php - 我需要更新连接两个不同表的表。此查询是否合适?

javascript - 使用 Jquery 使用 JSON 创建角色和权限表矩阵

php - fatal error : Call to a member function query() PHP CLASS

javascript - 如何将图像放在日间事件中? clndr.js

jquery css 高度,有什么问题?