Request not showing all data in array - php

I am using laravel 5.1 and I am in the middle of the problem.
<script type="text/javascript">
var counter = 0;
$(document.body).on('click', ".popCategory", function () {
var appendcategory = "";
if (counter < 100) {
counter++;
appendcategory += '<input type="text" class="form-control" name="category[]" placeholder="Category" value="545454" />';
$('.category_append').append(appendcategory);
}
});
</script>
As shown in image I have appended the category with the help of jquery and when I submit the form I get the result as shown in below:
The problem is that I only get one value when I have submitted three value of category . you can see the inspect element below:
<form action="" role="form" method="POST" enctype="multipart/form-data">
<input type="number" class="form-control" name="current_amount" placeholder="Amount" id="current_amount" min="1"/></td>
<td><select name="cash_flow_status" class="form-control">
<option value="cashin">CashIn</option>
<option value="cashout">CashOut</option>
</select></td>
<td class="category_append">
<input type="text" class="form-control" name="category[]" placeholder="Category" value="545454"/>
</td>
<td>
<a href="javascript:void(0)" class="popCategory">
<i class="fa fa-plus"></i></a>
</td>
<td class="item_append"><input type="text" class="form-control" name="items[]" placeholder="Items"/></td>
<td>
<div name="add1" class="btn btn-success cashFlowAddPart" onClick="appendItems()" ><i class="fa fa-plus"></i></div></td>
<td colspan="2">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" name="submit" id="cashflowsubmit" value="Submit" class="btn btn-success"/>
</td>
</form>
Can someone help me?

Related

laravel submitting multiple rows of a form to mysql by creating duplicate rows

i have this form in larvel that will submit data to mysql database
every time i click Add Morenew row is created using script i want to submit all the rows at ones
<form action={{route('invetory.create')}} method="post">
#csrf
<div id="service">
<input type="text" name="item"/>
<input type="text" name="tax" />
<input name="price" type="text" />
<input name="selling_price" type="text" />
<input name="total_price" type="text" />
<input type="submit"/>
</div>
</form>
<a class="btn-floating btn-primary" id="addmore" onclick="duplicate()">Add More</a>
input fields duplicating script
<script>
function duplicate() {
var original = document.getElementById('service');
var rows = original.parentNode.rows;
var i = rows.length - 1;
var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplic" + (i); // there can only be one element with an ID
original.parentNode.insertBefore(clone, rows[i]);
}</script>
invetory.create route in web.php
`Route::post('/inventory', 'App\Http\Controllers\InventoryController#create')->name('invetory.create');`
funtion in InventoryController.php
public function create(Request $REQUEST){
Inventory::Create($REQUEST->all());
return redirect()->action([InventoryController::class, 'index']);
}
If I am not wrong, what I get from your question is that you want to insert multiple data in single query.
You have to give field name using array other wise it is difficult to differentiate multiple data using same key name. Array will differentiate your data using index value with same name.
Here is a simple example what you want to implement. According to this you can create your own format. Here I used table structure, if you want you can use div structure.
In Blade file:-
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="container">
<form action="{{route('invetory.create')}}" method="POST">
<table class="table table-bordered" id="dynamicTable">
<tr>
<th>item</th>
<th>tax</th>
<th>price</th>
<th>selling_price</th>
<th>total_price</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="invetory[0][item]" class="form-control" /></td>
<td><input type="text" name="invetory[0][tax]" class="form-control" /></td>
<td><input type="text" name="invetory[0][price]" class="form-control" /></td>
<td><input type="text" name="invetory[0][selling_price]" class="form-control" /></td>
<td><input type="text" name="invetory[0][total_price]" class="form-control" /></td>
</tr>
</table>
<button type="button" name="add" id="add" class="btn btn-success">Add More</button>
<button type="submit" class="btn btn-success">Save</button>
</form>
</div>
<script type="text/javascript">
var i = 0;
$("#add").click(function(){
++i;
$("#dynamicTable").append('<tr><td><input type="text" name="invetory['+i+'][item]" class="form-control" /></td><td><input type="text" name="invetory['+i+'][tax]" class="form-control" /></td><td><input type="text" name="invetory['+i+'][price]" class="form-control" /></td><td><input type="text" name="invetory['+i+'][selling_price]" class="form-control" /></td><td><input type="text" name="invetory['+i+'][total_price]" class="form-control" /></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>');
});
$(document).on('click', '.remove-tr', function(){
$(this).parents('tr').remove();
});
</script>
</body>
</html>
And in controller you have to do like this
public function create(Request $request){
foreach ($request->invetory as $key => $value) {
Inventory::create($value);
}
return redirect()->action([InventoryController::class, 'index']);
}
You may use
public function create(Request $REQUEST){
Inventory::firstOrCreate($REQUEST->all());
return redirect()->action([InventoryController::class, 'index']);
}
You may also check laravel docs

How to select perticular class in jquery from a dynamic table

I am trying to create a system to put sequence number of lessons, and i tried to make it unique as u can see that i take a variable $i but no luck . i am new in jquery. dont know how to use (this) in this situation. i am going to use ajax to save the data.
<?php
$chapter_id = $_GET['id'];
$lesson = mysqli_query($conn_course, "SELECT * FROM `lesson_list` WHERE `chapter_id` = '$chapter_id' order by `sequence` asc");
$i=0;
while ($info1 = mysqli_fetch_array($lesson)) {
$lesson_id = $info1['lesson_id'];
$lesson_info = mysqli_query($conn_course, "SELECT * FROM `lesson` WHERE `lesson_id` = '$lesson_id'");
$info = mysqli_fetch_array($lesson_info);
$i++;
?>
<tr>
<td>
<form>
<input type="hidden" class="lesson_id<?=$i?>" value="<?=$lesson_id?>">
<input type="hidden" class="sno" value="<?=$i?>">
<input type="hidden" id="chapter_id" value="<?=$chapter_id?>">
<input class="form-control sequence_no" style="width:27%;" type="number" value="<?= $info1['sequence'] ?>">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
<?= $info['lesson_name'] ?>
</button>
</a>
</td>
<td>
<?= strtoupper($info['code']) ?>
</td>
<td>
<?php if($info['video_url'] == 0){}else{ ?>
<a href="https://player.vimeo.com/video/<?= $info['video_url'] ?>" target="blank"><button type="button"
class="btn btn-outline-light btn-sm">Play Video</button></a>
<?php } ?>
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="<?=$lesson_id?>">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
<?php } ?>
myjquery is
$(document).ready(function () {
$('.sequence_no').on("change", function () {
var sequence_no = $('.sequence_no').val();
var s_no = $('.sno').val();
var chapter_id = $('#chapter_id').val();
var lesson_id = $('.lesson_id' + s_no).val();
alert(s_no);
})
})
alert is showing only the first table row s_no. how to select the lesson id of particular tr
You can simply get closest tr using $(this).closest("tr") and then use .find to get required classes values .
Demo Code :
$('.sequence_no').on("change", function() {
var selector = $(this).closest("tr")
var sequence_no = $(this).val();
var s_no = selector.find('.sno').val();
var chapter_id = selector.find('.chapter_id').val();
var lesson_id = selector.find('input[class*=lesson_id]').val();
console.log(s_no, lesson_id);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table>
<tr>
<td>
<form>
<!--added class-->
<input type="hidden" class="lesson_id<?=$i?>" value="22">
<input type="hidden" class="sno" value="1">
<input type="hidden" class="chapter_id" value="1">
<input class="form-control sequence_no" style="width:27%;" type="number" value="2">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
Smething
</button>
</a>
</td>
<td>
Abc
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="1">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
<tr>
<td>
<form>
<input type="hidden" class="lesson_id<?=$i?>" value="26">
<input type="hidden" class="sno" value="2">
<input type="hidden" class="chapter_id" value="2">
<input class="form-control sequence_no" style="width:27%;" type="number" value="2">
</form>
</td>
<td>
<a href="index.php?p=lesson-page&id=<?= $info['lesson_id'] ?>">
<button type="button" class="btn btn-outline-light btn-sm">
Smething
</button>
</a>
</td>
<td>
Abc2
</td>
<td>
<form class="" action="../edit/delete.php" method="get" style="float:left; margin-right:5px;">
<input type="hidden" name="chapter_id" value="<?=$_GET['id']?>">
<input type="hidden" name="lesson_id" value="2">
<input type="submit" class="btn btn-outline-light btn-sm" name="delete_lesson" onclick="return confirm('Do you want to delete it')" value="Delete">
</form>
<a href="index.php?p=lesson-create&task=edit&id=<?= $info['lesson_id'] ?>&page=chapter-page&chapter_id=<?=$chapter_id?>">
<button type="button" class="btn btn-outline-light btn-sm">Edit
</button>
</a>
</td>
</tr>
</table>

Append table using jQuery with php

Hi every one I'm trying to append a table within a form. But i'm having an error message in console: "Uncaught SyntaxError: missing ) after argument list". When I click the button to add rows it keeps directing me to php script. I Tried to fix it but no luck anyway sorry for bad english, here are the codes.
$('#more_row').append('<tr id="row'+i+'"><td><?php echo "<select name='alpha_num[]' class='form-control' id='item'>"; echo"<option value=''selected disabled>Select Material</option>";while($row = mysqli_fetch_row($result1)){echo"<option value=$row[0]>$row[1]$row[0]</option>";}echo"</select>";?></td><td><input type="text" name="qty[]" class="form-control" id="quantity" placeholder="Qty"></td><td><input type="text" name="uom[]" class="form-control" id="um" placeholder="UOM"></td><td><select name="status[]" class="form-control" id="stat"><option value="approved">approved</option><option value="return">return</option></select></td><td><button id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
<div class="panel-body">
<form method="post" action="material_receive_report_process.php">
<div class="row">
<div class="col">
<label for="dr">PO.NO</label>
<input type="text" name="dr_no" class="form-control" id="dr">
</div>
<div class="col">
<label for="dr">DR.NO</label>
<input type="text" name="dr_no" class="form-control" id="dr">
</div>
<div class="col">
<label for="date">Date</label>
<input type="text" name="date" class="form-control" id="date">
</div>
</div>
<table id="more_row">
<tr>
<td>
<?php echo "<select name='alpha_num[]' class='form-control' id='item'>";
echo"<option value=''selected disabled>Select Material</option>";
while($row = mysqli_fetch_row($result1)){
echo"<option value=$row[0]>$row[1]$row[0]</option>";
}
echo"</select>";
?>
</td>
<td>
<input type="text" name="qty[]" class="form-control" id="quantity" placeholder="Qty">
</td>
<td>
<input type="text" name="uom[]" class="form-control" id="um" placeholder="UOM">
</td>
<td>
<select name="status[]" class="form-control" id="stat">
<option value="approved">approved</option>
<option value="return">return</option>
</select>
</td>
<td><button id="add1" class="btn btn-info">Add</button></td>
</tr>
</table>
<br>
<p>Condition:
<input type="checkbox" name="condition[]" value="damage_visible">Damage visible
<input type="checkbox" name="condition[]" value="good_condition">Good conditon
<input type="checkbox" name="condition[]" value="full_qty">Full qty
<input type="checkbox" name="condition[]" value="partial_qty">Partial qty</p>
<div class="form-group">
<label for="supp">Supplier</label>
<input type="text" name="supplier" class="form-control" id="supp">
</div>
<div class="form-group">
<label for="remark">Remarks</label>
<textarea name="remark" class="form-control" id="remark"></textarea></p>
<input type="submit" name="save" class="btn btn-success" value="Save">
</div>
</form>
<br><br>
</div>
</div>
The 'add row' button submits the form because the default value for the type attribute of button elements is "submit". My guess is that the syntax error is due to the mess of single quotes, double quotes, and line breaks that PHP is dumping in your Javascript. Look at the generated code your browser gets. That's what needs to be correctly formed.
The error is most likely due to conflict between Php and JavaScript strings. Consider escaping i.e. \' and using different one for the two say JavaScript use double quotes " while Php use single quotes '

in html post to php form: when button type = button, how to pass value?

You're setting up some attribute values for an object, such as colour, size, weight etc in a form. If using buttons to specify these values before wishing to post all the information to a php page for further processing - how do you get the values passed to php, if the buttons are not in themselves submitting the form?
For example:
<form action="processgivenvalues.php" method="post">
choose colour: <button type="button" name="colour" value="green"></button>
<button type="button" name="colour" value="blue"></button>
choose size: <button type="button" name="size" value="big"></button>
<button type="button" name="size" value="small"></button>
<input type="submit">
and on php page:
<?php
echo You specified:
echo $size;
echo $frame
?>
Many thanks.
The point of a type="button" is to be a thing you can hook JavaScript up to. It isn't designed to send values to the server.
Submit buttons are, but you want to let the user pick from multiple sets rather then just one, so you can't use those either.
While you use buttons with JavaScript that generates/updates hidden inputs with the values you want, you really should just use radio buttons instead. They are designed to let users pick one item from a group.
<form action="processgivenvalues.php" method="post">
<fieldset>
<legend>Colour</legend>
<label>
<input type="radio" name="colour" value="green"> Green
</label>
<label>
<input type="radio" name="colour" value="blue"> Blue
</label>
</fieldset>
<fieldset>
<legend>Size</legend>
<label>
<input type="radio" name="size" value="big"> Big
</label>
<label>
<input type="radio" name="size" value="small"> Small
</label>
</fieldset>
<input type="submit">
</form>
You could use jQuery for this.
html:
choose colour: <button type="button" class="colour" value="green">Green</button>
<button type="button" class="colour" value="blue">Blue</button>
choose size: <button type="button" class="size" value="big">Big</button>
<button type="button" class="size" value="small">Small</button>
<button id='submit' type="button">Submit</button>
jQuery:
You make the form on the run
$(document).ready(function(){
window.size = '';
window.colour = '';
$(".colour").click(function() {
window.colour = $(this).val();
});
$(".size").click(function() {
window.size = $(this).val();
});
$("#submit").click(function() {
if(window.size == '' || window.colour == ''){
return alert('Choose colour and Size');
}
var form = $('<form></form>');
$(form).hide().attr('method','post').attr('action',"processgivenvalues.php");
var input1 = $('<input type="hidden" />').attr('name',"size").val(window.size);
var input2 = $('<input type="hidden" />').attr('name',"colour").val(window.color);
$(form).append(input1);
$(form).append(input2);
$(form).appendTo('body').submit();
});
});
jsFiddle
This can also be done with only javaScript.
As Quentin stated, buttons are not useful for selecting between options, but you comment that you want something customizable with an image...
Option 1: Use <label for="id"></label> with a radio button in a table or list.
<table>
<tbody>
<tr>
<td style="background-color: green;">
<label for="green">
<input name="colour" value="green" id="green" type="radio">
</label>
</td>
</tr>
<tr>
<td style="background-color: blue;">
<label for="blue">
<input name="colour" value="blue" id="blue" type="radio">
</label>
</td>
</tr>
<tr>
<td>
<label for="big">
<input name="size" value="big" id="big" type="radio">
<big>Big</big>
</label>
</td>
</tr>
<tr>
<td>
<label for="small">
<input name="size" value="small" id="small" type="radio">
<small>Small</small>
</label>
</td>
</tr>
</tbody>
</table>
Option2: Use droplists with styling:
<select name="colour2">
<option value="0">Select a color</option>
<option value="green" style="background: green;"> </option>
<option value="blue" style="background: blue;"> </option>
</select>
<br>
<select name="size2">
<option value="0">Select a size</option>
<option value="big" style="font-size: 16px;">Big</option>
<option value="small" style="font-size: 12px;">Small</option>
</select>
Here's a demo fiddle I created (just signed up)

PHP Form not posting all fields

I have a strange issue that I cannot get my head around.
I have the following form and when I click submit I do not GET or POST all fields in the form. Only two fields are being picked up being - isnew=true action=object &submit=Start
<form name="newOffer" action="/auth/dashboard" method="post">
<td><?php echo form_hidden('isnew', 'true');?><?php echo form_hidden('action', 'object');?><input type="text" id="newOfferItem" placeholder="Offer Free Item" class="input-xlarge"></td>
<td><input type="text" id="newOfferText" placeholder="Offer Description" class="input-xlarge" rel="tooltip" title="Description How to get free item"></td>
<td><input type="text" id="newOfferFreeOn" placeholder="Stamps for free item" class="input-xlarge" rel="tooltip" title="Number only. Ex. 5"></td>
<td><span class="label label-danger">Inactive</span></td>
<td><?php $attributes = 'class = "btn btn-success"'; echo form_submit('submit', 'Start', $attributes);?></td>
</form>
You need to add NAME attributes for each INPUT element - either instead of or in addition to the ID attributes.
e.g.
<form name="newOffer" action="/auth/dashboard" method="post">
<td>
<?php echo form_hidden('isnew', 'true');?>
<?php echo form_hidden('action', 'object');?>
<input type="text" NAME="newOfferItem" id="newOfferItem" placeholder="Offer Free Item" class="input-xlarge">
</td>
<td>
<input type="text" NAME="newOfferText" id="newOfferText" placeholder="Offer Description" class="input-xlarge" rel="tooltip" title="Description How to get free item">
</td>
<td>
<input type="text" NAME="newOfferFreeOn" id="newOfferFreeOn" placeholder="Stamps for free item" class="input-xlarge" rel="tooltip" title="Number only. Ex. 5">
</td>
<td>
<span class="label label-danger">Inactive</span>
</td>
<td>
<?php $attributes = 'class = "btn btn-success"'; echo form_submit('submit', 'Start', $attributes);?>
</td>
</form>
Always add a name attribute in input element.
Here is an example for you..
index.html
<form method="POST" action="process.php">
<input type="text" name="username" value="">
<input type="password" name="password" value="">
<input type="submit" name="submit" value="login">
</form>
process.php
<?php
// checking if submit is pressed
if (isset($_POST['submit'])) {
// assigning posted values
$username = $_POST['username'];
$password = $_POST['password'];
// testing
echo $username . "<br>";
echo $password;
}
?>

Categories