-1

Below is a form which would appear no matter javascript is enabled or disabled.

<?php
if (logged_in()) {
echo "
    <form method='post' name='form' action='' class='oldform'>
        <textarea  name='content' id='content' ></textarea>
        <input type='submit' value='post' name='submit'/>
    </form>";
  }
  ?>

Now I want to replace the above form with another form when javascript is enabled complying with the logged_in php code. I think code below will hide old form and display new form but I am not able to get how to put script in echo so when javascript is enabled <script> replaces old form with new.

<script>
    <style>
     .oldform{display:none;}
     </style>

    <form name="newform">
    </form>
<script>
EJW
  • 338
  • 3
  • 6
  • 18
bɪˈɡɪnə
  • 1,087
  • 2
  • 23
  • 46

1 Answers1

1

To improve load time, and prevent errors with css in old browsers, you can (though it requires a second page visit) use javascript to set a cookie.

If the cookie exists server-side (they have javascript) render form 1, else form 2.

page html

<script type="text/javascript">
  document.cookie = 'hasJS=true';
</script>

page php

if (isset($_COOKIE['hasJS'])){
  // Form 1
}else{
  // Form 2
}
Manikiran
  • 2,618
  • 1
  • 23
  • 39