Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中通过嵌入mysql下拉菜单复制html表单javascript_Javascript_Php_Html_Mysql_Drop Down Menu - Fatal编程技术网

在php中通过嵌入mysql下拉菜单复制html表单javascript

在php中通过嵌入mysql下拉菜单复制html表单javascript,javascript,php,html,mysql,drop-down-menu,Javascript,Php,Html,Mysql,Drop Down Menu,经过大量研究,我学会了如何使用javascript复制HTML表单的一部分,在我尝试将php添加到javascript中之前,它工作得很好 本页面的目的是让活动组织者提交每位员工通过销售获得的收入 我的问题是,我希望“员工姓名”在每个副本上显示为下拉菜单,该字段将从员工表中检索 这是我的代码: var记录=1; 函数add_fields(){ 记录++; var objTo=document.getElementById('staff\u sales') var divtest=docume

经过大量研究,我学会了如何使用javascript复制HTML表单的一部分,在我尝试将php添加到javascript中之前,它工作得很好

本页面的目的是让活动组织者提交每位员工通过销售获得的收入

我的问题是,我希望“员工姓名”在每个副本上显示为下拉菜单,该字段将从员工表中检索

这是我的代码:


var记录=1;
函数add_fields(){
记录++;
var objTo=document.getElementById('staff\u sales')
var divtest=document.createElement(“div”);
divtest.innerHTML='Record'+Record+':名称:
销售:';
对象追加子对象(divtest)
}

----------
出售:


在Javascript中连接字符串时,换行符很重要。这里有一个小演示:

<script>
//single line, works
var str_test = '<div class="label">Record ' + ' here comes a ' + '<?php echo "string from PHP" ?>' + '</div>';
console.log('str_test = ', str_test);

//multiple with + at end of line, works
str_test = '<div class="label">Record ' + ' here comes a ' + 
                '<?php echo "string from PHP" ?>' + '</div>';
console.log('str_test = ', str_test);

//does not work - string truncated as <div class="label">Record  here comes a 
str_test = '<div class="label">Record ' + ' here comes a ' 
                '<?php echo "string from PHP" ?>' + '</div>';
console.log('str_test = ', str_test);
</script>

有错误吗?你得到了什么?别无选择?没有值/名称?查询运行正常吗?这里需要更多信息……如果没有嵌入javascript中的php,它工作得很好(当然没有下拉菜单),当表单不重复时。是的,你已经说过了。但是我很确定你的控制台中有一些错误。或者一个php错误。做一些调试。。。。在添加到dom之前,您是否记录了divtest?内容可以吗?我打赌错误就在那里的某个地方。缺少/错误的引号f.e.,我强烈建议首先构建html/字符串,然后将其应用于divtest.innerHTML。更容易调试。你做了两次同样的数据库查询…不太好
   <?php

    //first build a staff array
    $squery=mysqli_query($link, "SELECT * FROM staff") ;
    $staff = array();

    while ($srow = mysqli_fetch_array($squery)) {

        //build an array, indexed by id
        $id = $srow["staff_id"];
        $staff[$id] = $srow; 

        //build the html right into the array
        $staff[$id]['html'] = '<option value="$id">'.$srow["staff_name"].'</option>'; 
     }
    ?>
var html_str = '<div class="label">Record ' + record + ':</div>'+
    '<div class="content">'+
     '<span>Name: <select name="stock_type_id[' + record + ']">' +
     '<?php foreach($staff as $a){ echo $a["html"]; } ?>'+
     '</select>'+
     '<input type="text" style="width:48px;" name="staff_id[' + record + ']" value="" /></span>'+
     '<span> Sales: <input type="number" style="width:48px;" name="staff_sales[' + record + ']" value="" />' + 
     '</span></div>';

divtest.innerHTML = html_str;
//etc.... as before
script type = "text/javascript" >