dongli5785 2017-11-06 19:48
浏览 55
已采纳

当我不知道要抓取的所有变量时如何处理php帖子

I have a form that the user can add additional lines (using javaScript) but when the save button is clicked, how to I grab the new data to assign to a variable?

What's happening now: The user adds new rows, clicks save and the rows disappear from the page along with the data.

Here is my html:

    <table id="desc_table" class="form">
      <tbody id="desc_tbody">
        <tr id="headings">
          <td><font><h3>Description</h3></font></td>
          <td><font><h3>Hours</h3></font></td>
          <td><font><h3>Rate</h3></font></td>
          <td><font><h3>Amount</h3></font></td>
          <td></td>                          
        </tr>

        <tr>
          <td><input name="desc1" type="text" value="<?php echo $desc1; ?>"></td> 
          <td> <input name="desc_hr1" type="text" value="<?php echo $desc_hr1; ?>"></td>
          <td> <input name="desc_rt1" type="text" value="<?php echo $desc_rt1; ?>"></td>
          <td><input name="desc_amt1" type="text" value="<?php echo $desc_amt1; ?>"></td>

          <td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
        </tr>
        <tr>
          <td><input name="desc2" type="text" value="<?php echo $desc2; ?>"></td> 
          <td> <input name="desc_hr2" type="text" value="<?php echo $desc_hr2; ?>"></td>
          <td> <input name="desc_rt2" type="text" value="<?php echo $desc_rt2; ?>"></td>
          <td><input name="desc_amt2" type="text" value="<?php echo $desc_amt2; ?>"></td>
          <td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
        </tr>
        <tr>
          <td><input name="desc3" type="text" value="<?php echo $desc3; ?>"></td>
          <td> <input name="desc_hr3" type="text" value="<?php echo $desc_hr3; ?>"></td>
          <td> <input name="desc_rt3" type="text" value="<?php echo $desc_rt3; ?>"></td>
          <td><input name="desc_amt3" type="text" value="<?php echo $desc_amt3; ?>"></td>
          <td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
        </tr>                         
        </tbody>
   </table>

    <div>
        <center><input type="button" value="Add New Line" id="add_btn" name="add_btn" onclick="newLine()"></center>
    </div>  

JavaScript:

function newLine(){

//get the total amount of rows in the table
var $rowcount = $('#desc_table tr').length;

//add the row number to the field name to make it unique
var $desc = "desc" + $rowcount;
var $desc_hr = "desc_hr" + $rowcount;
var $desc_rt = "desc_rt" + $rowcount;
var $desc_amt = "desc_amt" + $rowcount;

//create new row data
var $newRowData = '<tr><td><input name=' + $desc + ' type=text></td><td><input name=' + $desc_hr + ' type=text></td><td><input name=' + $desc_rt + ' type=text></td><td><input name=' +$desc_amt+ ' type=text></td><td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td></tr>';

//append new row to bottom of the table
$($newRowData).appendTo($("#desc_table tbody"));

}

PHP variable declaration:

...

if (!empty($_POST['desc1'])){
    $desc1 = $_POST['desc1'];
}
else{
    $desc1 = NULL;
}
if (!empty($_POST['desc_hr1'])){
    $desc_hr1 = $_POST['desc_hr1'];
}
else{
    $desc_hr1 = NULL;
}
if (!empty($_POST['desc_rt1'])){
    $desc_rt1 = $_POST['desc_rt1'];
}
else{
    $desc_rt1 = NULL;
}
if (!empty($_POST['desc_amt1'])){
    $desc_amt1 = $_POST['desc_amt1'];
}
else{
    $desc_amt1 = NULL;
}
if (!empty($_POST['desc2'])){
    $desc2 = $_POST['desc2'];
}
else{
    $desc2 = NULL;
}
if (!empty($_POST['desc_hr2'])){
    $desc_hr2 = $_POST['desc_hr2'];
}

...

When the page variables are captured with the POST, how do I find the new row variables to declare them and capture the data?

  • 写回答

2条回答 默认 最新

  • doudi8525 2017-11-06 20:10
    关注

    Use array-style names rather than incrementing the suffix on each row:

       <tr>
          <td><input name="desc[]" type="text" value="<?php echo $desc; ?>"></td> 
          <td> <input name="desc_hr[]" type="text" value="<?php echo $desc_hr; ?>"></td>
          <td> <input name="desc_rt[]" type="text" value="<?php echo $desc_rt; ?>"></td>
          <td><input name="desc_amt[]" type="text" value="<?php echo $desc_amt; ?>"></td>
    
          <td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
        </tr>
    

    PHP will then put all the inputs into arrays, and you can loop through them.

    foreach ($_POST['desc'] AS $i => $desc) {
        $desc_hr = $_POST['desc_hr'][$i];
        $desc_rt = $_POST['desc_rt'][$i];
        $desc_amt = $_POST['desc_amt'][$i];
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP