dongshan8194 2017-02-06 14:39
浏览 49
已采纳

无法通过javascript向php提交表单

I have a form in html which I want to run verification in Javascript first before POST ing to PHP. However the link up to the PHP section does not seem to be working despite the fact that I have assigned names to each input tag and specified an action attribute in the form tag.

Here is the HTML code for the form:

<form id="signupform" action="signupform.php" method="post">
  <input type="text" name="Email" placeholder="Email Address" class="signupinput" id="email" />
  <br />
  <input type="password" name="Password" placeholder="Password" class="signupinput" id="passwordone" />
  <br />
  <input type="password" placeholder="Repeat Password" class="signupinput" id="passwordtwo" />
  <br />
  <input type="button" value="Sign Up" class="signupinput" onClick="verifypass()" id="submit" />
</form>

The button calls the javascript function which I use to verify the values of my form before sending to php:

function verifypass() {
  var form = document.getElementById("signupform");
  var email = document.getElementById("email").value;
  var password1 = document.getElementById("passwordone").value;
  var password2 = document.getElementById("passwordtwo").value;
  var emailcode = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

      if (emailcode.test(email)) {
        if (password1.length > 6) {
          if (password1 == password2) {
            form.submit(); //this statement does not execute
          } else {
            $("#passwordone").notify("Passwords do not match!", {
              position: "right"
            })
          }
        } else {
          $("#passwordone").notify("Password is too short!", {
            position: "right"
          })
        }
      } else {
        $("#email").notify("The email address you have entered is invalid.", {
          position: "right"
        })
      }
    }
  • 写回答

3条回答 默认 最新

  • dsgawmla208057 2017-02-06 17:22
    关注

    For some reason, some JavaScript implementations mix up HTML element IDs and code. If you use a different ID for your submit button it will work (id="somethingelse" instead of id="submit"):

    <input type="button" value="Sign Up" class="signupinput" onClick="verifypass()" id="somethingelse" />
    

    (I think id="submit" has the effect that the submit method is overwritten on the form node, using the button node. I never figured out why, perhaps to allow shortcuts like form.buttonid.value etc. I just avoid using possible method names as IDs.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。