单个提交按钮可以完成三个功能

时间:2015-08-22 15:25:00

标签: javascript php email

已经有一段时间,因为我发布了最后一次感谢,提前为您提供所有帮助..我有一个带有提交按钮的电子邮箱。

我想要检查此电子邮件地址以确保其不为空,如果要显示消息,然后要求用户输入我要验证的有效电子邮件地址,以便它只是hotmail和gmail帐户,例如xyz@hotmail.com和xyz@gmail.com,别无其他..

下面我的代码可以检查是否为空,并在屏幕上显示警告消息,但我不知道如何操作电子邮件地址检查,如果一切正常,如何使用相同的提交按钮提交有效的电子邮件谢谢你提交后的弹出消息..提前谢谢...唱歌

ps:提前为任何初学者的错误道歉...对不起

<?php
if(isset($_POST['email'])) {
	
$to      = 'xyz@hotmail.com';
$subject = '';
$email = $_POST['email_from'];
//$message = "LIST \r\n".
$message = "signoff list name \r\n";

   }

 $email_from = $_POST['email']; 

// create email headers
$message = wordwrap($message, 100, "\r\n");
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $message, $headers);
?>


<p>If you would like to receive our weekly newsletter email address below.</p>

<script type="text/javascript">
 function IsEmpty(){
  if(document.forms['isgtest'].email_from.value == "")
  {
    alert("Email field is empty, please enter email format");
    return false;
  }
  //return submit "email_from.value";
  (document.forms['test'].email_from.value == "subscribe")
  //return .email_from.value == "";
  //alert("thank u for joining the list !");
  //return true;
}
    </script>

<!--<script type="text/javascript"></script>-->
<form name="isgtest" class="rform" method="post" action="g.php"> 
<fieldset><legend>testing...</legend> 

<label for="email_from"><span style="color: #ff0000;"><strong>*</strong>
</span>Email address:<input id="email_from" type="text" name="email_from" size="25" />  <input id="insert" id="btn" onclick="return IsEmpty();" style="float: right;" type="button" name="submit" value="Subscribe" /></fieldset>
</form>

2 个答案:

答案 0 :(得分:1)

如果您要进行前端验证(仅应用于改善用户体验,从不信任用户输入并始终在服务器端进行验证!),为什么不使用为此目的而存在的HTML5功能。像这样:

<form>
    <label>Email:
        <input type='email' pattern=".+(@gmail.com|@hotmail.com)" required />
    </label>
    <button type="submit">subscribe</button>
</form>
  • type=email确保只接受电子邮件地址
  • required确保在提交之前提供值
  • pattern接受输入需要遵守的正则表达式才能提交。

就我个人而言,我并不是我的浏览器产生的默认错误消息的忠实粉丝,但我更不喜欢你使用的提醒,所以...

如果你坚持使用javascript方式,我会建议这样的事情(伪代码,未经测试):

function isEmpty(input) { ... }
function isEmail(input) { ... }
function isGmailOrHotmail(input) { ... }
function isValid(node) {
    var value = node.value;
    return ! isEmpty(value) && isEmail(value) && isGmailOrHotmail(value);
}

然后你可以将isValid函数绑定到提交按钮(最好是从你的脚本文件或块中绑定,但内联onclick方式也可以正常工作)

答案 1 :(得分:0)

<?php

$email_from = $_POST['email'];
$errors=array(); //track the errors as the script runs
function isValidEmail($addr) // Check for a valid email
{
return filter_var($addr, FILTER_VALIDATE_EMAIL) ? TRUE : FALSE;
}

if(!isValidEmail($email_from))$errors[]='Please enter a valid email address';

//Next, test for the email provider you wanted to filter by
$atPos=strpos($email_from,'@');//find the @ symbol
$afterAt=substr($email_from,$atPos,strlen($email_from)-$atPos); //get everything after
$dotPos=strpos($afterAt,'.');
$domain=strtolower(substr($afterAt,0,$dotPos)); //get the typed domain, lowercase
if($domain!='hotmail'||$domain!='gmail')$errors[]='Email must be hotmail or gmail';

if(isset($_POST['email'])) {

if(count($errors)<0)
{
$to      = 'xyz@hotmail.com';
$subject = '';
$email = $_POST['email_from'];
//$message = "LIST \r\n".
$message = "signoff list name \r\n";

// create email headers
$message = wordwrap($message, 100, "\r\n");
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $message, $headers);
}
}// note that I changed the nesting to only trigger the mail on post
?>
<p>If you would like to receive our weekly newsletter email address below.</p>

<script type="text/javascript">
function IsEmpty(){
if(document.forms['isgtest'].email_from.value == "")
{
alert("Email field is empty, please enter email format");
return false;
}
//return submit "email_from.value";
(document.forms['test'].email_from.value == "subscribe")
//return .email_from.value == "";
//alert("thank u for joining the list !");
//return true;
}
</script>

<!--<script type="text/javascript"></script>-->

<?php
if(count($errors)>0)
{
foreach $errors as $e //Now tell the user what went wrong
{
echo "$e<br>"; // you can also use '' to enclose js tags and use alert
}
}
?>

<form name="isgtest" class="rform" method="post" action="g.php"> 
<fieldset><legend>testing...</legend> 

<label for="email_from"><span style="color: #ff0000;"><strong>*</strong>
</span>Email address:<input id="email_from" type="text" name="email_from" size="25" /><input id="insert" id="btn" onclick="return IsEmpty();" style="float: right;" type="button" name="submit" value="Subscribe" />  </fieldset>
</form>