0

I have the following Javascript which allows me to generate a select input box when a button is clicked and append the select box to the .cardday div:

        <script>
            $('input[name="queue"]').click(function(){
                $("<select name='select[]'>"+
                    "<option value='volvo'>Volvo</option>"+
                    "<option value='saab'>Saab</option>"+
                    "<option value='mercedes'>Mercedes</option>"+
                    "<option value='audi'>Audi</option>"+
                    "</select>").appendTo('.cardDay');
            })
</script>

I also have a PHP array with the values that I need to put inside the select box. This array is called $exerciseList

How can I have it so when the select box is added to the card div it will have populated with the PHP array $exerciseList key and value?

Full file as requested:

    @extends('layouts.app')

@section('content')

<div class="card" style="padding:10px;">
        {!! Form::open(['action' => ['ExercisePlansController@store'], 'method' => 'POST']) !!}
        <center><h3 style="margin-top:10px;margin-bottom:20px;">Create Training Plan
                {{ Form::submit('Create this Plan', ['class' => 'btn btn-success', 'style' => 'float:right;align:right;margin-right:10px;']) }}
                </h3></center>
    <hr style="margin:10px;"></hr>


    <div class="form-group">
            {{ Form::label('title', 'Name') }}
            {{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Exercise Plan Name E.g Advanced Bulking']) }}
    </div>
    <div class="form-group">
        {{ Form::label('plan', 'Calender Plan') }}</br>
        <div class="col-md-2" style="padding:0px 10px 0px 0px;flex:0%;">
            <div class="cardDay" style="padding:10px;background-color:#c7e0fc;">

                <h3 style="margin-bottom:3px;">
                    <center>Monday</center>
                    <hr style="margin:10px;"></hr>
                </h3>
                <input class="btn btn-primary" style="font-family:Helvetica;padding:10px;margin:5px 0px 5px 0px;width:100%;" name="queue" value="Add Exercise">
                    <div class="panel-group">
                        <div class="panel panel-default">
                          <div class="panel-heading">
                            <h4 class="panel-title">
                              <a data-toggle="collapse" href="#collapse1001">New Exercise</a>
                            </h4>
                          </div>
                          <div id="collapse1001" class="panel-collapse collapse in">
                                <div class="panel-body">

                                    <div class="form-group">    
                                        {{ Form::label('exerciseList', 'Exercise') }} <br/>
                                        {{ Form::select('exerciseList[]', $exerciseList) }} <br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('description', 'Exercise Description') }} <br/>
                                            <a class="card-link"  href="./exercise/">Exercise Details</a><br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('reps', 'Number of Reps') }} <br/>
                                            {{ Form::text('reps[]', "") }} <br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('sets', 'Number of Sets') }} <br/>
                                            {{ Form::text('sets[]', '') }} <br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('weight', 'Weight (kg)') }} <br/>
                                            {{ Form::text('weight[]', '') }} <br/>
                                    </div>     
                                </div>

                          </div>
                        </div>
                      </div>
        </div>
            <center>
            {{ Form::hidden('_method', 'PUT') }}

            </center>
        {!! Form::close() !!}
        "{{ Form::label('exerciseList', 'Exercise') }} <br/>"+
        "{{ Form::select('exerciseList[]', $exerciseList) }} <br/>"+
        <script>
            $('input[name="queue"]').click(function(){
                $("<select name='select[]'>"+
                    "<option value='volvo'>Volvo</option>"+
                    "<option value='saab'>Saab</option>"+
                    "<option value='mercedes'>Mercedes</option>"+
                    "<option value='audi'>Audi</option>"+
                    "</select>").appendTo('.cardDay');
            })
        </script>
@endsection
4
  • Post your html file. Basically you just need to iterate over your array before the page render Commented Apr 16, 2018 at 18:38
  • I don't understand what you are trying to achieve here. Do you want to populate $exerciseList from PHP to select box when you click a button? Commented Apr 16, 2018 at 18:39
  • The array $exerciseList is already populated. When the button is click, this javascript adds a new div to my page and inside that new div is this select box. I need the select box to contain the values that are already stored in $exerciseList. Does that make any more sense? Commented Apr 16, 2018 at 18:43
  • Possible duplicate of Generating a JavaScript array from a PHP array Commented Apr 16, 2018 at 18:47

2 Answers 2

2

Have the server generate the Javascript:

<script>
    $('input[name="queue"]').click(function(){
    $("<select name='select[]'>"+
    <?php foreach ($exerciseList as $value){ ?>
        "<option value='<?php echo $value; ?>'><?php echo $value; ?></option>"+
    <?php } ?>
    "</select>").appendTo('.cardDay');
    })
</script>

Therefore, when the Javascript is loaded in the browser the code will have your PHP variable values included.

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

5 Comments

Something like this but I was trying this with a for loop to get the data out of the $exerciseList array. The $exerciseList array is large and changes from time to time.
Looks like what I am after, I didn't know you could use a for inside js. I need to bite the bullet and learn js. Anyway I am getting Undefined variable: value
ive just remembered you will need to wrap the foreach opening and closing segments in php tags but this code will work foryou needs
Worked. Thank you!
No problem at all :)
1

Use php serverside to generate the scripts.

<script>
    $('input[name="queue"]').click(function(){
    $("<select name='select[]'>"+
    @for($list in $exerciseList)
        "<option value={{ $list}}> {{ $list}} </option>"+
    @endfor
    "</select>").appendTo('.cardDay');
    })
</script>

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.