English 中文(简体)
利用javascript、表格和php 张贴和提供问题
原标题:Post and submit problems using javascript, forms and php

我正在尝试创建一个用于验证电子邮件的JavaScript,并将其与表单和PHP一起使用。当然,会遇到一些问题...

  1. 你可以以我的形式看到,我确实把“职位”定义为方法。 但是,我只能把数据当作一种方法。 在我开始增加电子邮件核查稿和通过该文本之前,它一直在努力。

  2. 如果电子邮件地址不正确,我会返回false。但问题是请求test.php(在action中定义)不应该被执行,当前即使我返回false,页面也会被访问。

  3. 根据上述问题的答案,如果电子邮件已经验证OK,我需要通过Javascript提交表单吗?

JavaScript: (JavaScript: )

function isValidEmail() {
    regExp = /^[a-zA-Z]+([_.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)*(.[a-zA-Z]{2,4})+$/;
 if(document.subscribe.email1.value.search(regExp) == -1){
          alert(document.subscribe.email1.value + " Incorrect email address");
    return false; 
    } 

//document.subscribeForm.submit();

return true; 
}

PHP: PHP

<?php

echo $_POST[ email1 ];

$mysqli = mysqli_connect("localhost", "root", "", "test", "3306");

$result = mysqli_query($mysqli, "SELECT email, id, subscriber, subscribed_date FROM `user` u;");

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
foreach($row as $key => $value){

   echo "$key = $value<BR/>";
}
}

mysqli_free_result($result);
mysqli_close($mysqli);

?>

HTML: HTML (超文本标记语言)

<div id="subscribe">
  <form action="test.php" name="subscribe" method=post">
    <p id="formlabel">E-mail</p> <input type="text" name="email1">
    <br>
    <p id="formlabel">Repeat e-mail</p> <input type="text" name="email2"> <br/>
    <input type="submit" value="Subscribe" onclick="isValidEmail()">
  </form> 
最佳回答
<input type="submit" value="Subscribe" onclick="isValidEmail()">

该编码执行isValidEmail(),然后放弃结果。

您可以说:onclick=”return is ValidEmail(>。 然而:

  1. 把验证/提交代码放在

    中,而不是,以确保所有类型的表单提交都能调用它。

  2. 最好避免使用内联事件处理程序。

  3. 你的表单的 method 属性缺少一个 ",这可能是它默认返回 get 的原因。

所以

<form id="subscribe" method="post" action="test.php">
    ...
</form>

<script type="text/javascript>
    document.getElementById( subscribe ).onsubmit= function() {
        if (!this.elements.email1.value.match(/^[^@]+@[^@]+$/) {
            alert( Please enter an e-mail address );
            return false;
        }
        if (this.elements.email1.value!=this.elements.email2.value) {
            alert( E-mail addresses do not match );
            return false;
        }
        return true;
    } 
</script>

我用一个简单的正则表达式替换了原先的一个,因为你现在使用的表达式是错误的,会拒绝很多有效的电子邮件地址。因为客户的电子邮件地址不符合你心目中的电子邮件地址的概念而拒绝他们是有问题的。

使用正则表达式正确验证电子邮件地址非常困难。更好的做法是仅对明显格式错误的字符串进行简单检查,如上述示例。如果您确实需要检查电子邮件地址,您将必须尝试向其发送电子邮件,或者至少尝试查找地址的域名部分的MX记录。

请参阅此问题进行讨论。

问题回答

你应该将该函数附加到表单的onsubmit事件:

<form action="test.php" name="subscribe" method="post" onsubmit="isValidEmail()">

where you can stop the event returning false. Also, you made a typo in method=post", that s why it doesn t get submitted as POST data.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签