-3

The PHP script doesn't seem to call dis(); function..Here it is:

PHP:

if (!$_SESSION['user']) {
    echo"<script type='text/javascript'>dis();</script>";
 }

JS:

  <script type="text/javascript">
   function dis() {
    $(document).ready(function() {
        $("#main_text_area").attr("disabled", "disabled");
    });
    }

When I place just $("#main_text_area").attr("disabled", "disabled"); it disables correctly...but I need to do it on a function call...Thanks for comments.

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
Rod Rig Gez
  • 133
  • 2
  • 11
  • "The PHP script doesn't seem to call dis(); function" --- sure, because PHP knows nothing about JS – zerkms Sep 28 '11 at 23:47
  • PHP doesn't call javascript; your browser does. PHP simply outputs the data to be used by whatever client (in this case a browser) requested the data. – Decent Dabbler Sep 28 '11 at 23:49
  • 3
    See: http://stackoverflow.com/questions/7016701/creating-jquery-ajax-requests-to-a-php-function/7016795#7016795 – NullUserException Sep 28 '11 at 23:50
  • 2
    You should check produced code. Check if "dis()" is declared before called. Some browsers also have built-in js debuggers. (Try pressing F12 and look into errors console). – NiematojakTomasz Sep 28 '11 at 23:52
  • Ok...I see..PHP is server-side based , JS client...but it is possible to call PHP from JS ( the other way round..) – Rod Rig Gez Sep 28 '11 at 23:52
  • @MrX "*call PHP from JS*" - That's AJAX – Mike B Sep 28 '11 at 23:54
  • @Mr X: "but it is possible to call PHP from JS" --- no. It is possible to call JS from JS. – zerkms Sep 28 '11 at 23:54
  • @Tomasz HA..It works ! Exactly as you suggested JS should've been declared before PHP function call...so it is possible. Thanks ! – Rod Rig Gez Sep 28 '11 at 23:55
  • 2
    @MrX There's a very important distinction you're missing here. PHP isn't doing any interacting with javascript whatsoever. PHP is outputting javascript code that interacts with other javascript code. – Mike B Sep 28 '11 at 23:57
  • Crazy idea but why not just use ` disabled="disabled">` or similar? – Phil Sep 28 '11 at 23:57
  • @Herbert ...Ajax or as Tomasz wrote - by declaring JS before PHP. – Rod Rig Gez Sep 28 '11 at 23:59
  • @Phil Could do this as well...true. Thanks. – Rod Rig Gez Sep 29 '11 at 00:02
  • 2
    @Mr X - you can't call PHP code from JS. If you use AJAX you are making a request to the webserver but your JS doesn't know or care whether that request is handled by PHP, ASP, JSP, or whatever. All it knows is that it gets some response back. At no point is your JS directly calling PHP code, and at no point does your PHP call JS code. – nnnnnn Sep 29 '11 at 00:18
  • @nnnnnn: That's an important distinction. Well said. – Herbert Sep 29 '11 at 00:22

1 Answers1

0

I'd recommend, instead of disabling the textarea, outputting the content of the <textarea> as static text.

Maybe something like this:

<?php if( !$_SESSION['user'] ): ?>
    <div class="text">
       <?php echo $textareaContents; ?>
    </div>
<?php else: ?>
    <textarea id="main_text_area">
       <?php echo $textareaContents; ?>
    </textarea>
<?php endif; ?>

The Javascript approach you're currently taking is trivially easy to get around.

Mark Biek
  • 146,731
  • 54
  • 156
  • 201