开发者

Problem with Javascript Form to work consistently with Ajax to login to a Private Magento Store

开发者 https://www.devze.com 2023-03-15 00:45 出处:网络
My Webdeveloper abandoned me and my form doesn\'t seem to work always. When clicked it doesn\'t appear to do anything (just shows the \"javascript:;\" href on the browser status bar), but sometimes

My Webdeveloper abandoned me and my form doesn't seem to work always.

When clicked it doesn't appear to do anything (just shows the "javascript:;" href on the browser status bar), but sometimes it works..开发者_运维知识库.

I've searched everywhere for a solution but I haven't had any luck. Maybe I don't know where to start!! Really would appreciate help: I'm a novice and have been struggling. THANK YOU!!

<head>
   <link rel="stylesheet" type="text/css" href="css.css" />
      <script type="text/javascript" src="prototype.js" ></script>
      <script type="text/javascript">
          function login(){
             if($('username').value == '') alert('Username cannot be left blank');
             if($('password').value == '') alert('Password cannot be left blank');
      new Ajax.Request('http://www.website.com/index.php/customer/account/ajaxLogin',
      {
   method:'post',
      parameters: 'username='+$('username').value+'&password='+$('password').value,
         onSuccess: function(transport){
             if (transport && transport.responseText){
      try{
         response = eval('(' + transport.responseText + ')');
         }
     catch (e) {
    response = {};
     }
   }

   if (response.success){
                           location.href="http://www.website.com/index.php"
   }else{
       if ((typeof response.message) == 'string') {
    alert(response.message);
       } 
       return false;
   }
 },
   onFailure: function(){ alert('Something went wrong...') }
   });
 }
   </script>
      </head>

This is the HTML form

<form name="frm" method="post" action="#" id="frm">
    <ul>
        <li><input type="text" class="rounded" name="username" id="username" value="Username" /></li>
        <li><input type="password" class="rounded" name="password" id="password" value="Password" /></li>
        <li><a href="javascript:;" onClick="login();return false;"><img src="splash/button.png" alt="" /></a></li>
                   </ul>
</form>

Callup of prototype.js refers to prototype.js MIT Javascript framework, version 1.6.1


try changing this:

<a href="javascript:;" onClick="login();return false;">

into this:

<a href="javascript:login();return false;">

I hope it'll help

edit: It seems that your JS code has been written badly too. Lets consider lines:

     if($('username').value == '') alert('Username cannot be left blank');
     if($('password').value == '') alert('Password cannot be left blank');

Even when the login fields are left blank a request is going to be sent. Change to:

     if($('username').value == '') {
              alert('Username cannot be left blank');
              return false;
     }
     if($('password').value == '') {
              alert('Password cannot be left blank');
              return false;
     }

Other things should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号