douhuangzhi0707 2014-12-30 20:15
浏览 31
已采纳

php在javascript函数中循环

I have some code that allows me to use javascript to add rows to a result table. I basically setup the first row in a table, and then have an add row button that will duplicate the row. this works fine accept when I try to add a dropdown field that uses php to pull results. The row still adds when I click the button, but the php dropdown portion doesn't showup.

    function addOpenResult() {
  var openResults = document.getElementById("openResults");
  var openResultsRow = openResults.insertRow(openResults.rows.length-1);
  var col1html = "<td width='50'><input size='5' width='100' type='text' name='rsRD1' /></td>";
  var col2html = "<td width='50'><input size='5' width='100' type='text' name='rsRD2' /></td>";
  var col3html = "<td width='50'><input size='5' width='100' type='text' name='rsRD3' /></td>";
  var col4html = "<td width='50'><input size='5' width='100' type='text' name='rsRD4' /></td>";
  var col5html = "<td width='100'><input size='10' width='100' type='text' name='rsScore' /></td>";
  var col6html = "<td width='200'><select name='playerID'><option selected>---- Select Player ---</option>" . <?php while ($trpRow=mysql_fetch_array($trpResult)) {extract($trpRow); echo '<option value=$playerID>$lastName, $firstName</option>' ;}?> . "</select></td>";
  var col7html = "<td width='100'><input size='10' type='text' name='rsAmount' /></td>";
  var col8html = "<td width='100'><input size='10' type='text' name='rsOtherWinnings' /></td>";
  var col9html = "<td width='100'><input size='10' type='text' name='rsOtherValue' /><input type='hidden' name='catID' value='15'></td>";


  var col1 = openResultsRow.insertCell(0); col1.innerHTML=col1html;
  var col2 = openResultsRow.insertCell(1); col2.innerHTML=col2html;
  var col3 = openResultsRow.insertCell(2); col3.innerHTML=col3html;
  var col4 = openResultsRow.insertCell(3); col4.innerHTML=col4html;
  var col5 = openResultsRow.insertCell(4); col5.innerHTML=col5html;
  var col6 = openResultsRow.insertCell(5); col6.innerHTML=col6html;
  var col7 = openResultsRow.insertCell(6); col7.innerHTML=col7html;
  var col8 = openResultsRow.insertCell(7); col8.innerHTML=col8html;
  var col9 = openResultsRow.insertCell(8); col9.innerHTML=col9html;
  var col10 = openResultsRow.insertCell(9); col10.innerHTML=col10html;
}
  • 写回答

3条回答 默认 最新

  • douxing6434 2014-12-30 20:24
    关注

    You're incorrectly concatenating your JavaScript string with PHP content. Change

    var col6html = "<td width='200'><select name='playerID'><option selected>---- Select Player ---</option>" . <?php while ($trpRow=mysql_fetch_array($trpResult)) {extract($trpRow); echo '<option value=$playerID>$lastName, $firstName</option>' ;}?> . "</select></td>";
    

    To

    <?php
    $options = "";
    while ($trpRow=mysql_fetch_array($trpResult)) {
        extract($trpRow);
        $options .=  "<option value=\"$playerID\">$lastName, $firstName</option>" ;
    }
    ?>
    var col6html = "<td width='200'><select name='playerID'>"+ 
                      "<option selected>---- Select Player ---</option>" +
                      <?php echo json_encode($options) ?>  +
                   "</select></td>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制