I am trying to create a form where the names of the inputs are an array. I am going through an array sent from another view to get fields to show. in this example I want to show 3 fields.
$someResult = array('0','1','2');
$fields = array(0=>'fName',1=>'mName',2=>'lName');
#foreach($someResult as $k){
<td> {!! Form::text($fields[$k][],$someVal) !!}</td>
#endforeach
I tried simplifing it to straight php:
foreach ($someResult as $k){
echo "<tr><td><input type='text' name='".$fields[$k][]."' value='".$someVal."'></tD></tr>";
}
Either way, i get the error "Cannot use [] for reading"
How can i declare name argument as an array?
My goal is something like:
<td>
<input type='text' name='fName[]' value='someVal'>
</td>
<td>
<input type='text' name='mName[]' value='someVal'>
</td>
//etc.....
The square bracket should be used as a string in your dom.
#foreach ($someResult as $k)
<tr>
<td>
<input type="text" name="{{ $fields[$k] }}[]" value="{{ $someVal }}">
</td>
</tr>
#endforeach
Related
Am try to update the data in my table from input form using eloquent:
This the output of my code
So what i want is that when i click submit i want all the data to be update respective of its ID
My table looks like this
Here is my code in Blade
<form action="{{url('update/users')}}" method="post">
#csrf
<table>
<tbody>
<tr>
<th>ID</th>
<th>Names</th>
</tr>
#foreach($data as $users)
<tr>
<td><input type="" name="id" value="{{$users->id}}" ></td>
<td><input type="" name="name" value="{{$users->name}}" ></td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit">Submit</button>
</form>
in Controller
public function update(Request $request){
//code here
// $users = Users::
return back()->with('success','Successfully');
}
to update multiple resources in db you can accomplish this using:
first you must edit your form to send multiple data to server :
you must set type of inputs. the type of id input must be hidden becasue user can not edit it . this input will be used in controller to determine the user to update .
you can send a array parameter to server. to accomplish this you can
set the name attribute of input as array
<form action="{{url('update/users')}}" method="post">
#csrf
<table>
<tbody>
<tr>
<th>ID</th>
<th>Names</th>
</tr>
#foreach($data as $key=> $users)
<tr>
<td><input type="hidden" name="users[{{$key}}][id]" value="{{$users->id}}" ></td>
<td><input type="text" name="users[{{$key}}][name]" value="{{$users->name}}" ></td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit">Submit</button>
</form>
so the users in request is a array that each element is array with id and name key
public function update(Request $request){
foreach($request->input('users') as $user){
$id=$user['id'] ;
$name=$user['name'] ;
User::find($id)->update([
'name'=>$name
]) ;
}
return back()->with('success','Successfully');
}
I have a html table with check box on every row and when I click the checkboxes at a specific row, i am supposed to echo the input text using php. However, I can only get the input text for the first row. All the other rows give me a blank data. I dont understand whats wrong with my code
<?php
if(isset($_POST['submit']))
{
foreach($_POST['test'] as $key=>$value)
{
echo $_POST['tCode'][$key];
}
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div style="width:48%; margin-left:440px">
<table border="0px" align="center" class = "table" style="margin-right:530px;">
<form name="MainForm" method="post" action="" >
<tr>
<th style = "float:right">Tracking code:</th>
<th>
<td> <input type="textbox" name="tCode[]" id="1" ></td>
<td> <input type="checkbox" name="test[]" id="name" value ="1" />
</td>
</th>
</tr>
<tr>
<th style = "float:right" >Order Code: </th>
<th>
<td> <input type="textbox" name="tCode[]" id="2" ></td>
<td> <input type="checkbox" name="test[]" id="name" value ="2" />
</td>
</th>
<th>
</tr>
<tr>
<th style = "float:right" >product Code: </th>
<th>
<td> <input type="textbox" name="tCode[]" id="3" >
</td>
<td> <input type="checkbox" name="test[]" id="name1" value ="3"/></td>
</th>
<th>
</tr>
<td></td>
<td><input class="btn" type="submit" value="Submit" name = "submit" style="margin-left:50px" /></td>
When i tick the first checkbox, and type hello world in the first texbox, click submit, it is able to echo out "hello world", whereas for the other checkboxes, the inputted text is blank despite the textbox having value in it.
OK, there's lots of things going on here that are worth addressing. Keep in mind this comes from the spirit of wanting to help you become a better coder. Not all of it applies to PHP, but all of the feedback will help you be a better coder in general.
Do NOT use inline styles. They are frowned upon, they make the code messy / hard to diagnose, and they make maintenance very difficult.
DO use a code editor that will hilight issues for you (I'm a huge fan of PHPStorm). The code had all kinds of mismatched html tags that I have cleaned up below.
Do NOT use ids unnecessarily. On html inputs, there are only two purposes for using ids: (a) So you can address them with CSS styles, or (b) So you can access them easily with some javascript code. You were doing neither, so I stripped out all the ID's.
DO format your code with proper spacing and indenting. Formatting makes it far easier to read and troubleshoot.
DO put the PHP "key" inside the name of the inputs. This is one of the reasons you were having difficulties - the indexes did not match.
DO use defensive coding (checking if something isset, is ! empty, or array_key_exists) before accessing an array element. Note I used array_key_exists below - that's because other methods can still return FALSE, even if it was set (but there was no value).
The reason it didn't work is because checkboxes are only present in the $_POST superglobal if they are checked. Therefore, the indexes did not match like you expected them to. For example, checking ONLY the second box resulted in a $key of 1, NOT 2 as you would expect.
I've updated the inputs, as well as the PHP, to work.
The below code has been updated, and tested, and proven to work:
<?php
if( isset( $_POST[ 'submit' ] ) ) {
// this is for debugging / testing. Comment / remove as necessary
var_dump( $_POST );
// load ALL of the text inputs for convenient access in the loop
$codes = $_POST['tCode'];
// test first to be sure ANY checkboxes were checked, to prevent notices
if ( isset( $_POST[ 'test' ] ) ) {
// loop over all posted checkboxes
foreach( $_POST[ 'test' ] as $key => $on ) {
// isset or ! empty can return FALSE if present, but empty value
if ( array_key_exists( $key, $codes ) ) {
echo '<br>The input for row ' . $key . ' is: ' . $codes[ $key ];
}
}
}
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>
</title>
</head>
<body>
<form name="MainForm" method="post" action="" >
<table>
<tr>
<th>
Tracking code:
</th>
<td>
<input type="textbox" name="tCode[1]" >
</td>
<td>
<input type="checkbox" name="test[1]" />
</td>
</tr>
<tr>
<th style = "float:right" >
Order Code:
</th>
<td>
<input type="textbox" name="tCode[2]" >
</td>
<td>
<input type="checkbox" name="test[2]" />
</td>
</tr>
<tr>
<th>
product Code:
</th>
<td>
<input type="textbox" name="tCode[3]" >
</td>
<td>
<input type="checkbox" name="test[3]" />
</td>
</tr>
</table>
<input class="btn" type="submit" value="Submit" name="submit" />
</form>
</body>
</html>
I have a view page
$nameArray = array('name' => 'Formname', 'id' => 'Formname');
echo form_open_multipart('controller/file_upload_function',$nameArray);
<table id="companytable" >
<tr><td><input type="file" id="dummyfile" name="dummyfile"></td></tr>
<tr>
<td >
<label id="addlabel" style="cursor: pointer;">
<i class="fa fa-plus-circle" aria-hidden="true"></i></label>
</td>
<tr>
<td>
<input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company">
</td>
</tr>
<tr>
<td>
<input type="submit" id="btn_upload" name="btn_upload" value="Save">
</td>
</tr>
</table>
<?php echo form_close(); ?>
Using Jquery I need to append more 'companyname' fields according to user(On cliking 'add company' label).'companytable' is the id of the table.
$("#addlabel").click(function(){
$("#companytable").append('<tr><td><input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company"></td></tr>');
});
The problem is when I try to submit my view and try to get the array items 'companyname'.
$company_items = array();
$company_items = $this->input->post('companyname');
print_r($company_items);
I am able to get only the first item of the array that is already given the view page.The appended array item is lost.Can somebody help me, please?
Your HTML code works fine. The problem is from jquery or add button. if you are using html button tag for that, then you have to use preventDefault() in the jquery like
$('.add').click(function(e){
e.preventDefault();
$("#companytable").append('<tr><td><input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company"></td></tr>');
});
or you can use input while selecting type as button, for this you don't need any changes for your code.
I'm getting an error of
htmlspecialchars() expects parameter 1 to be string, object given.
I'm trying to print an array from session to blade.
view:
<input type="text" name="to" value="{{$mail}}">
controller:
public function view_send_email()
{
$data["_email_list"] = Tbl_press_release_email::get();
$data["sent_email"] = Request::input('sent_email');
$mail = Session::get('email');
return view("send_email", compact('data', 'mail'));
}
You should try this:
#foreach ($mail as $email)
<input type="text" name="to[]" value="{{$email}}">
#endforeach
Note: As you will have multiple values in $email you need to take array of input element as mentioned in above code (i.e name = "to[]")
Updated Answer
#foreach ($mail as $email)
#foreach ($mail as $emails)
<input type="text" name="to[]" value="{{$emails}}">
#endforeach
#endforeach
<input type="text" name="to" value="{{$mail}}">
To
<input type="text" name="to" value="{{print_r($mail)}}">
It seems like it's returning multiple values, so you have to loop through them to display all of them, use a foreach loop.
#foreach ($mail as $email)
<input type="text" name="to" value="{{$email}}">
#endforeach
If you want Form Model Binding
That's a different thing but the same concept, you can view the docs here.
EDIT: It looks like you want to store an array into an input, to do this you must add a [] at the end of the name of your input like this
<input type="text" name="to[]" value="{{$mail}}">
Then when they submit you simply go Input::get('to')[0] to display the first input.
Is there a way that I can post two values at the same time from a single field in a table but hide one from the user?
I would like the following form to post the values ID and reason_name when it is submitted but have the user only be able to see (and edit in the text box) the reason_name.
<form name="add_positioning" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1" class="autoTable_pos">
<tr>
<td>Positioning</td><td> </td></tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $user_id AND category = 'positioning' AND current = 1";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="text" name="reason[]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<?
}
?>
<tr>
<td>
<input type="text" name="reason[]" size="25"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Save" name="save_reasons"/>
</td>
</tr>
</table>
</form>
The form POST action so far (basic echo at the moment for my own sanity to check that it posts the values correctly, which it does...)
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $item) {
echo $item.'<br/>';
}
}
This table displays the values that are held in a database but enables the user to add more values by dynamically adding a new row (using JQUERY I haven't included) to the table when they type in an empty one at the bottom and also allows them to edit or delete existing values.
For each value posted I intend to check if the ID value is empty, if it is it means that it is a new value and enter a new record into the database, if it isn't update the existing record in the database with the corresponding ID. I don't have a problem writing that bit, I just can't think how to get the ID value posted as well as the reason_name while keeping ID hidden from the user.
Add the ID to the name attribute of the Text boxes of the reasons loaded from the DB. Leave the other Text boxes added using JQ without the ID.
E.g.
Text box which shows the existing reason loaded from the DB
<input type="text" name="reason[][ID]" size="25"/>
Text box added with JQ
<input type="text" name="reason[]" size="25"/>
Then once you submit the form you get you will get the following array.
array
'reason' =>
array
0 =>
array
14 => string 'VAL1' (length=4)
1 => string 'VAL2' (length=5)
By checking for an array in the each element in the "reason" array, you can differentiate two type of Text Boxes.
Here is the complete code I have tested.
<?PHP
//var_dump($_POST);
foreach($_POST['reason'] as $item=>$value)
{
if(is_array($value)){
foreach($value as $ID=>$reason)
{
echo "Existing Reason : ".$ID." - ".$reason."<br>";
}
}
else{
echo "New Reason : ".$item." - ".$value.'<br/>';
}
}
?>
<form action="" method="POST">
<input type="text" name="reason[][14]" size="25" value="aaaa"/>
<input type="text" name="reason[]" size="25" value="bbbbb"/>
<input name="" type="submit">
</form>
Assuming the id is at $row['reason_id'] I think you want:
$i = 0;
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="hidden" name="reason_id[<? echo $i; ?>]" size="25" value="<? echo $row['reason_id']; ?>"/>
<input type="text" name="reason[<? echo $i; ?>]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/></td></tr>
<? $i++ } ?>
This way you can later
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $key => $item) {
$id = $_POST['reason_id'][$key];
echo $id . " " . $item.'<br/>';
}
}