简体   繁体   中英

Silex: redirect onchange select button (using URL Generator)

I'm using the Silex Framework. I'm creating a system where users can answer my questions. And I've got the following route set up:

$routes->match('/assignment/register/{type}/{count}', function (Request $request, $type, $count) use ($app) {

Now, there can be two types of questions: Multiple Choice or Open. The type of the route is either MC or OP. So, the form will be generated like this:

if($type == 'MC'){

        $builder->add('type', ChoiceType::class, array(
            'label' => 'Type',
            'required' => 'required',
            'attr' => array('class' => 'select-field', 'autocomplete' => 'off', 'onchange' => 'this.form.submit()'),
            'label_attr' => array('class' => 'label'),
            'choices' => array('Multiple Choice' => 'MC', 'Open' => 'OP'),
            'expanded' => false,
            'multiple' => false,
        ));

    }
    if($type == 'OP'){

        $builder->add('type', ChoiceType::class, array(
            'label' => 'Type',
            'required' => 'required',
            'attr' => array('class' => 'select-field', 'autocomplete' => 'off', 'onchange' => 'this.form.submit()'),
            'label_attr' => array('class' => 'label'),
            'choices' => array('Multiple Choice' => 'MC', 'Open' => 'OP'),
            'expanded' => false,
            'multiple' => false,
            'data' => 'OP',
        ));

    }

However, I want to be redirected when I choose for another type of question. So let's say I'm on the MC question, how can I be redirected to the OP page when I select OP in the select box? I thought something should be done with onchange='' . But I have no clue on how to continue.

Update

When I change the select button I 'd like to be redirected to the New URL:

Let's say I'm on the multiple choice page:

Www.MyWebsite.nl/Escape/public/assignment/create/MC/3

MC stands for multiple choice, and the 3 tells the script that I can give 3 answers.

Now when I set the select to Open Question (OP) I need to be redirected to this URL:

Www.MyWebsite.nl/Escape/public/assignment/create/OP/1

This has to be done using the URL generator in Silex/Symfony, because I can't just give the full URL

update 2

I've now got the following piece of code in my twig template. However, it still doesn't execute when I change my select box:

{% block scripts %}
    {% if urlTo is defined %}
          <script>
          var urlTo = {{ urlTo }}; 
          document.getElementById('form_type').onchange(function()
          {
            alert(urlTo);
            window.location.href(urlTo);
          });
        </script>
    {% endif %}
{% endblock %}

The URL is given perfectly. I've tested that, so that part does work. Where is my mistake?

To pass the URL from the controller to your script you just need to attach a template variable like so:

<?php
// somwhere in your route definition file
$app->match(
    '/assignment/register/{type}/{count}', 
    function (Request $request, $type, $count) use ($app) {
      // do your magic here
      $urlTo = $app['url_generator']->generate('path-name', [$path=>$options]);

      // here you pass the $urlTo variable to your template
      // by passing a 2n argument to the render method
      // https://silex.sensiolabs.org/doc/2.0/providers/twig.html#usage
      return $app['twig']->render('your-template-name-here.twig', ["urlTo" => $urlTo]);
    }
);

And then in your template where you define your JS (assuming you are using Twig, you have not specified on which templating are you using):

<?php
// this is your template file
<script>
  var urlTo = {{ urlTo }}; // here comes the injection from the PHP value to JS
  document.getElementById('the-id-of-the-select-element').onchange = function(e) {
    // probably you want to check the selected value here and then...
    window.location.replace(urlTo);
  };
</script>

This is a bare minimum skeleton to show how to pass the urlTo PHP variable into your template and convert it into a JS variable. From here on, you should elaborate on that and adapt this code to your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM