43

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

  1. I want to do some task in the same file (db task) AND
  2. I want the form data to be sent to test.php with the redirection

here is my code

    <?php
    if(isset($_POST['btn'])){
        //do some task
    ?>
    <script type="text/javascript">
        var e = document.getElementById('testForm'); e.action='test.php'; e.submit();</script>
    <?php
    }
    ?>
<form name="testForm" id="testForm"  method="POST"  >
    <input type="submit" name="btn" value="submit" autofocus  onclick="return true;"/>
 </form>

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

bitoshi.n
  • 2,278
  • 1
  • 16
  • 16
n92
  • 7,424
  • 27
  • 93
  • 129
  • 2
    Pretty logical, your javascript runs at the moment it is outputted in the browser and your form is printed ... after that. You should enclose your code in a window.onload event listener so that it is only executed after the page load completion. – yent May 15 '12 at 07:42

5 Answers5

80

Just echo the javascript out inside the if function

 <form name="testForm" id="testForm"  method="POST"  >
     <input type="submit" name="btn" value="submit" autofocus  onclick="return true;"/>
 </form>
 <?php
    if(isset($_POST['btn'])){
        echo "
            <script type=\"text/javascript\">
            var e = document.getElementById('testForm'); e.action='test.php'; e.submit();
            </script>
        ";
     }
  ?>
keto23
  • 1,177
  • 1
  • 10
  • 16
  • 3
    Please note the the solution here is _not_ generating the JavaScript code with PHP but the fact that the PHP block comes after the form tags which means the DOM element 'testForm' is initialized when the JavaScript is executed. – chiborg May 16 '12 at 09:50
10

Lately I've come across yet another way of putting JS code inside PHP code. It involves Heredoc PHP syntax. I hope it'll be helpful for someone.

<?php
$script = <<< JS

$(function() {
   // js code goes here
});

JS;
?>

After closing the heredoc construction the $script variable contains your JS code that can be used like this:

<script><?= $script ?></script>

The profit of using this way is that modern IDEs recognize JS code inside Heredoc and highlight it correctly unlike using strings. And you're still able to use PHP variables inside of JS code.

6

You can put up all your JS like this, so it doesn't execute before your HTML is ready

$(document).ready(function() {
   // some code here
 });

Remember this is jQuery so include it in the head section. Also see Why you should use jQuery and not onload

nischayn22
  • 447
  • 3
  • 14
5

At the time the script is executed, the button does not exist because the DOM is not fully loaded. The easiest solution would be to put the script block after the form.

Another solution would be to capture the window.onload event or use the jQuery library (overkill if you only have this one JavaScript).

chiborg
  • 26,978
  • 14
  • 97
  • 115
0

You can use PHP echo and then put your JavaScript code:

<?php
      echo "
            <script>
              alert('Hellow World');
            </script>
        "; 
?>
James Risner
  • 5,451
  • 11
  • 25
  • 47