如何在php中编写javascript代码 [英] how to write javascript code within php

查看:74
本文介绍了如何在php中编写javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击提交"按钮后,我得到了一张表格:

I have got a form, on clicking the submit button:

<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代码,它可以正常工作.此代码中的问题是什么,对此有何解决方法?

But not able to submit the form, if I call the javascript code on onClick, it works.what is the problem in this code, Is there any work around for this?

推荐答案

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

You have to echo the whole JScript function because PHP doesn't know what you're doing:

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中编写javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆