I'm trying to set a max attribute based on the retrieved value in my database but it doesn't seem to work for me.
These is the codes that I tried
//
<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="<?=$data->store_quantity?>" required>
//
<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="{{$data->store_quantity}}" required>
I also tried to retrieve it as value="{{$data->store_quantity}}" to see if I'm able to retrieve the value in my database and it does. Furthermore there data types in my database are int
but if I do it manually like this, it works just fine
<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="5" required>
this is also how I retrieve it in my database in my controller
public function products($encryption_id){
$encrypt_id = Crypt::decrypt($encryption_id);
$data=product::where('id',$encrypt_id)->first();
return view("admin.products",compact("data"));
}
What should I do?
{{$data->store_quantity}} may not be an integer or number or may be smaller than 0.
Try this:
#if(is_int($data->store_quantity) && $data->store_quantity > 0)
<input type="number" name="entered_qty" placeholder="Enter Quantity" min="1" max="{{$data->store_quantity}}" required>
#else
// The data ur ($data->store_quantity) may not be an integer or it's smaller than 0...
<p>"Error: Data received is not an integer or number or its smaller than 0"</p>
#endif
You can validate the user using the dropdown as shown below.
<select name="entered_qty">
<option value="0">Select Quantity</option>
#if(empty($data->store_quantity))
#for ($i = 1; $i < $data->store_quantity; $i++)
<option value="{{$i}}">{{$i}}</option>
#endfor
#endif
</select>
Related
I have a form which have input field type number.
So, when i try to edit that form it shows all the value from the database.
But in Input type field it doesn't show any value . Even if there is value when i inspect but it doesnt show in browser.
<input type="number" value="#if(!empty($salary->salary)){{$salary->salary}} #endif" name="salary" class="form-control" id="inputsalary" placeholder="salary">
value inside input type number it doesn't show in browser
Try this:
<input type="number" value="{{ $salary->salary ? $salary->salary : ''}}" name="salary" class="form-control" id="inputsalary" placeholder="salary">
Hope it helps.
I would like to retrieve listed form parameters in next page by laravel.
The following code is just example for explain simply.
<form>
<input type="text" name="cat_a" value="Category A">
<input type="text" name="cat_b[0]" value="Category B0">
<input type="text" name="cat_b[1]" value="Category B1">
<input type="text" name="cat_b[2]" value="Category B2">
</form>
I can get cat_a on next page from parameter Request $request like $request->cat_a.
However I have no idea how to get cat_bs.
Please give me advice.
Thanks.
remove numbers in 'name' of input tag,
<form>
<input type="text" name="cat_a" value="Category A">
<input type="text" name="cat_b[]" value="Category B0">
<input type="text" name="cat_b[]" value="Category B1">
<input type="text" name="cat_b[]" value="Category B2">
</form>
get cat_b values and get each value by foreach.
$catsB = $request->cat_b;
foreach($catsB as $catB) {
// do something
}
I hope be useful.
Hope it works...
$cat_b_array = $request->cat_b;
foreach($cat_b_array as $cat_b_value) {
echo $cat_b_value;
}
You can get your list of values with this code
$cat_b_list = $request->input('cat_b.*')
Also you can get specific value by replacing * by array key
I'm trying to add or multiply input values to come to two total, this was working without trying to use a for loop. My intention was to match the selectors with the various iterations of i that are at the end of each string. I have a fiddle example here : https://jsfiddle.net/shiataz12/Lo1yek2g/69/
I've tried to copy and paste the function while manually inputting names for each iteration, however it doesnt quite work either as only the first two and last two checkbox do anything if clicked.
HTML :
<--- HTML SECTION --->>
// obtained from $nosday = $_POST['nosday'];
<br>
// value is normally $nosday
<br>
<input type="text" value="5" id="nosday"><br><br>
// obtained from $count = mysqli_num_rows($result);
<br>
// value is normally $count;
<br>
<input type="text" value="2" id="countrows"><br><br>
<label for="checkbox1">Standard</label>
<<----Assume checkbox --> id --> is $qri and $qri = "qr"."$i";
<input type="checkbox" name="checkbox1" class="quip" value="125" id="qr1" checked><br><br>
<label for="checkbox1">Equipped</label>
<input type="checkbox" name="checkbox1" class="quip" value="225" id="qr1"><br><br>
<label for="checkbox3">GPS</label>
<input type="checkbox" name="checkbox3" value="20" id="qr1"><br><br>
<label for="checkbox4">booster</label>
<input type="checkbox" name="checkbox4" value="20" id="qr1"><br><br>
<label for="checkbox5">One tent</label>
<input type="checkbox" name="checkbox5" class="tents" value="60" id="qr1" checked><br><br>
<label for="checkbox6">Two tents :</label>
<input type="checkbox" name="checkbox6" class="tents" value="80" id="qr1"><br><br>
// assume id="#dailytotal" where dailytotal = "dailytotal"."$i";
<br>
Daily :<span id="dailytotal1"> </span><br><br>
// Assume id = "$lengthtotal" where $lengthtotal = "lengthtotal"."$i";
<br>
Total :<span id="lengthtotal1"> </span><br><br>
// Hidden element for daily rate to use in $_POST, shown for now
<br>
<input tyep="text" id="dailytot1" name="pricef1" value="">
<br>
// Hidden element for length rate to use in $_POST, shown for now
<input tyep="text" id="lengthtot" name="pricef2" value="">
<input type="text" value="2" id="countrows"><br><br>
<label for="checkbox1">Standard</label>
<input type="checkbox" name="checkbox1" class="quip" value="125" id="qr2" checked><br><br>
<label for="checkbox1">Equipped</label>
<input type="checkbox" name="checkbox1" class="quip" value="225" id="qr2"><br><br>
<label for="checkbox3">GPS</label>
<input type="checkbox" name="checkbox3" value="20" id="qr2"><br><br>
<label for="checkbox4">booster</label>
<input type="checkbox" name="checkbox4" value="20" id="qr2"><br><br>
<label for="checkbox5">One tent</label>
<input type="checkbox" name="checkbox5" class="tents" value="60" id="qr2" checked><br><br>
<label for="checkbox6">Two tents :</label>
<input type="checkbox" name="checkbox6" class="tents" value="80" id="qr2"><br><br>
Daily :<span id="dailytotal2"> </span><br><br>
Total :<span id="lengthtotal2"> </span><br><br>
// Hidden element for daily rate to use in $_POST, shown for now
<br>
<input tyep="text" id="dailytot2" name="pricef1" value="">
<br>
// Hidden element for length rate to use in $_POST, shown for now
<input tyep="text" id="lengthtot2" name="pricef2" value="">
$(function() {
$("input.quip").change(function() {
$("input.quip").not(this).prop("checked", false);
});
$("input.tent").change(function() {
$("input.tent").not(this).prop("checked", false);
});
$(function() {
//get the values of the selected options
var counter = ("#countrows").val();
var i = 0;
for (i = 1; i <= counter; i++){
let v = $.map($('#qr' + i).is("checked"), function(i) {
return parseFloat($(i).val());
});
let s = v.reduce((a, b) => a + b); //sum them up to a daily total
console.log(v);
$('#dailytotal' + i).text('R' + s + '/day');
$('#lengthtotal' + i).text('R' + s * parseFloat($("#nosday").val()) + '/day');
$('#dailytot' + i).val(s);
$('#lengthtot' + i).val(s * parseFloat($("#nosday").val()));
}
});
});
The example provided assumes that there are two rows of a database and so i = 2 so id's should change to qr2 for i=2. I expected this to work by using the number of rows as a base to use a for in Jquery, limited to the number of rows so that each section can act independently, however they continue to react to the function together and do not show any values.
I thought perhaps if i could get it working for now and copy/paste code with individual id's that'd give me time to work on a better solution however it doesn't work as i'd expected. with map would it be better to declare an array prior and can i use the same id across inputs as an array? It makes calculation easier, although i guess i could use the name selector.
Hopefully someone can guide me to the correct answer, this is driving me up the wall.
Trimmed it down a little. There are other issues. The "Labels for" are suppose to refer to the id of the labelled element. The "Standard" and "Equipped" options are really radio buttons, not checkboxes, since presumably you can have one of the others. I renamed some of the classes and id's, although you may not need all of those. I am presuming that maybe you are generating the HTML dynamically and just add the value of "i" for a suffix, depending upon how many row you have. I actually just made changes to the first row and rewrote the JS to give you a total for the items form the values in the input elements for that row. Not sure exactly how you are calculating those totals, since $("#multiplier") I do not think is even in your code. You will have to look that over and see what you want, but the totals look like it is working.
<--- HTML SECTION --->>
// obtained from $nosday = $_POST['nosday'];
<br>
// value is normally $nosday
<br>
<input type="hidden" value = "1.5" id = "multiplier">
<input type="text" value="5" id="nosday"><br><br>
// obtained from $count = mysqli_num_rows($result);
<br>
// value is normally $count;
<br>
<input type="hidden" value="2" id="countrows"><br><br>
// Assume checkbox is $qri and $qri = "qr"."$i";<br>
<label for="standard1">Standard</label>
<input type="radio" name="equipment" id ="standard1" class="quip qr1" value="125" checked><br><br>
<label for="equipped1">Equipped</label>
<input type="radio" name="equipment" id ="equipped1" class="quip qr1" value="225" ><br><br>
<label for="gps1">GPS</label>
<input type="checkbox" name="gps" id ="gps1" value="20" class ="qr1"><br><br>
<label for="booster1">booster</label>
<input type="checkbox" name="booster" id ="booster1" value="20" class ="qr1"><br><br>
<label for="tents1">One tent</label>
<input type="checkbox" name="tents" id ="tents1" class="tents qr1" value="60" checked><br><br>
<label for="twotents1">Two tents :</label>
<input type="checkbox" name="twotents" id ="twotents1" class="tent qr1" value="80"><br><br>
// assume id="#dailytotal" where dailytotal = "dailytotal"."$i";
<br>
Daily :<span id="dailytotal1"> </span><br><br>
// Assume id = "$lengthtotal" where $lengthtotal = "lengthtotal"."$i";
<br>
Total :<span id="lengthtotal1"> </span><br><br>
// Hidden element for daily rate to use in $_POST, shown for now
<br>
<input tyep="text" id="dailytot1" name="pricef1" value="">
<br>
The JS probably needs to be tweaked since I do not know how you are generating the HTML. As it is, it should calculate the totals for all "i" rows on load, and it would not be hard to add something to have it calculate the total for the "ith" row. That should give you something to work with.
function Calc() {
//get the values of the selected options
var counter = $("#countrows").val();
let totals = [];
for (i = 1; i <= counter; i++) {
totals[i] = 0;
$.each($('.qr' + i + ':checked'), function() {
totals[i] += parseInt($(this).val());
});
console.log(totals[i]);
$('#dailytotal' + i).text('R' + totals[i] + '/day');
$('#lengthtotal' + i).text('R' + totals[i] * parseFloat($("#multiplier").val()) + '/day');
$('#dailytot' + i).val(totals[i]);
$('#lengthtot' + i).val(totals[i] * parseFloat($("#multiplier").val()));
}
}
Calc();
$("[class*=qr]").on("change", function(e) {
e.preventDefault();
Calc();
});
"[class*=qr]" isn't the best really because *=qr is not that specific really, depending upon your markup. Also, not clear that you are using form tags anywhere either.
I have the jQuery below and it works, when a value in the price input is inserted, for example "3" it adds "3,00" but then there is a validation error in the controller saying that the price is an integer. Do you know how to correct the issue? That is, maitain the price as an integer but dont have the validation errror.
PostController:
$prod = Product::create([
'price' => $request->price,
]);
input:
<div class="form-group col-md-6">
<label for="price">Price></label>
<input type="number" min="1" step="any" required class="form-control" value="{{ old('price', '0.00€') }}" name="price" id="price"/>
</div>
jQuery:
document.getElementById("price").onblur =function (){
this.value = parseFloat(this.value.replace(/,|\€/g, ""))
.toFixed(2)
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
If you'd like to send a users raw input data to the backend while at the same time displaying their data as formatted on the frontend (in the same input box), you could use a hidden form field which you set once the user has inputed a value.
<div class="form-group col-md-6">
<label for="price">Price></label>
<input type="hidden" name="price" />
<input type="number" min="1" step="any" required class="form-control" value="{{ old('price', '0.00€') }}" name="show-price" id="price"/>
</div>
Then when your user leaves the input field, you can format the input while setting the hidden input to it's raw value pre-formatting:
document.getElementById("show-price").onblur =function (){
var $price = document.getElementById("price");
$price.value = this.value;
this.value = parseFloat(this.value.replace(/,|\€/g, ""))
.toFixed(2)
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
I'm not exactly sure what the input is coming to you as on #show-price, so you might need to format it a little to trim it to a bare integer, but the idea is that before you perform the visual formatting you set your hidden form fields value to that raw data.
I have a sort of complex php,html script that I am using to create an interactive design. I am using a repeat table and i am basically cloning the contents of a particular table row and appending it to the end of the table. When I select a certain option in the select menu, the corresponding textfields get updated with the correct values. However cloned rows misbehave
What I'm asking is if there is a way to make this fields also change values. Here is the various code
Code for adding/cloning a row. Please note the id details is the id of a table row
$("#addrow1").click(function(){
//alert("It works");
//$('#description tr:last').after('<tr>...</tr>');
$('#details').clone().appendTo('#dailyexpense');
});
Code for changing the textfield and textbox values
$("#cities").live('change',function(){
var cityrates = $("#cities :selected").val();
var brkfstrates = (0.2 * cityrates).toFixed(1);
$("#brkfasttxt").val(brkfstrates);
$("#brkfastchk").val(brkfstrates);
var lunchrates = (0.3 * cityrates).toFixed(1);
$("#lunchtxt").val(lunchrates);
$("#lunchchk").val(lunchrates);
var dinnerrates = (0.3 * cityrates).toFixed(1);
$("#dinnertxt").val(dinnerrates);
$("#dinnerchk").val(dinnerrates);
var incdntlrates = (0.2 * cityrates).toFixed(1);
$("#incidentltxt").val(incdntlrates);
$("#incidentlchk").val(incdntlrates);
});
My table row code for the one that gets loaded by the browser
<tr id="details">
<td><label for="date"></label>
<input style="background-color:#CCC;" type="text" name="date" id="date" /></td>
<td><label for="cities"></label>
<select name="cities" id="cities" style="background-color:#CCC; width:220px;">
<?php
foreach($country as $makassi1){
$selectcities = "SELECT City, Country, Rate FROM perdiem_rates WHERE Country = '$makassi1'";
$checkcity = mysql_query($selectcities)or die(mysql_error());
$countcities = mysql_num_rows($checkcity);
while($row=mysql_fetch_assoc($checkcity))
{
$countries = ($row['Country']);
$names =($row['City']) ;
$rate =($row['Rate']) ;
$ratess=$countries."-".$names
?>
<option id="cityoptrates" value="<?php echo $rate; ?>">
<?php echo $ratess; ?>
</option>
<?php
}
}
?>
</select></td>
<td><input name="brkfastchk" type="checkbox" class="chkbox" id="brkfastchk" />
<label for="brkfasttxt"></label>
<input style="background-color:#CCC;" name="brkfasttxt" type="text" id="brkfasttxt" size="5" readonly="readonly" />
<label for="brkfastchk"></label></td>
<td><input type="checkbox" name="lunchchk" id="lunchchk" class="chkbox" />
<label for="lunchtxt"></label>
<input style="background-color:#CCC;" name="lunchtxt" type="text" id="lunchtxt" size="5" readonly="readonly" />
<label for="lunchchk"></label></td>
<td><input type="checkbox" name="dinnerchk" id="dinnerchk" class="chkbox" />
<label for="dinnertxt"></label>
<input style="background-color:#CCC;" name="dinnertxt" type="text" id="dinnertxt" size="5" readonly="readonly" />
<label for="dinnerchk"></label></td>
<td><input type="checkbox" name="incidentlchk" id="incidentlchk" class="chkbox" />
<label for="incidentltxt"></label>
<input style="background-color:#CCC;" name="incidentltxt" type="text" id="incidentltxt" size="5" readonly="readonly" />
<label for="incdntchk"></label></td>
<td colspan="2"><label for="daily_totals"></label>
<input style="background-color:#CCC;" name="daily_totals" type="text" id="daily_totals" size="5" /></td>
</tr>
As per the above my textfield value changing code works perfectly with the first initial row. The cloned rows don't work. The only one which works on the cloned rows is the select menu for selecting the cities. Help needed. Open to suggestions and improvements.
link to actual code in jsfiddle http://jsfiddle.net/NAafu/10/
In your case maybe you should also clone the events:
$('#details').clone(true).appendTo('#dailyexpense');
as stated in the documentation of clone()
EDIT - The problem perhaps is that you are using an id selector ($("#cities").live) which returns only one element. Ids should be unique on the page you should use a class instead