Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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&;Javascript中的html_Javascript_Php_Html - Fatal编程技术网

php&;Javascript中的html

php&;Javascript中的html,javascript,php,html,Javascript,Php,Html,我想在JavaScript脚本中添加html标记和php代码 这是我的密码 <script> function myFunction() { var table = document.getElementById("myTable"); var row = table.insertRow(2); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var cell3 = r

我想在JavaScript脚本中添加html标记和php代码

这是我的密码

<script>
function myFunction()
{
    var table = document.getElementById("myTable");
    var row = table.insertRow(2);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(1);
    var cell4 = row.insertCell(1);
    cell1.innerHTML = "NEW CELL1";
    cell2.innerHTML = "NEW CELL2";

function myDeleteFunction()
{
    document.getElementById("myTable").deleteRow(2);
}
</script>

函数myFunction()
{
var table=document.getElementById(“myTable”);
var行=table.insertRow(2);
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
var cell3=行插入单元格(1);
var cell4=行插入单元格(1);
cell1.innerHTML=“新建cell1”;
cell2.innerHTML=“新建cell2”;
函数myDeleteFunction()
{
document.getElementById(“myTable”).deleteRow(2);
}
我想在cell1中添加select标记,并且我的select标记具有来自数据库的选项值 这是select标记的代码::

<td>
    <select id="nlocation" name="nlocation" >
    <?php 
        $res = mysql_query("SELECT * FROM `nlocation` WHERE `nlocation` != '$location' ");
        if(mysql_num_rows($res)> 0)
        {
            for($i=0;$i<mysql_num_rows($res);$i++) {
                $row=mysql_fetch_assoc($res);       
                echo"<option value='$row[nlocation]'> $row[nlocation] </option>";       
            }
        }           
    ?>     
    </select>
</td>

轻松!更换

echo"<option value='$row[nlocation]'> $row[nlocation] </option>";
在javascript中:

cell1.innerHTML = "<?php echo $arr_data[0]['nlocation']; ?>";
cell2.innerHTML = "<?php echo $arr_data[1]['nlocation']; ?>";
cell1.innerHTML=“”;
cell2.innerHTML=“”;
有两种方法:

使用点添加字符串:“foo”。$bar.“foo”

echo“$row['nlocation']”;
或者(我更喜欢)使用backets foo{$bar}foo

echo"<option value='{$row['nlocation']}'> {$row['nlocation']} </option>";
echo“{$row['nlocation']}”;

注意:我更喜欢第二个选项,因为我们使用“.所以PHP无论如何都会在字符串中查找变量。如果您想要更快的脚本,请确保对每个文本使用'foo'

最好将PHP脚本创建的HTML内容分配给JavaScript变量

<script type="text/javascript">
    var selectContent = "<select id=\"nlocation\" name=\"nlocation\">" +
        "<?php 
            $res=mysql_query("SELECT * FROM `nlocation` WHERE `nlocation` != '$location' ");
            if(mysql_num_rows($res)> 0)
            {
                for($i=0;$i<mysql_num_rows($res);$i++) {
                    $row=mysql_fetch_assoc($res);

                    echo "<option value='".$row['nlocation']."'> ".$row['nlocation']." </option>";
                }
            }
        ?>" +
        "</select>";
</script>

myFunction()
缺少右大括号(
}
)。在nlocation列中有什么类型的数据?如果有特殊字符,请小心,然后必须使用htmlspecialchars()对字符串进行转义:htmlspecialchars($row['nlocation'));为什么解决了这个问题?我的问题是在JavaScript1中的cell1中添加这个selec标记,因为$row[nlocation]不是引用nlocation索引,而是引用$nlocation变量。@user3315393如果select标记应添加到页面加载中,则将其保存到PHP变量中,并将该PHP变量传递给JavaScript(它们应位于同一页面上),而不是
echo
,如果select标记是动态的,则使用@ICanHasCheezburger实际上,PHP会将
$row[nlocation]
转换为
$row['nlocation']
(尽管强烈建议()这样做)。实际的问题是,除非字符串包含在双引号中并且数组用
{}
转义,否则PHP不会对数组执行字符串替换。请相信这不是我的问题!!!我的问题是如何在代码cell2.innerHTML=“NEW cell2”中插入select标记"; 我想用select标签替换“new cell2”仅此问题请确认这不是我的问题!!!我的问题是如何在javascript函数中插入select标记!!!!!!!!在代码cell2.innerHTML=“NEW cell2”中;我想用select标记替换“new cell2”仅用于解决此问题
echo"<option value='" . $row['nlocation'] . "'> " . $row['nlocation'] . " </option>";
echo"<option value='{$row['nlocation']}'> {$row['nlocation']} </option>";
<script type="text/javascript">
    var selectContent = "<select id=\"nlocation\" name=\"nlocation\">" +
        "<?php 
            $res=mysql_query("SELECT * FROM `nlocation` WHERE `nlocation` != '$location' ");
            if(mysql_num_rows($res)> 0)
            {
                for($i=0;$i<mysql_num_rows($res);$i++) {
                    $row=mysql_fetch_assoc($res);

                    echo "<option value='".$row['nlocation']."'> ".$row['nlocation']." </option>";
                }
            }
        ?>" +
        "</select>";
</script>
<script>
function myFunction()
{
    var table = document.getElementById("myTable");
    var row = table.insertRow(2);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(1);
    var cell4 = row.insertCell(1);
    cell1.innerHTML = "NEW CELL1";
    // INSERTING SELECT CODE HERE!!
    cell2.innerHTML = selectContent;

function myDeleteFunction()
{
    document.getElementById("myTable").deleteRow(2);
}
</script>