0

My website is trackschoolbus.com. You can see a login form at the top right. What I have set up is when a wrong input is given it redirects to home page with a parameter as ?er=1 i.e. http://www.trackschoolbus.com/?er=1. I need to display a error message when the error url comes so I have written

<script type="text/javascript"> 
   $(function(){ 
           if (document.location.href.indexOf('er=1') > 0) 
$("#display").show(); 
   }); 
</script>

and the html is

<div id="display" style="display:none;">wrong input</div>

my login form is

<form name="login-form" id="login-form" method="post" action="http://www.trackschoolbus.com/vehicleTracking/index.php">
    <input name="LoginForm[username]" id="LoginForm_username" type="text" placeholder="Registered Email" value="" class="error" required/>
    <input maxlength="30" name="LoginForm[password]" id="LoginForm_password" type="password" placeholder="Password" value="" class="error" required />
    <input type="submit" onclick="this.disabled=true;this.form.submit();" name="yt0" class="btn-submit" value="Login" />
</form> 

still it shows display none.

11
  • Instead of Javascript you can use php condition Apr 14, 2015 at 9:35
  • how can i add that ?
    – Melvin
    Apr 14, 2015 at 9:36
  • Is the $("#display").show(); inside if{ } or the code formatting is so?
    – Shaunak D
    Apr 14, 2015 at 9:36
  • 1
    <input type="submit" onclick="this.disabled=true;this.form.submit();" is highly questionable. NEVER submit in a submit event and in some cases the submit event is stopped when the button is disabled. Instead hide it or replace it in the FORM's submit event
    – mplungjan
    Apr 14, 2015 at 9:42
  • 1
    $("login-form").on("submit",function() { $("input[name='yt0']").hide();});
    – mplungjan
    Apr 14, 2015 at 9:49

3 Answers 3

4

use php

    <form name="login-form" id="login-form" method="post" action="http://www.trackschoolbus.com/vehicleTracking/index.php">
            <input name="LoginForm[username]" id="LoginForm_username" type="text" placeholder="Registered Email" value="" class="error" required/>
            <input maxlength="30" name="LoginForm[password]" id="LoginForm_password" type="password" placeholder="Password" value="" class="error" required />
            <input type="submit" onclick="this.disabled=true;this.form.submit();" name="yt0" class="btn-submit" value="Login" />
            <?php if (isset($_GET['er']) && $_GET['er'] == 1) {
                echo '<div id="display">wrong input</div>';
            }?>
            </form> 
0
1

You can use this code

if ($_REQUEST['er']==1)
{
   echo '<script type="text/javascript"> 
      $("#display").show(); 
   </script>';
}
3
  • It may work, but it's less elegant than just generating the div with PHP (Madalin's solution). Your solution injects a script that shows an existing div, that's two steps instead of one. Apr 14, 2015 at 9:41
  • @JeremyThille I think his answer will always make display:none of div Apr 14, 2015 at 9:42
  • All the more reason :) Apr 14, 2015 at 9:46
1

This is relatively simple in javascript.

Using the code snippet in this thread: How can I get query string values in JavaScript?

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if (getParameterByName("er") == "1") 
    $("#display").show(); 
}); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.