[英]Uploading data and file in one form to ajax in Laravel
I'm using Bootstrap Modal for my registration page. 我在注册页面上使用了Bootstrap Modal。 The user should be able to input his image as well as information on this form.
用户应该能够在该表格上输入他的图像以及信息。
HTML 的HTML
<form method="POST">
<center>
<img name="user_image" id="proPic" src="images/Profile_ICON.png">
</center>
<center>
<label for="inputImg" id="upload_link"> Upload Image </label>
<input type="file" id="inputImg" accept="image/*">
</center>
<input type="text" class="registrationForm" name="username" id="username" placeholder="Username">
<center>
<span id="user_name_error" class="error"></span>
</center>
<input type="text" class="registrationForm" name="email" id="email" placeholder="Email ID">
<center>
<span id="user_email_error" class="error"></span>
</center>
<input type="text" class="registrationForm" name="user_mobile_number" id="user_mobile_number" placeholder="Mobile Number">
<center>
<span id="user_mobile_error" class="error"></span>
</center>
<input type="text" class="registrationForm" name="user_address" id="user_address" placeholder="Address">
<center>
<span id="user_address_error" class="error"></span>
</center>
<input type="password" class="registrationForm" name="password" id="password" placeholder="Password">
<center>
<span id="user_password_error" class="error"></span>
</center>
<input type="password" class="registrationForm" name="user_confirm_password" id="user_confirm_password" placeholder="Confirm Password">
<center>
<span id="user_confirm_error" class="error"></span>
</center>
<button type="button" id="registrationBtn">REGISTER</button>
</form>
js/AJAX/jQuery js / AJAX / jQuery
$("#registrationBtn").click(function(){
var data = {
'username':$('#username').val(),
'email':$('#email').val(),
'user_mobile_number':$('#user_mobile_number').val(),
'user_address':$('#user_address').val(),
'password':$('#password').val(),
'user_confirm_password':$('#user_confirm_password').val()
};
$.ajax({
url: "/signUp",
method: 'POST',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: data,
//dataType: 'json',
success: function(data){
$('#Modal-Register').modal('hide');
$('#Modal-Login').modal('show');
},
error: function(data){
var errors = data.responseJSON;
if (errors.hasOwnProperty('username')){
$('#user_name_error').html(errors.username);
}
else {
$('#user_name_error').html('');
}
if(errors.hasOwnProperty('email')){
$('#user_email_error').html(errors.email);
}
else {
$('#user_email_error').html('');
}
if(errors.hasOwnProperty('user_mobile_number')){
$('#user_mobile_error').html(errors.user_mobile_number);
}
else {
$('#user_mobile_error').html('');
}
if(errors.hasOwnProperty('user_address')){
$('#user_address_error').html(errors.user_address);
}
else {
$('#user_address_error').html('');
}
if(errors.hasOwnProperty('password')){
$('#user_password_error').html(errors.password);
}
else {
$('#user_password_error').html('');
}
if(errors.hasOwnProperty('user_confirm_password')){
$('#user_confirm_error').html(errors.user_confirm_password);
}
else {
$('user_confirm_error').html('');
}
}
});
});
controller 控制者
class UserController extends Controller{
public function signUp(Request $request){
$this->validate($request,[
'username' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'user_mobile_number' => 'required|max:255',
'user_address' => 'required',
'password' => 'required|min:6',
'user_confirm_password' => 'required|same:password'
]);
$user = new User;
$user->username=$request->input('username');
$user->email=$request->input('email');
$user->user_mobile_number =$request->input('user_mobile_number');
$user->user_address=$request->input('user_address');
$password = Hash::make($request->input('password'));
$user->password = $password;
if ($user->save()){
echo "Successfully registered";
}
else{
echo "Failed to register";
}
}
}
How do Integrate adding of profile picture to this code? 如何将个人头像添加到此代码中?
Here's how i would do it 这是我会做的
<input type="hidden" value="image_id">
<input type="hidden" value="image_id">
$user->add_image($image)
This of course requires an Image
model and a relationship between User
and Image
(one to one?) and an add_image
function on the User
model 当然,这需要一个
Image
模型以及User
和Image
之间的关系(一对一?)以及User
模型上的add_image
函数。
First of all add enctype="multipart/form-data" to your form and add name="photo" to your 首先,将enctype =“ multipart / form-data”添加到您的表单中,然后将name =“ photo”添加到您的表单中
<input type="file">
Like this 像这样
<input type="file" name="photo"/>
and add the following to your controller 并将以下内容添加到您的控制器中
$path = $request->photo->storeAs('images', 'filename.jpg');
This will save the image to your storage folder 这会将图像保存到您的存储文件夹中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.