如何在php中编写javascript代码

how to write javascript code within php

本文关键字:javascript 代码 php      更新时间:2023-09-26

我有一个表单,点击提交按钮:

<form>
   <h2 style="text-align: center">* * * PROJECTS * * *</h2>
<br>
   <input type="button" name="submit" onclick="document.write('<?php GetCellValues() ?>');" Value="SAVE">
<br>
</form>
<?php
    function GetCellValues()
    {
        echo("Save");
        var str = '';
        var rows = document.getElementsByTagName('tr');
        var table=document.getElementById("project");
        for (var i=0;i<table.rows[0].cells.length;i++)
        {
            str = str + (table.rows[0].cells[i].innerHTML) + ', ' ;     
        }
        for (var c = 1 ; c < rows.length ; c++)
        {
            str += ''n' + "0" + c + ', ';
            var row = rows[c];
            var inputs = row.getElementsByTagName('input');                
            for (var k = 0 ; k < inputs.length ; k++)
            {
                str += inputs[k].value + ', ';
            }
        }
    }
?>
<?php
$handle = fopen("data.csv", "w");
$hide = $_REQUEST['hide'];
fwrite($handle,$hide);
$file = file('data.csv');
$lines = count($file);
echo'<table id = "projest" border = "1" cellpadding="2" cellspacing="0" style = "width: 60%; margin-left: auto; margin-right: auto; border-color: brown; background-color:gray;">';
echo'<tr cellpadding="100">
    <th width="15%"><h3>Sl.No.</h3></th>
    <th width="15%"><h3>Project Name</h3></th>
    <th width="15%"><h3>ChangeDate</h3></th>
    <th width="15%"><h3>Changed By</h3></th>
</tr>';
for ($i=1; $i<$lines; $i++) 
{
  $part = explode(',', $file[$i]);
  echo'<tr>
    <td align= "center" width="5%">'.$part[0].'</td>
    <td align= "center" width="25%"><input type="text" value='.$part[1].'></td>
    <td align= "center" width="25%"><input type="text" value='.$part[2].'></td>
    <td align= "center" width="25%"><input type="text" value='.$part[3].'></td> 
  </tr>';
}
echo'</table>'; 
?>

但由于无法提交表单,如果我在onClick上调用javascript代码,它就起作用了。这段代码有什么问题,有什么解决办法吗?

您必须回显整个JScript函数,因为PHP不知道您在做什么:

echo "<script type = 'text/javascript'>function GetCellValues()
    {
        //echo('Save'); use document.write()?
        var str = '';
        var rows = document.getElementsByTagName('tr');
        var table=document.getElementById("project");
        for (var i=0;i<table.rows[0].cells.length;i++)
        {
            str = str + (table.rows[0].cells[i].innerHTML) + ', ' ;     
        }
        for (var c = 1 ; c < rows.length ; c++)
        {
            str += ''n' + "0" + c + ', ';
            var row = rows[c];
            var inputs = row.getElementsByTagName('input');                
            for (var k = 0 ; k < inputs.length ; k++)
            {
                str += inputs[k].value + ', ';
            }
        }
    }</script>";
<?php
   echo "<script> your javascript code   </script>";

?>

php will work from server javascript will work from client side

您不能将javascript代码(在客户端执行)和php(面向服务器)混合使用。

首先,函数GetCellValues是javascript,而不是php,所以用<script></script>替换第一个<?php?>

然后,您需要一个表单和一个按钮type=submit,或者一个ajax调用。