0

I have an Ajax call to create a new Quiz, with a link to the edit page of the quiz (using Laravel)

Now I have inside my JavaScript a PHP function, which therefor has a JavaScript variable again.

How is the variable data.id inserted inside the php asset function?

$('#newQuiz').click(function () {
    var quizName = $('#quiz-name').val();
    var url = "quizzes/store";
    var token = '{{ csrf_token() }}';

    $.ajax({
        type: "POST",
        url: url,
        data: {
            'name': quizName,
            '_token': token
        },
        success: function (data) {
            $('.quiz-table > tbody').append(
                    '<tr>' +
                    '<td>' + data.id + '</td>' +
                    '<td>' + data.name + '</td>' +
                    '<td>' + '<button type="button" class="btn btn-warning"><a href="{{ asset("quizzes/".+data.id+."/questions") }}">Edit Quiz</a></button>' + '</td>' +
                    '<td>' + '<button type="button" class="btn btn-success">Play Quiz</button>' + '</td>' +
                    '</tr>'
            );

            $('#quiz-name').val('');
        }
    });
});

This rule for clarification {{ asset("quizzes/".+data.id+."/questions") }}

1
  • You haven't actually posted any PHP, only what looks like some Twig placeholders. In any case, you can't use PHP on JavaScript like this because PHP runs only server-side. Commented Oct 2, 2015 at 12:34

1 Answer 1

2

How is the variable data.id inserted inside the php asset function?

You can't. PHP runs on the server, you can't set the value of a PHP variable via Javascript - the opposite is true though.

Sign up to request clarification or add additional context in comments.

1 Comment

@Drown he could do another ajax call though, I suspect since data.id is being returned by success, it was already in php form first.

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.