2

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>'; 
?>

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?

2
  • possible duplicate of Reference: Why does the PHP (or other server side) code in my Javascript not work? Commented Nov 19, 2013 at 11:24
  • PHP runs on the serverside where there is no javascript engine, the javascript has to be echo'ed to the browser just like HTML, as that is where javascript is executed. As a sidenote, this is generally a bad idea, you should have your javascript in it's own file instead. Commented Nov 19, 2013 at 11:24

3 Answers 3

3

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>";
Sign up to request clarification or add additional context in comments.

Comments

2
<?php
   echo "<script> your javascript code   </script>";


?>


php will work from server javascript will work from client side

Comments

1

You cannot mix javascript code (which is executed on client side) and php (which is server oriented).

First, your function GetCellValues is javascript, not php, so replace your firsts <?php and ?> by <script> and </script>.

Then, you need a form and a button type=submit, or an ajax call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.