2

My situation

I have a piece of javascript that does several manipulations on a webform on form.php. The form is set to action="form.php" and thus loads itself upon submission. The webform is only built if (!isset($_POST['submit'])), so after posting, the input elements and containers that make up the webform are not there. This is generally how the code is structured:

<?php if (!isset($_POST['submit'])) { 
//the form has not been submitted
?>
  <form action="<?php $_SERVER['PHP_SELF']?>" method="post" id="form" name="form">
    <!-- general input elements -->
    <input type="submit" name="submit" value="Send">
  </form>
<?php }
else {
// the form has been submitted
  // posted values are submitted to a database
}
?>

The javascript still tries to do manipulations on these elements, though. Since these aren't there anymore, errors occur. I could use a bunch of if (HTMLElement) statements for every manipulation to a certain HTMLElement, but I hope there is a more solid, more general solution.

The question

Given that the form submits to its page and that it's not an option to simply keep showing the form after POSTing, do you have a suggestion how I can detect the POSTing of the webform and shut certain parts of my javascript down?

A more general question would be:

How can a php POST be detected by javascript?

bakkerjoeri
  • 225
  • 1
  • 11
  • Have you really SUBMITTED the form? or just reloaded the form through javascript? code please! – Kalpesh Aug 03 '12 at 22:21
  • 1
    Could you not perform some PHP on the submission page, save the results to a session (or database etc), then reload the form page back to its original state? If not why don't you use the same PHP IF condition on the javascript? – Phil Cross Aug 03 '12 at 22:22
  • 1
    possible duplicate of [Client-side detection of HTTP request method](http://stackoverflow.com/questions/121218/client-side-detection-of-http-request-method), Q: "Is it possible to detect the HTTP request method (e.g. GET or POST) of a page from JavaScript? If so, how?". A: **"In a word - No"** – Rob W Aug 03 '12 at 22:22
  • Thank you for your input. Your comments have contributed to my understanding of the problem in a valuable way. – bakkerjoeri Aug 03 '12 at 23:36

1 Answers1

2

One thing that comes to mind is something like this:

<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') $isPost = true; ?>

// later on in your script

<?php if (!$isPost): ?>
<script type="text/javascript">
// the JS code that manipulates your form
</script>
<?php endif; ?>

Basically, if the form was posted, don't render the JS code that manipulates it. If that code is mixed with other code you always want included, then you'll have to work it a little differently. In that case you could try:

<script type="text/javascript">
var isPost = <?php echo $isPost ? 'true' : 'false' ?>;

if (!isPost) {
    document.getElementById('blah').doSomething();
}
</script>
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
drew010
  • 68,777
  • 11
  • 134
  • 162
  • I find your first suggestion a very good and elegant solution. All the javascript that manipulates the forms is essentially in one .js document, so I can simply call the file into action only `if (!isset($_POST['submit']))`. Thank you for your answer. This was exactly what I was looking for. – bakkerjoeri Aug 03 '12 at 23:35