Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alewa's avatar
Level 2

HTML form clicking error

web.php

Route::post('/addfavorite/{id}', [App\Http\Controllers\PropertyController::class, 'addFavorite']);

properties.blade.php file

<!-- Gallery -->
<section id="property" class="padding bg_gallery">
  <div class="container">
    <div id="property-gallery" class="cbp listing1">
    @foreach($properties as $property)
      <div class="cbp-item latest sale">
        <div class="property_item">
          <div class="image">
            <a href="{{ url('property_detail/'.$property->id) }}"><img id="dose" src="{{ $property->property_image == '' ? '/backend/assets/images/avatars/thumb-3.jpg':  '/'.$property->property_image }}" alt="latest property" class="img-responsive"></a>
            <div class="price clearfix"> 
              <span class="tag pull-right">GHC {{ $property->price }}
                @if($property->payment_type != '')
                {{ $property->payment_type }}
                @else
                @endif</span>
            </div>
            <span class="tag_t">{{ $property->property_type }}</span> 
          </div>
          <div class="proerty_content">
            <div class="proerty_text">
              <h3 class="captlize"><a href="{{ url('property_detail/'.$property->id) }}">{{ $property->name }}</a></h3>
              <p>{{ $property->location }}</p>
            </div>
            <div class="property_meta transparent"> 
              @if($property->land_size != '')
              <span><i class="icon-select-an-objecto-tool"></i>{{ $property->land_size }}</span> 
              @else
              @endif
              @if($property->bedroom != '')
              <span><i class="icon-bed"></i>{{ $property->bedroom }} Bedroom(s)</span>
              @else
              @endif 
              <span style="color:blue;"><i class="icon-safety-shower"></i>{{ $property->property_category->name }}</span>    
            </div>
            <div class="favroute clearfix">
              <p><i class="icon-calendar2"></i>&nbsp;{{ $property->created_at->toFormattedDateString() ?? '' }} </p>
              <ul class="pull-right">
                <li>
                  <form action="{{ url('addfavorite/'.$property->id) }}" method="post">
                    @csrf
                    <button type="submit"><i class="icon-like"></i></button>
                  </form>
                </li>
                <li><a href="#" class="share_expender" data-toggle="collapse"><i class="icon-share3"></i></a></li>
              </ul>
            </div>
            <div class="toggle_share collapse" id="seventy">
              <ul>
                <li><a href="javascript:void(0)" class="facebook"><i class="icon-facebook-1"></i> Facebook</a></li>
                <li><a href="javascript:void(0)" class="twitter"><i class="icon-twitter-1"></i> Twitter</a></li>
                <li><a href="javascript:void(0)" class="vimo"><i class="icon-vimeo3"></i> Vimeo</a></li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      @endforeach
    </div>
    @if(isset($_GET['sort']))
    <div class="col-sm-12 text-center top20">
      {{ $properties->appends(['sort'=>$_GET['sort']])->links() }}
    </div>
    @else
    <div class="col-sm-12 text-center top20">
      {{ $properties->links() }}
    </div>
    @endif
  </div>
</section>
<!-- Gallery End -->

PropertyController

<?php

namespace App\Http\Controllers;

use App\Models\Property;
use App\Models\PropertyCategory;
use App\Models\Blog;
use App\Models\Admin;
use App\Models\Favorite;
use Illuminate\Support\Str;
use Carbon\Carbon;
use RealRashid\SweetAlert\Facades\Alert;
use Session;
use Auth;
use DB;
use Illuminate\Http\Request;

class PropertyController extends Controller
{
    public function index(){
        $pg = "property";

        $properties = Property::with(['property_category'])->where('status','1')->where('purchase_status','Not Sold');

        //Checking or sorting properties by prices and name
        if(isset($_GET['sort']) && !empty($_GET['sort'])){
          if($_GET['sort']=="property_latest"){
            $properties->orderby('properties.id','Desc');
          }else if($_GET['sort']=="price_lowest"){
            $properties->orderby('properties.price','Asc');
          }else if($_GET['sort']=="price_highest"){
            $properties->orderby('properties.price','Desc');
          }else if($_GET['sort']=="name_a_z"){
            $properties->orderby('properties.name','Desc');
          }else if($_GET['sort']=="name_z_a"){
            $properties->orderby('properties.name','Asc');
          }
        }

        $properties = $properties->inRandomOrder()->paginate(3);
        return view('properties',compact('properties','pg'));
    }

    public function newProperties(){
      $pg = "newproperty";
      
      $newproperties = Property::with(['property_category'])->where('status','1')->where('purchase_status','Not Sold')->latest()->paginate(3);
      return view('new_properties',compact('newproperties','pg'));
    }
  
    public function details($id){
        $pg = "property";

        $property = Property::with(['property_category','property_images'])->where(['id'=>$id,'status'=>1])->where('purchase_status','Not Sold')->first();
  
        return view('property_detail',compact('property','pg'));
    }
  
    public function categoryProperties($slug){
        if(PropertyCategory::where('slug', $slug)->exists()){
  
          $pg = "property";
          $propertycategory = PropertyCategory::where('slug', $slug)->first();
          $catproperties = Property::where('property_category_id',$propertycategory->id)->where('status',1)->paginate(2);
          return view('cat_properties',compact('catproperties','propertycategory'));
        }else{
          abort(404);
        }
  
    }

    public function searchProperty(Request $request)
    {
        $propertycategories = PropertyCategory::where('status','1')->get();
        $query = $request->input('query');
        //you can search property by name, address, location, but we are searching by name
        $properties = Property::with(['property_category'])->where('name','LIKE',"%$query%")->where('status','1')->where('purchase_status','Not Sold')->get();
        return view('search_property',compact('properties','query','propertycategories'));
    }

    public function addFavorite(Request $request, $id){
      $pg = "favorite";

      if($request->isMethod('post')){
          $data = $request->all();

          //Get property id
          $property = Property::find($id);
          $property_id = $property->id;

          //Generate Session Id at cart table if not exist and if user has not login
          $session_id = Session::get('session_id');
          if(empty($session_id)){
            $session_id = Session::getId();
            Session::put('session_id',$session_id);
          }

          //If properties already exist in cart table
          if(Auth::check()){
            //if user is login
            $session_id = "";
            $user_id = Auth::user()->id;
            $countProperties = Favorite::where(['property_id'=>$property_id,'user_id'=>$user_id])->count();
          }else{
            //If user is not login
            $user_id = 0;
            $countProperties = Favorite::where(['property_id'=>$property_id,'session_id'=>$session_id])->count();
          }

          if($countProperties>0){
            Alert::warning('Property Already exist in cart!');
            return back();
          }

          //Add properties to cart table
          $item = new Favorite;
          $item->session_id = $session_id;
          $item->user_id = $user_id;
          $item->property_id = $property_id;
          $item->save();

          Alert::success('Property Added to favorite successfully! <a href="/favorite_properties">View Favorite</a>');
          return back();
      }
  }

  public function favoriteProperty(){
    $pg = "favorite";

    if(Auth::check()){
      //If user is logged in / pick auth id of user
      $getFavoriteItems = Favorite::with(['property'=>function($query){
          $query->select('id','name','price','property_type','location','property_image','property_category_id');
      }])->where('user_id',Auth::user()->id)->orderby('id','Desc')->get();
    }else{
      //If user is not logged in / pick session id
      $getFavoriteItems = Favorite::with(['property'=>function($query){
          $query->select('id','name','price','property_type','location','property_image','property_category_id');
      }])->where('session_id',Session::get('session_id'))->orderby('id','Desc')->get();
    }
    return view('favorite_properties',compact('getFavoriteItems','pg'));
  }

  public function destroyFavorite($id){
    DB::table('favorites')->where('id',$id)->delete();
    Alert::success('Favorite item deleted successfully');
    return back();
  }

}

But when the properties displays on the browser, anytime i click on add to favorites of the first property, it does not work, but when you click on the add to favorite of the other properties, it works perfectly, and this add to favorite function is working on all my pages with the same code, except the first property in the properties page, and when i inspect the form does not show, but shows for the other properties

<li>
<input type="hidden" name="_token" value="T83Q3dqRaqA49nKQdKN1XOvW6Gmhs4ZDO78VHtMx">
<button type="submit">
<i class="icon-like"></i>
</button>
</li>

But the others come like this

<li>
<form action="http://emmadocestates.test/addfavorite/2" method="post">
<input type="hidden" name="_token" value="T83Q3dqRaqA49nKQdKN1XOvW6Gmhs4ZDO78VHtMx">                    <button type="submit">
<i class="icon-like"></i>
</button>
 </form>
</li>

What could be the issue?, do i have to do something in my browser?

0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

The dump of what you see in the html, is that using the devtools element inspector?

Try viewing the actual page source (view source from right click on page)

My guess at this point is that you have an un-closed form elsewhere in the page. Can't see any issue with this section.

1 like
Alewa's avatar
Level 2

thanks, i had a form that was not closed. is now working

Alewa's avatar
Level 2

but when deleting the properties in the favorites table, i am having a problem, instead of it to delete and redirect back, it is rather redirecting me to a different page without deleting the properties in the favorite table Web Route

Route::get('/delete-property/{id}', [App\Http\Controllers\PropertyController::class, 'destroyFavorite']);

PropertyController

public function destroyFavorite($id){
    DB::table('favorites')->where('id',$id)->delete();
    Alert::success('Favorite item deleted successfully');
    return back();
  }

favorite_properties.blade.php file

@foreach($getFavoriteItems as $favorite)
    <div class="row bg-hover">
      <div class="my-pro-list">
	    <a href="{{ url('property_detail/'.$favorite->property_id) }}">
        <div class="col-md-2 col-sm-2 col-xs-12">
          <img id="sly" class="img-responsive" src="{{ $favorite->property->property_image == '' ? '/backend/assets/images/avatars/thumb-3.jpg':  '/'.$favorite->property->property_image }}" alt="image"/>
        </div>
		</a>
        <div class="col-md-8 col-sm-8 col-xs-12">
          <div class="feature-p-text">
            <h4>{{ $favorite->property->name }}</h4>
            <p>Property Area, {{ $favorite->property->location }}</p>
            <span><b>Status:</b>  For Sale</span><br>
            <div class="button-my-pro-list">
              <a href="property_detail.html">GHC {{ $favorite->property->price }}</a>
            </div>
          </div>
        </div>
        <div class="col-md-2 col-sm-2 col-xs-12">
          <div class="select-pro-list">
            <a href="{{ url('property_detail/'.$favorite->property_id) }}"><i class="icon-eye"></i></a>
            <a href="/delete-property/{{ $favorite->id }}" type="submit" onClick="return confirm('Are you sure You want to Delete')"><i class="icon-cross"></i></a>
          </div>
        </div>
      </div>
    </div>
    @endforeach

Please what is the issue?

Snapey's avatar
        <a href="{{ url('property_detail/'.$favorite->property_id) }}"><i class="icon-eye"></i></a>
        <a href="/delete-property/{{ $favorite->id }}" type="submit" onClick="return confirm('Are you sure You want to Delete')"><i class="icon-cross"></i></a>

Any reason why you didn't use the URL helper like in the first link?

You should never perform something like this with a delete route as its too easy for a web crawler to crawl links and delete all favourites of all users. I know you have an on-click check but a crawler (or your browser pre-loading links) will not care about your on-click.

  1. use a form and submit it when the delete button is clicked
  2. check ownership of the favourite before deleting
1 like
Alewa's avatar
Level 2

even when i use it, it redirects to /delete-property/2 and does not delete the properties in the favorites table

Please or to participate in this conversation.