我想将表单数据插入数据库。现在,我在做什么时,当我单击注册按钮时,它首先检查email
是否已经存在,如果是,则显示一条现有消息,否则将表单数据插入数据库,并向用户发送确认邮件。那么,我该怎么做?请帮助我。
控制器:FirstController.php
public function register(Request $request)
{
$user_id = date('YmdHis');
$fname = $request->input('fname_sign');
$lname = $request->input('lname_sign');
$email = $request->input('email_sign');
$pass = $request->input('pass_sign');
$candidate_type = $request->input('candidate_type');
$s_date = date('Y-m-d');
$confirm_id = md5($user_id);
$sql = DB::select('select * from user where email="'.$email.'"');
if(count($sql) > 0)
{
echo "<p>Email id already exist. Please sign up with different email id</p>";
}
else
{
$data = array('user_id'=>$user_id,'fname'=>$fname, 'lname'=>$lname, 'email'=>$email, 'password'=>$pass, 'candidate_type'=>$candidate_type, 's_date'=>$s_date,'confirm_id'=>$confirm_id);
DB::table('candidate')->insert($data);
echo "<p>save successfully</p>";
}
}
视图:
<script>
$(document).ready(function(){
$("#signup").click(function(e){
e.preventDefault();
fname_sign = $("#fname_sign").val();
lname_sign = $("#lname_sign").val();
email_sign = $("#email_sign").val();
pass_sign = $("#pass_sign").val();
candidate_type = $("#candidate_type").val();
$.ajax({
type:"POST",
data:{"fname_sign":fname_sign, "lname_sign":lname_sign, "email_sign":email_sign, "pass_sign":pass_sign, "candidate_type":candidate_type, "_token":"{{csrf_token()}}"},
url:"{{URL::to('register')}}",
success:function(data){
$("#success_in").html(data);
}
});
});
});
</script>