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?

4
  • Have you really SUBMITTED the form? or just reloaded the form through javascript? code please! Commented Aug 3, 2012 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? Commented Aug 3, 2012 at 22:22
  • 1
    possible duplicate of 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" Commented Aug 3, 2012 at 22:22
  • Thank you for your input. Your comments have contributed to my understanding of the problem in a valuable way. Commented Aug 3, 2012 at 23:36

1 Answer 1

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

1 Comment

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.

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.