Contact form not doing anything in laravel 5.7 even not showing any error - laravel

This was my contact form and here is the form action
<form action="{{ route('contact.send')}}" method="post" class="contactForm">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage"></div>
<div class="row">
<div class="span4 form-group">
<input type="text" name="name" id="name" placeholder="Your Name"data-rule="required" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="span4 form-group">
<input type="email" name="email" id="email" placeholder="Your Email" data-rule="required" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="span4 form-group">
<input type="text" name="phone" id="phone" placeholder="Your Phone"data-rule="required" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="span6 form-group">
<input type="text" name="subject" id="subject" placeholder="Subject" data-rule="required" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="span6 form-group">
<input type="text" name="dateandtime" id="dateandtime" placeholder="Date and Time" data-rule="required" data-msg="Please Select a EST time and Date." />
<div class="validation"></div>
</div>
<div class="span12 margintop10 form-group">
<textarea name="message" rows="12" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
<p class="text-center">
<button class="btn btn-large btn-theme margintop10" type="submit">Submit message</button>
</p>
</div>
</div>
</form>
Bellow is the route and controller
Route::post('/contact','ContactController#sendMessage')->name('contact.send');
and the controller was
public function sendMessage(Request $request)
{
$this->validate($request,[
'name'=>'required',
'phone'=>'required',
'email'=>'required|email',
'subject'=>'required',
'messase'=>'required'
]);
$contact =new \App\contact();
$contact->name = $request->name;
$contact->email = $request->email;
$contact->phone = $request->phone;
$contact->subject = $request->subject;
$contact->date_and_time = $request->dateandtime;
$contact->message = $request->message;
$contact->status = false;
$contact->save();
Toastr::success('You Message Sent Successfully We will contact you soon!','Success',["positionClass" => "toast-top-center"]);
return redirect()->back();
}
And then the migration was as bellow
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('phone');
$table->string('email');
$table->string('date_and_time');
$table->text('message');
$table->boolean('status');
$table->timestamps();
});
}
But when I clicking on my submit button after filling up the form that not showing any error or not doing anything. Please help me to solve this problem.

If you are using laravel 5, here is what you need to do for showing success message:
In your controller file:
return redirect()->back()->with('success', 'your message,here');
In your blade file:
#if (\Session::has('success'))
<div class="alert alert-success">
<ul>
<li>{!! \Session::get('success') !!}</li>
</ul>
</div>
#endif
If you want to show error message than in your controller:
return Redirect::back()->withErrors(['msg', 'The Message']);
In your blade file:
#if($errors->any())
<h4>{{$errors->first()}}</h4>
#endif

Related

Contact us page receive information but not getting message

See the contact us page here.
This is view file of contact page.
<div class="section-title">
<h2>Contact</h2>
<h3><span>Contact Us</span></h3>
</div>
<div class="row" data-aos="fade-up" data-aos-delay="100">
</div>
<div class="row justify-content-center" data-aos="fade-up" data-aos-delay="100">
<div class="col-lg-9">
<form action="{{route('contact.store')}}" method="post" role="form" class="php-email-form">
#csrf
<div class="form-row">
<div class="col form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validate"></div>
</div>
<div class="col form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validate"></div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validate"></div>
</div>
<div class="mb-3">
<div class="loading">Loading</div>
<div class="sent-message">Your message has been sent. Thank you!</div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact Section -->
This is Controller code of contact us page.
public function store(Request $request)
{
Contact::create([
'name' => $request->name,
'email' => $request->email,
'subject' => $request->subject,
'message' => $request->message,
]);
\Session::flash('flash_message','successfully saved.');
return redirect()->back();
}
Problem is i have a one page theme so i redirect same page after contact us details fill. But contact details here stay , not blank....what is it solution?
don't use return back try by using return view
return view('viewName');
Because your browser will do "back" when you use your code:
return redirect()->back();
Change to use this:
return redirect()->route('your_back_route_name');
I think that will work as reload page.
When it's not work, i think it is about your browser auto fill.
So, set you input value is empty when load.
<input value="" />

Data Not Submitting to Database

I'm making a contact page but the form data is not saving to the database. What's the solution?
ContactController.php
public function contact()
{
if($request->isMethod('post'))
{
$data = $request->all();
}
$contact = new Contact;
$contact->name = $data['contact_name'];
$contact->email = $data['contact_email'];
$contact->subject = $data['contact_subject'];
$contact->body = $data['description'];
$category->save();
return redirect()->back()->with('flash_message_success',
'Your message has been sent successfully');
}
contact.blade.php
<form action="{{ url('/contact') }}" id="main-contact-form" class="contact-form row" name="contact-form" method="post">
{{ csrf_field() }}
<div class="form-group col-md-6">
<input type="text" name="contact_name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group col-md-6">
<input type="email" name="contact_email" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group col-md-12">
<input type="text" name="contact_subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group col-md-12">
<textarea name="description" id="message" required="required" class="form-control" rows="8" placeholder="Your Message Here"></textarea>
</div>
<div class="form-group col-md-12">
<input type="submit" name="submit" class="btn btn-primary pull-right" value="Submit">
</div>
</form>
Routes:
Route::get('contact', function(){
return view('contact');
});
Route::post('contact', function(){
return view('contact');
});
Use $contact->save(); not $category->save(); and also remove the if statement (for now): if($request->isMethod('post')) {
Your route should be:
Route::post('contact', 'ContactController#contact')->name('contact');

How to insert data in laravel 5.2 using ajax serialize() function?

I am having a trouble implementing data insertion using laravel by passing data from my view using serialize() function to my controller.I am just starting to play around laravel but I am now stacked on this. Begging someone to help me solve this. Thanks a lot. Below are my codes.
Product Form
<form class="form-horizontal prod-form" id="prod-form" style="background-color: #e2e2e2;" method="post" enctype="multiprodt/form-data">
<fieldset>
<div class="alert alert-dismissable alert-success alert-add-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<center><h4>Data successfully saved.</h4></center>
</div>
<address></address>
<input type="hidden" name="prod_id" class="prod_id" id="prod_id" value="">
<input type="hidden" name="_token" value="<?= csrf_token(); ?>">
<div class="form-group">
<label for="inputActivity" class="col-lg-2 control-label">Product Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[pharmaceutical]" id="inputPharmaceutical" placeholder="Product name" value="" style="width:260px;height:40px;" onchange="" required>
</div>
</div>
<div class="form-group">
<label for="inputActivity" class="col-lg-2 control-label">Description</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[description]" id="inputDescription" placeholder="Description" value="" style="width:260px;height:40px;" onchange="" required>
</div>
</div>
<div class="form-group">
<label for="inputActivity" class="col-lg-2 control-label">Unit</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[unit]" id="inputUnit" placeholder="Unit" value="" style="width:260px;height:40px;" onchange="" required>
</div>
</div>
<div class="form-group">
<label for="inputVenue" class="col-lg-2 control-label">Price</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[price]" id="inputPrice" placeholder="Price" value="" style="width:260px;height:40px;" required>
</div>
</div>
<div class="form-group">
<label for="inputSponsors" class="col-lg-2 control-label">Quantity</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[quantity]" id="inputQuantity" placeholder="Quantity" value="" style="width:260px;height:40px;" required>
</div>
</div>
<div class="form-group">
<label for="inputSponsors" class="col-lg-2 control-label">Amount</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="prod[amount]" id="inputAmount" placeholder="Amount" value="" style="width:260px;height:40px;" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-primary submit-prod">Submit</button>
<button class="btn btn-default">Cancel</button>
</div>
</div>
</fieldset>
</form>
Javascript Function when submit button is clicked
<script type="text/javascript">
$(".submit-prod").click(function(e){
e.preventDefault();
var button_text = $(this).text();
alert($("#prod-form").serialize());
$.post("{{ url('/addprod') }}",$("#prod-form").serialize(),function(data){
if(data.notify == "Success"){
console.log(data.notify);
}
},"json");
}); //end
</script>
Route.php
Route::group(['middleware' => 'web'], function () {
Route::post('addprod', 'Product\ProductController#store');
Route::get('/home', 'HomeController#index');
});
ProductController.php
<?php
namespace App\Http\Controllers\Product;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Product\Product as Product;
class ProductController extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
public function create(){
}
public function store(Request $request){
//$product = new Product;
$prod_details = $request->all();
$query = Product::create($prod_details);
if($query){
$notification = "Success";
} else{
$notification = "Failed";
}
echo json_encode(array('notify'=>$notification));
}
}
Model: Product.php
<?php
namespace App\Product;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
}
Sample Input:
Error Output:
Well the issue is the csrf-token please user form helper class to declare your forms or declare the token. Else you will take millenniums to solve your problem

Laravel 5.2 cannot update record

I cannot seem to update my record.
My controller
public function add()
{
return view('cars.add');
}
public function edit($id)
{
$car = Cars::whereId($id)->firstOrFail();
return view('cars.edit', compact('car'));
}
public function store(CarFormRequest $request)
{
$car = new Cars(array(
'name' => $request->get('name'),
'color_id' => $request->get('color')
));
$car->save();
$car->position_id = $car->id;
$car->save();
session()->flash('status', 'Successfully Added a Car!');
return view('cars.add');
}
public function update($id, CarFormRequest $request)
{
$car = car::whereId($id)->firstOrFail();
$car->name = $request->get('name');
$car->color_id = $request->get('color');
if($request->get('status') != null) {
$car->status = 0;
} else {
$car->status = 1;
}
$car->save();
return redirect(action('CarController#edit', $car->id))->with('status', 'The ticket '.$id.' has been updated!');
}
my routes:
Route::get('/', 'PagesController#home');
Route::get('/about', 'PagesController#about');
Route::get('/contact', 'PagesController#contact');
Route::get('/cars', 'CarsController#index');
Route::get('/cars/edit/{id?}', 'CarsController#edit');
Route::post('/cars/edit/{id?}', 'CarsController#update');
Route::get('/cars/add', 'CarsController#add');
Route::post('/cars/add', 'CarsController#store');
here is my view:
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" method="post">
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<input type="text" id="color_id" name="color_id" value="{!! $car->color_id !!}">
<fieldset>
<legend>Edit Car Information</legend>
<div class="form-group">
<label for="title" class="col-lg-2 control-label">Car Name</label>
<div class="col-lg-10">
<input type="text" value="{{ $car->name }}" class="form-control" id="name" placeholder="Car Name">
</div>
</div>
<div class="form-group">
<label for="title" class="col-lg-2 control-label">Car Color</label>
<div class="col-lg-10">
<div class="btn-group" data-toggle="buttons">
<label id="opt1" class="btn btn-primary">
<input type="radio" name="color" id="option1" autocomplete="off"> Red
</label>
<label id="opt2" class="btn btn-primary">
<input type="radio" name="color" id="option2" autocomplete="off"> Blue
</label>
<label id="opt3" class="btn btn-primary">
<input type="radio" name="color" id="option3" autocomplete="off"> Yellow
</label>
<label id="opt4" class="btn btn-primary">
<input type="radio" name="color" id="option4" autocomplete="off"> Green
</label>
<label id="opt5" class="btn btn-primary">
<input type="radio" name="color" id="option5" autocomplete="off"> Black
</label>
<label id="opt6" class="btn btn-primary">
<input type="radio" name="color" id="option6" autocomplete="off"> White
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
The $id variable in whereIn must be array and you need to specify the database column too. This should be like -
public function edit($id)
{
$car = Cars::whereId('id', [$id])->firstOrFail();
return view('cars.edit', compact('car'));
}
Change all occurrence of
$car = car::whereId($id)->firstOrFail();

Stripe form not submitting

My stripe form is not submitting. When I press submit it goes to page cannot be displayed. I replaced the route code to resolve to an echo "test"; and the post request shows the echo. Any help would be appreciated. This code is from a tutorial http://www.sitepoint.com/selling-downloads-stripe-laravel/
Route
Route::get('/buy/{id}', function($id)
{
$download = Download::find($id);
return View::make('buy', array('download' => $download));
});
Route::post('/buy/{id}', function($id)
{
Stripe::setApiKey(Config::get('laravel-stripe::stripe.sk_test_aaaaaaaaaaaaaaaaaaaaaaaa'));
$download = Download::find($id);
$token = Input::get('stripeToken');
// Charge the card
try {
$charge = Stripe_Charge::create(array(
"amount" => $download->price,
"currency" => "gbp",
"card" => $token,
"description" => 'Order: ' . $download->name)
);
// If we get this far, we've charged the user successfully
Session::put('purchased_download_id', $download->id);
return Redirect::to('confirmed');
} catch(Stripe_CardError $e) {
// Payment failed
return Redirect::to('buy/'.$id)->with('message', 'Your payment has failed.');
}
});
View
#extends('layouts.default')
#section('content')
<h1>Your Order</h1>
<h2>{{ $download->name }}</h2>
<p>£{{ ($download->price/100) }}</p>
<form action="" method="POST" id="payment-form" role="form">
<input type="hidden" name="did" value="{{ $download->id }}" />
<div class="payment-errors alert alert-danger" style="display:none;"></div>
<div class="form-group">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number" class="form-control input-lg" />
</label>
</div>
<div class="form-group">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc" class="form-control input-lg" />
</label>
</div>
<div class="form-group">
<label>
<span>Expires</span>
</label>
<div class="row">
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-3">
<input type="text" size="2" data-stripe="exp-month" class="input-lg" placeholder="MM" />
</div>
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-3">
<input type="text" size="4" data-stripe="exp-year" class="input-lg" placeholder="YYYY" />
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Submit Payment</button>
</div>
</form>
#stop

Resources