0

How should I do this?

 $ids = ArrayHelper::map(Tours::find()->select(['pk', 'programa'])->asArray()->all(), 'pk', 'programa');


$this->registerJs('
$("#child1_child2").change(function() {
    var val = $("#child1_child2 option:selected").val();
    valInt = parseInt(val);
    $("#form").attr("action", "programas/" + "'.$ids[.'"valInt"'.].'");
});
', \yii\web\View::POS_END);

I dont know how to parse this part:

$("#form").attr("action", "programas/" + "'.$ids[.'"valInt"'.].'");

$ids it's a php array

valInt is a js varibale

I thought this was correct but I'm getting parse error

syntax error, unexpected '.', expecting ']'

if I write "'.$ids[40].'" it works great and with no errors.

Thi is in the view of site/index.php, working with Yii2.

Thanks for the help!

4
  • You can't pass JS you're outputting back to the server and expect a response without using AJAX. Alternatively, you can put a json encoded version of $ids as a var in the JS and then index the JS version of $ids... Commented Nov 30, 2016 at 21:25
  • You might emit the $ids so that it was a javascript array on the client and then you could index it with valInt Commented Nov 30, 2016 at 21:28
  • but "'.$ids[40].'" its working fine or is not related? Commented Nov 30, 2016 at 21:31
  • It's working because $ids[40] is set as a php array. The problem is that you're trying to get the statically generated JS to be dynamic before it's sent to the browser. That won't fly. Commented Nov 30, 2016 at 21:49

1 Answer 1

1

Try this

$ids = ArrayHelper::map(Tours::find()->select(['pk', 'programa'])->asArray()->all(), 'pk', 'programa');


$this->registerJs('
$("#child1_child2").change(function() {
    var val = $("#child1_child2 option:selected").val();
    valInt = parseInt(val);
    var $ids = '. json_encode($ids) .';
    $("#form").attr("action", "programas/" + $ids[valInt]);
});
', \yii\web\View::POS_END);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.