tags:

views:

83

answers:

3

passing a value to a php page via get, depending if the value is true then how do i fire my jquery function..

currently trying..

<script type="text/javascript" language="javascript">
 function showError(){ 
  $('#errormsg').fade(1000);
  $('#errormsg').html('<div class="error"><p>failed</p></div>');
 }
</script>

php on the page:

<?php if(isset($_GET['error'])) { ?>
   <script type="text/javascript" language="javascript">
     showError();
   </script>
<?php } ?>
A: 

Do you know why they put javascript in browsers? Check the error value in javascript before you send to the server. Do not check the value on the server then send back a page that calls a javascript function which displays an error message, this is an unnecessary trip to the server.

SquareRootOf2
It usually good manners to comment when you downvote. I didn't downvote this answer myself, but I imagine this was voted down because of how often validation logic can't be implemented w/o access to server resources (i.e. the database). Probably the argumentative tone as well.
Ken Browning
Error checking on both client and server side is a valid belt-and-suspenders technique. Besides, this might well be a case where javascript is not a sensible way to validate this (is this person's email already in my 200K+ member database for example).
Sean Vieira
I did not comment the down vote because I left an answer. And the error is obviously not an error that has to be validated on the server, the error variable is in the get request!
SquareRootOf2
for instance if the user clicked logout, then tried to visit a locked page he would be returned to say the login page with a fade in msg, or else if the session timed out then the msg would fire, or just for something totally different im just messing around just now trying various things..thanks for the responses though.
I do agree however that the data should be checked twice, on the server and the client, its the idea of having the error in the get request that confuses me.
SquareRootOf2
Putting the error in the GET seems valid to me (albeit a bit strange). User submits request, it fails, redirect user using a GET to a different page with the error parameter set to true so something gets displayed.
seth
+1  A: 

You probably need to wait until the dom is ready before you call showError(), e.g.

<?php if(isset($_GET['error'])) { ?>
    <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            showError();
        });
    </script>
<?php } ?>

I would probably take this approach though:

<script type="text/javascript">
    $(document).ready(function() {
        var showError = <?php echo (isset($_GET['error']) ? 'true' : 'false'); ?>;
        if (showError) {
            showError();
        }
    });
</script>
Tom Haigh
A: 

this works,

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $('#errormsg').hide();
        var showError = <?php echo (isset($_GET['accesscheck']) ? 'true' : 'false'); ?>;
        if (showError) {
            $('#errormsg').fadeIn(2000);
         $('#errormsg').html('Login Failed or Session Expired.');
         $('#errormsg').fadeOut(10000);
        }
    });
    </script>

just have to remember to put <div id="errormsg"></div> on the page.. thanks for the response..

no need for four separate $('#errormsg'). 1 will do, store it in a var and use that.
redsquare