Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
 
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. –  adeneo Nov 19 '13 at 11:24
add comment

3 Answers

up vote 0 down vote accepted

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>";
share|improve this answer
add comment
<?php
   echo "<script> your javascript code   </script>";


?>


php will work from server javascript will work from client side
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.