dongqian3750
2019-01-23 04:21
浏览 88
已采纳

验证电子邮件并将其发送给流明控制器

I'm trying to validated email with lumen controller. If email is entered correctly, it should pop out "valid" but at the current state its always "invalid" even with correct emails entered.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

 class NewsletterController extends Controller
 {
  public function subscribe(Request $request)
 {
    $email = $request->query('email');
    echo $email;


    function valid_email($email) {

        return !!filter_var($email, FILTER_VALIDATE_EMAIL);


    }

    if( valid_email($email) ) {

        return response()->json(["valid"]);



    } else {

        return response()->json(["invalid"]);

    }

}
}

AJAX Call via jQuery

 function msg() {
var email = document.getElementById("email").value;
console.log(email);
var email2 = $('input[type=email]').val();
console.log(email2);
event.preventDefault();

$.get("newsletter/subscribe?email + $('input[type=email]').val()", function(data, status){
    console.log("data: " + data + "
Status: " + status);
})
.done(function() {
    //console.log( "done" );
    alert("DONE");
})
.fail(function() {
    console.log( "error" );
    alert("Fail");
})
.always(function() {
    //console.log( "finished" );
    alert("hz");
});            

};

Form

 <form class="join-newsletter">
    <input id="email" name="email" type="email" placeholder="Enter your email address" class="email-address">
    <input onclick="msg()" type="submit" value="Subscribe" class="submit-email button">
 </form>

Updated code: still showing data: invalid.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duan5362 2019-01-23 05:20
    已采纳

    I suppose your ajax request could be changed to.

    $.get("/newsletter/subscribe?email="+ $('#email').val()
    

    The possible problem is here you don't send email value correctly to your controller.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题