You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 4:获取轮播图当前显示图片的src并传入PHP控制器存入数据库

Got it, let's solve this problem step by step. The goal is to capture the src of the currently displayed avatar in your carousel and send it along with your form submission to your PHP controller. Here's how to modify your existing code to make this work:

Step 1: Add a Hidden Input to Your Form

First, we need a place to store the current avatar's src so it gets submitted with the form. Add a hidden input field inside your form in the Twig template. We'll initialize it with the first avatar's src to start:

<div class="app-form app-form-character_id">
  {{ form_start(idForm) }}
  {{ form_widget(idForm) }}
  <!-- Hidden field to track current avatar source -->
  <input type="hidden" id="currentAvatarSrc" name="current_avatar" value="{{ asset('images/avatars/001.png') }}">
  <button class="app-nav-btn app-form-btn" type="submit">CONFIRM</button>
  {{ form_end(idForm) }}
</div>

Next, modify your showDivs function to update the hidden field's value every time the carousel slides. This ensures the hidden field always holds the src of the currently visible image:

let slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  let i;
  let x = document.getElementsByClassName("app-avatar-slide-img");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  // Get the currently visible image
  const currentImage = x[slideIndex - 1];
  currentImage.style.display = "block";
  // Update the hidden field with its src
  document.getElementById('currentAvatarSrc').value = currentImage.src;
}

Step 3: Retrieve the Value in Your PHP Controller

Now, when the form is submitted, you can grab the current_avatar value from the request in your controller. Note that currentImage.src will give you the full URL, so if you need just the relative path (like images/avatars/003.png), you can parse it to remove the domain and base path:

use Symfony\Component\HttpFoundation\Request;

public function handleCharacterCreation(Request $request)
{
    $idForm = $this->createForm(IdFormType::class);
    $idForm->handleRequest($request);

    if ($idForm->isSubmitted() && $idForm->isValid()) {
        // Get form data from your existing form
        $formData = $idForm->getData();
        
        // Get the full avatar URL from the hidden field
        $fullAvatarUrl = $request->request->get('current_avatar');
        
        // Optional: Convert full URL to relative path (if needed)
        $baseUrl = $request->getSchemeAndHttpHost() . $request->getBasePath() . '/';
        $relativeAvatarPath = str_replace($baseUrl, '', $fullAvatarUrl);

        // Now you can use $fullAvatarUrl or $relativeAvatarPath in your logic
        // e.g., save to database, process further, etc.
    }

    // Render your page as before
    return $this->render('page.html.twig', [
        'idForm' => $idForm->createView(),
    ]);
}

How This Works:

  • The hidden field acts as a bridge between your frontend carousel state and the backend form submission.
  • Every time the user clicks the carousel buttons, the JavaScript updates the hidden field to match the current image's src.
  • When the form is submitted, the hidden field's value is sent along with the rest of the form data, making it accessible in your PHP controller.

内容的提问来源于stack exchange,提问作者clockworkmadness

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:29:08