I want to make a button to add new item row :
This is the HTML in create.blade.php
<div class="createOrderForm" id="orderMenuItems">
<div class="orderItem">
<label> Item : </label>
<select name="item[]" required>
<option value="">Menu Items</option>
@foreach ($menuItems as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
@endforeach
</select>
<label>Quantity :</label>
<input type="text" name="quantity[]" value="" required>
</div>
</div>
and this is create.js that's included :
$(document).ready(function () {
console.log("Wele To create order Page");
var addItem = $('#addItem');
var orderMenuItems = $('#orderMenuItems');
$(addItem).click(function(e){
var newItem = '<div class="orderItem">';
newItem += '<label> Item : </label>';
newItem += '<select name="orderItem[]">';
newItem += '<option value="">Menu Items</option>';
newItem += "@foreach ($menuItems as $item)";
var itemID = "{!! $item->id !!}";
newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';
newItem += "@endforeach";
newItem += '</select>';
newItem += '<label>Quantity :</label>';
newItem += '<input type="text" name="quantity[]" value="" required>';
newItem += '</div>';
$(orderMenuItems).append(newItem);
});
});
The button works and adds new row, but there is a problem with the menu items in the new row :
I think the problem is here :
newItem += "@foreach ($menuItems as $item)";
var itemID = "{!! $item->id !!}";
newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';
newItem += "@endforeach";
I tried to use :
@foreach ($menuItems as $item)
....
@endforeach
instead of :
newItem += "@foreach ($menuItems as $item)";
....
newItem += "@endforeach";
but it doesn't work.
How Can I fix it ??
I want to make a button to add new item row :
This is the HTML in create.blade.php
<div class="createOrderForm" id="orderMenuItems">
<div class="orderItem">
<label> Item : </label>
<select name="item[]" required>
<option value="">Menu Items</option>
@foreach ($menuItems as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
@endforeach
</select>
<label>Quantity :</label>
<input type="text" name="quantity[]" value="" required>
</div>
</div>
and this is create.js that's included :
$(document).ready(function () {
console.log("Wele To create order Page");
var addItem = $('#addItem');
var orderMenuItems = $('#orderMenuItems');
$(addItem).click(function(e){
var newItem = '<div class="orderItem">';
newItem += '<label> Item : </label>';
newItem += '<select name="orderItem[]">';
newItem += '<option value="">Menu Items</option>';
newItem += "@foreach ($menuItems as $item)";
var itemID = "{!! $item->id !!}";
newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';
newItem += "@endforeach";
newItem += '</select>';
newItem += '<label>Quantity :</label>';
newItem += '<input type="text" name="quantity[]" value="" required>';
newItem += '</div>';
$(orderMenuItems).append(newItem);
});
});
The button works and adds new row, but there is a problem with the menu items in the new row :
I think the problem is here :
newItem += "@foreach ($menuItems as $item)";
var itemID = "{!! $item->id !!}";
newItem += '<option value="'+ itemID +'">'+ itemID +'</option>';
newItem += "@endforeach";
I tried to use :
@foreach ($menuItems as $item)
....
@endforeach
instead of :
newItem += "@foreach ($menuItems as $item)";
....
newItem += "@endforeach";
but it doesn't work.
How Can I fix it ??
Share Improve this question edited Dec 5, 2020 at 7:41 Rowayda Khayri asked Oct 13, 2017 at 12:28 Rowayda KhayriRowayda Khayri 4891 gold badge11 silver badges24 bronze badges 5- I think it's because you are mixing php and javascript so the {!! $item->id !!} isn't getting echoed by php, but is set as a string. – Mr. Greenwoodz Commented Oct 13, 2017 at 12:44
- So, what should I do to make it work ? @Mr.Greenwoodz – Rowayda Khayri Commented Oct 13, 2017 at 12:47
- Something like var itemID = '<?php echo $item->id ?>;'; – Mr. Greenwoodz Commented Oct 13, 2017 at 12:50
- I tried it but still having the same problem .. @Mr.Greenwoodz – Rowayda Khayri Commented Oct 13, 2017 at 13:04
- You need to generate the select in JS and append it with js. Don't use blade , it will be not piled. You will need ajax to get your value. – Dessauges Antoine Commented Oct 13, 2017 at 13:20
1 Answer
Reset to default 2I have no experience in laravel, but I'd guess that it simply doesn't feel responsible for js files.
I don't like this approach where you store the markup in two places. Once in the html, and once in a String in JS. And you have to keep these two places sync.
May I suggest a different approach:
in create.blade.php:
<div class="createOrderForm" id="orderMenuItems">
<div class="orderItem">
<label> Item : </label>
<select name="item[]" required>
<option value="">Menu Items</option>
@foreach ($menuItems as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
@endforeach
</select>
<label>Quantity :</label>
<input type="text" name="quantity[]" value="" required>
</div>
</div>
the template can be anywhere in the page, so why not store it right next to where it's used
and in create.js
$(document).ready(function () {
console.log("Wele To create order Page");
var $addItem = $('#addItem');
var $orderMenuItems = $('#orderMenuItems');
$addItem.click(function(){
var markup = $orderMenuItems.children('.orderItem')[0].outerHTML;
$orderMenuItems.append(markup);
});
});