What could be causing the null value issue when sending data to a controller via Laravel AJAX?

In my annonces table, I have multiple images and I want to update them. When I try to send the data (id, images, token) to the controller, it is returning null for the id.

details.blade.php

<h3 class="jumbotron" id="id">{{ $annonces->id }}</h3>
<h3 class="jumbotron">Add product images</h3>
<form method="post" action="{{route('filesUpdate')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">
   <!--<input type="hidden" name="_method" value="PUT">-->
  {{ csrf_field() }}                            
</form>  
  <script type="text/javascript">
            var id     = $("#id").val();
            Dropzone.options.dropzone = 
            {
              console.log(id);
            maxFilesize: 12,
            renameFile: function(file) {
                images = file.name;
                $.ajax({
                 url:"{{ route('filesUpdate') }}" ,
                 type:'POST',
                 data:{
                    id      : id,
                    "_token": "{{ csrf_token() }}"
                },
                 success: function(file, response) 
                  {
                      console.log(response);
                  },
                  error: function(file, response)
                  {
                     return false;
                  }
              })
               //return  images;
            }, 
            acceptedFiles: ".jpeg,.jpg,.png,.gif",
            addRemoveLinks: true,
            timeout: 50000,
            success: function(file, response) 
            {
                console.log(response);
            },
            error: function(file, response)
            {
               return false;
            }
            };
  </script>

AnnoncesController.php

 public function filesUpdate(Request $request)
    {
      dd($request->id);
    }

Answer №1

Change this code snippet from

var id = $("#id").val(); // Using val() method for input elements.

To the following

var id = $("#id").html(); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve webpage using HTML stored in web storage

I am exploring a new idea for an application that utilizes local storage, and I am seeking guidance on the most effective way to load content after it has been stored. The basic concept of the app involves pre-fetching content, storing it locally, and the ...

Having trouble retrieving the value of the second dropdown in a servlet through request.getParameter

I am facing an issue with storing the value of the second dropdown in a servlet after utilizing an ajax call in Java to populate it based on the selection made in the first dropdown. While I was able to successfully store the value of the first dropdown ...

Extracting Query Parameters from a Successful Ajax Call in Javascript

How can I extract data from the success response? Take a look at my code snippet: $.ajax({ async: 'true', url: 'https://exampleapi.com/product', type: 'GET', data: {page: idQuery}, success:(response => { v ...

Accessing information from an ASP.NET web service through an Ajax request in AngularJS

UPDATE: included additional methods for clarity enhancement I am attempting to fetch data from an ASP.NET webservice in C# using Ajax calls in AngularJS. The webmethod is structured as follows: [WebMethod] public User getUserByEmail(String Email) ...

What is causing the failure of success function commands to be executed?

Currently, I am facing some inconsistencies between my locally hosted app on WAMP (working perfectly) and the deployed version of my app. In an attempt to resolve this issue, I am working with CodeIgniter and jQuery AJAX. As part of my troubleshooting proc ...

Create a servlet that generates a PDF file and displays it in a fresh browser tab. However, the PDF document will be blank

I have a servlet that currently provides a PDF byte array: byte[] pdf = myService.getPdf(myArgs); response.setContentType("application/pdf"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check= ...

Move and place, editing tools

Is it possible to create a drag-and-drop editor similar to the one found in the form editor on wufoo.com? ...

Establishing a time frame using AJAX through a separate PHP script

In order to display either a video or an image based on a specific time range, I want to utilize the client's local time by taking it from the website. Here is what I have done so far: 1st file (time.php): <?php $b = time (); print date("H:i", $b ...

Unable to retrieve data from the JSON file after making a $http.post call

Currently facing an issue with my grocery list item app developed in AngularJS. I have simulated a connection to a server via AJAX requests made to local JSON files. One of the files returns a fake server status like this: [{ "status": 1 }] I am att ...

Prevent Ajax form submission before transmission

I'm currently working with a content management system that generates a form and uses AJAX to submit it. I want to validate the input fields before submission, but the CMS automatically sends the form on the submit event. bind(form, "submit", functio ...

Experiencing a 400 Bad Request error while attempting an Ajax call to a Controller in ASP.NET

Having trouble making a simple request from a CSHTML page to a Controller in my ASP.NET MVC website application. Attempting a DELETE call using Ajax: $.ajax({ url: "/company/delete", type: "DELETE", success: function (e) { showAlert(&a ...

Using Rails' link_to method within Bootstrap modals

I'm trying to implement a voting system where users can vote on different items displayed in a Bootstrap modal. Each item is listed as a button on the index page, and when clicked, it opens up the modal for voting. However, I'm facing challenges ...

Understanding the source component that triggered a p:ajax request

Dealing with multiple input fields each connected to the same listener using p:ajax, how can I determine which component triggered the listener? <h:inputText id="postalCode" size="20" value="# businessPartner.primaryAddress.postalCode}" <p:ajax eve ...

Utilize ReactJS and AJAX to easily upload images

Having trouble with uploading an image through a form to a url/api. When I submit the form, I'm calling the handleImgUpload() method. The issue is that the request being sent is coming back empty. Seems like there might be a problem with new FormData ...

Submitting Data Forms with AJAX on dynamically loaded webpages

Issue with Form Submission in Ajax-Generated Page I am experiencing an issue with form submission on a page generated within AJAX. The page contains two forms, #form1 and #form2. The jQuery code for submitting the form is as shown below: jQuery("#form1" ...

Refreshing div content on selection change using PHP ajax with Jquery

Here is my code for the portfolio.php file <select name="portfolio" id="portfolio_dropdown" class="service-dropdown"> <?php foreach($years as $year){ ?> <option value="<?php echo $year[&apo ...

Using Ajax and Servlet to populate a text field

Trying to fetch a value from a Servlet to an input box in a JSP using Ajax. Successfully retrieved the value to an Alert box but facing issues with placing it in an input box. Below is the code snippet for reference: AjaxTestServlet Code: protected voi ...

Failure to update hit counter in MySQL database using Ajax request

Below is the code snippet for loading a list of videos from a database on the index page: <?php ini_set('display_errors', 1); mysqli_set_charset($link, 'utf8mb4'); $query="SELECT * FROM videos"; $result=mysqli_query($lin ...

What are the steps for performing a self-triggered AJAX post request?

I have been exploring self-invoked functions and recently used an http.get function to retrieve data from a JSON file like this: var Callmodule = (function(){ var urljsonEntrata= "modello.json"; function getmodules(){ var req = $.ajax({ url: ...

The location.reload function keeps reloading repeatedly. It should only reload once when clicked

Is there a way to reload a specific div container without using ajax when the client requests it? I attempted to refresh the page with the following code: $('li.status-item a').click(function() { window.location.href=window.location.href; ...