I want to make a form using form repeater. As a result of my efforts somehow I could not register in the database.
html code:
<div class="m-portlet__body" id="myRadioGroup">
<div id="m_repeater_1">
<div class="form-group m-form__group row" id="m_repeater_1">
<div data-repeater-list="" class="col-md-12">
<div data-repeater-item class="form-group m-form__group row align-items-center">
<div class="col-md-12 m-form__group-sub">
<label class="form-control-label">Car plate</label>
<div class="input-group">
<input type="text" class="form-control" name="plate" placeholder="34 LAA 34" maxlength="10">
<div class="input-group-append">
<button data-repeater-delete="" class="btn btn-primary" type="button"><i class="la la-trash-o"></i></button>
</div>
</div>
</div>
</div>
</div>
<div class="m-form__group form-group row" style="padding-left: 48px; margin-top: -2rem;">
<div class="col-md-12">
<div data-repeater-create="" class="btn btn btn-sm btn-brand m-btn m-btn--icon m-btn--wide">
<span>
<i class="la la-plus"></i>
<span>add plate</span>
</span>
</div>
</div>
</div>
</div>
</div>
chrome looks like this in developer tools:
How should my database registration file be?
$plate = $_POST['plate'];
$sql = $db->prepare('INSERT INTO orders (plate) VALUES (?)');
$save = $sql->execute(array(
$plate,
));
The problem is in the html code, input name should be plate[1]. One possible cause is that you have id="m_repeater_1" declared twice. Make sure you are setting the template correctly first, and then you can search in all post data by using print_r($_POST); (with your current code no POST is being sent).
<input type="number" placeholder="Amount" name="repeat[0][amount]" class="form-control">
public function add()
{
$locations = $_POST['repeat'];
foreach ($locations as $key => $subValue) {
echo $subValue['amount'];
}
Related
in my where i use smarty to connect everything tpl file i have a form
<form method="post">
<div class="form-group"><label for="address"><strong>Address</strong></label><input class="border rounded-pill form-control" type="text" value='{$address}' name="address"></div>
<div class="form-row">
<div class="col">
<div class="form-group"><label for="city"><strong>City</strong></label><input class="border rounded-pill form-control" type="text" value='{$city}' name="city"></div>
</div>
<div class="col">
<div class="form-group"><label for="country"><strong>Country</strong></label><input class="border rounded-pill form-control" type="text" value='{$country}' placeholder="The Netherlands" name="country"></div>
</div>
</div>
<div class="form-group"><label for="phonenumber"><strong>Phone Number</strong></label><input class="border rounded-pill form-control" type="text" value='{$phone}' placeholder="+1 (0)1 234 56 789" name="phonenumber"></div>
</div>
<div class="card-body" style="background: #f5f5f5;">
<button class="btn btn-primary btn-sm" type="submit" name="contactsend">Save Settings</button>
</div>
</div>
</form>
But if i try to contact it in my php file
if(isset($_POST['contactsend'])) {
$inaddress = mysqli_real_escape_string($db, $_POST['address']);
$incity = mysqli_real_escape_string($db, $_POST['city']);
$insert = $db->query("UPDATE accounts SET address = ".$inaddress.", city = ". $incity ." WHERE id = ".$id_user."");
}
it wont get the contactsend button if i press it, so it also wont update the tables. can someone please help me?
that form is posting to it's own page ... is the php script in the same file as the render script? If not you need to set the action="" attribute of the form to the php script url
using for each loop I have to retrieve data however when adding new record data-repeater-create is taking default value as the last record of data even the element id is different.
<div data-repeater-list="car">
<?php foreach ($user_portfolio as $p_details) {
$p_title = $p_details['portfolio_title'];
$p_desc=$p_details['portfolio_desc'];
$p_expert = $p_details['portfolio_expert_id'];
$p_id = $p_details['portfolio_id'];
?>
<div data-repeater-item>
<div class="form row">
<div class="form-group mb-1 col-sm-12 col-md-12">
<label for="email-addr">Title</label>
<br>
<input type="text" name="port_id" value="<?php if(!empty($p_id)) {echo $p_id; } else { echo "";}?>">
<input type="text" class="form-control" id="portfolio_title<?php echo $p_id;?>" placeholder="Title " name="portfolio_title" value="<?php if(!empty($p_title)) {echo $p_title;} else {echo ""; }?>">
</div>```
below using data-repeater-create i am adding empty field however its taking last record as default value and I able to see in view source
``` <?php } //end of foreach?>
</div>
<div class="form-group overflow-hidden">
<div class="col-12">
<button data-repeater-create class="btn btn-theme" type="button">
<i class="ft-plus"></i> Add
</button>
<button type="submit" class="btn btn-theme">
<i class="la la-check-square-o"></i> Save
</button>
</div>
</div>
</form>```
I have a page with a form that has dynamic inputs, where the user can add and "remove" lines. (By "removes," I mean that it removes from the front end but doesn't actually remove from the database.) The functionality works, as far as updating existing rows and adding new rows. The problem I'm having is with deleting rows.
I'd like to have a modal popup that confirms that they want to delete the row before it deletes it. My first thought was to loop through the existing rows and create a corresponding modal per row with a form, but I'm stuck on setting up the controller to recognize which form/row it's looking to delete - so to recap there's going to be one form with dynamic fields (this is the one that works right now), and there's going to be multiple dynamic forms for deleting rows.
Here's my HTML:
<form action="{{route('preliminary-children.updateChildren')}}" method="POST">
#csrf
<!-- Content Row -->
<div class="row" id="childInfo">
<!-- About Process -->
<div class="col-xl-12 col-lg-12">
<div class="card shadow mb-4">
<!-- Card Header - Dropdown -->
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-secondary">Family Information - Children</h6>
</div>
<div id="dynamic_field">
#foreach($children as $i => $child)
<div class="card-body" id="row{{$i+1}}">
#if ($i != 0)
<hr/>
<div class="text-right px-2 my-2 child-{{$i+1}}">
<a href="#" class="text-decoration-none" data-toggle="modal" data-target="#remove{{$i+1}}">
<button type="button" class="px-0 btn btn-circle btn-danger">
<i class="fal fa-times"></i>
</button>
</a>
</div>
<div class="modal fade" id="remove{{$i+1}}" tabindex="-1" role="dialog" aria-labelledby="label{{$i+1}}" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="label{{$i+1}}">Delete Row</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to remove this row?</div>
<div class="modal-footer">
<button class="btn bg-gray-300" type="button" data-dismiss="modal">Cancel</button>
<form id="remove-form{{$i+1}}" name="formrow[]" action="{{route('preliminary-children.updateChildren')}}" method="POST" class="d-inline-block">
<input type="text" name="removeID[]" id="removeID{{$i+1}}" value="{{$child->id}}" hidden="hidden">
<button type="submit" id="{{$i+1}}" name="removerow[]" class="btn btn-danger text-decoration-none btn_remove">Remove</button>
</form>
</div>
</div>
</div>
</div>
#endif
<input type="text" name="userID[]" id="userID{{$i+1}}" value="{{ $user->id }}" hidden="hidden">
<input type="text" name="ID[]" id="childID{{$i+1}}" value="{{$child->id}}" hidden="hidden">
<input type="text" name="rel[]" id="rel{{$i+1}}" value="child" hidden="hidden">
<div class="form-row">
<div class="form-group col-md-6">
<label for="first">First Name</label>
<input name="first[]" type="text" class="form-control" id="first{{$i+1}}" value="{{$child->first_name}}" placeholder="First Name">
</div>
<div class="form-group col-md-6">
<label for="last">Last Name</label>
<input name="last[]" type="text" class="form-control" id="last{{$i+1}}" value="{{$child->last_name}}" placeholder="Last Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputSSN">SSN</label>
<input data-inputmask="'mask': '999-99-9999'" value="{{$child->ssn}}" type="text" class="form-control" name="inputSSN[]" id="SSN{{$i+1}}" placeholder="SSN">
</div>
<div class="form-group col-md-6">
<label for="dob">Date of Birth</label>
<input data-inputmask="'mask': '99/99/9999'" type="datepicker" value="{{$child->dob}}" class="form-control" name="dob[]" id="do{{$i+1}}B" placeholder="Date of Birth">
</div>
</div>
<div class="form-row">
<div class="form-group col-md">
<label for="inputGender">Gender</label>
<br/>
<select class="form-control d-inline-block w-25 mr-1" name="inputGender[]" id="Gender{{$i+1}}" placeholder="Gender">
#foreach($genders as $gender)
<option value="{{$gender}}" #if($child){{ ($child->gender == $gender) ? 'selected' : ''}} #endif>{{$gender}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md">
<label for="primaryHeight">Height</label>
<br/>
<select class="form-control d-inline-block w-25 mr-1" name="primaryFeet[]" id="primaryFeet{{$i+1}}">
#foreach($feet as $foot)
<option value="{{$foot}}" #if($child){{($child->height_feet == $foot) ? 'selected' : ''}} #endif>{{$foot}}</option>
#endforeach
</select>
<span class="d-inline-block w-10 mr-3">ft</span>
<select class="form-control d-inline-block w-25 mr-1" name="primaryInches[]" id="primaryInches{{$i+1}}">
#foreach($inches as $inch)
<option value="{{$inch}}" #if($child){{($child->height_inch == $inch) ? 'selected' : ''}} #endif>{{$inch}}</option>
#endforeach
</select>
<span class="d-inline-block w-10">in</span>
</div>
<div class="form-group col-md">
<label for="primaryWeight">Weight (lbs)</label>
<br/>
<input type="number" min="0" step=".1" class="form-control d-inline-block w-75 mr-1" id="primaryWeight{{$i+1}}" value="{{$child->weight_lbs}}" name="primaryWeight[]" placeholder="Weight (lbs)">
<span class="d-inline-block w-10">lbs</span>
</div>
</div>
</div>
#endforeach
</div>
<hr/>
<div class="text-center my-4">
<button type="button" name="add" id="add" class="btn btn-success">Add More</button>
</div>
</div>
</div>
</div>
<div class="form-row mb-4 text-center">
<div class="form-group col-md-12 mt-4">
Go Back <button type="submit" name="savecontinue" class="btn btn-info">Save and Continue</button>
</div>
</div>
</form>
And here's my controller:
public function updateChildren(Request $request)
{
$id = Auth::user()->id;
$user = Auth::user();
$family = UserFamily::where(['user_id'=> $id, 'rel' => 'child'])->get();
$count = count($family);
$children = ($count > 0 ? $family : '');
$input = $request->all();
$rules = [
'first[*]' => 'required',
'userID[*]' => 'required',
'rel[*]' => 'required',
'first[*]' => 'required',
'last[*]' => 'required',
'inputSSN[*]' => 'required',
'dob[*]' => 'required',
'inputGender[*]' => 'required',
'primaryFeet[*]' => 'required',
'primaryInches[*]' => 'required',
'primaryWeight[*]' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
foreach($input['userID'] as $index => $value) {
$feet = $input['primaryFeet'][$index];
$inch = $input['primaryInches'][$index];
$weight = $input['primaryWeight'][$index];
$inches = ($feet * 12) + $inch;
$bmi = number_format(((703 * $weight) / ($inches * $inches)), 2, '.', '');
if(isset($input['ID'][$index])){
$famid = $input['ID'][$index];
}else {
$famid = '';
}
if($famid > 0 || $famid != '') {
$user = UserFamily::firstOrNew(['user_id'=> $id, 'rel' => 'child', 'id' => $famid]);
}else{
$user = UserFamily::create(['user_id'=> $id, 'rel' => 'child']);
}
$user->user_id = $id;
$user->bmi = $bmi;
$user->first_name = $input['first'][$index];
$user->dob = $input['dob'][$index];
$user->last_name = $input['last'][$index];
$user->rel = $input['rel'][$index];
$user->ssn = $input['inputSSN'][$index];
$user->gender = $input['inputGender'][$index];
$user->height_feet = $input['primaryFeet'][$index];
$user->height_inch = $input['primaryInches'][$index];
$user->weight_lbs = $input['primaryWeight'][$index];
$user->save();
}
return redirect()->route('preliminary-review');
}
return redirect()->route('preliminary-children')->withErrors($validator);
}
The only thing I've tried is setting up an if/else if($request->has('savecontinue'){ ... } and wrapping my existing controller in that. The else{} would handle all the modals, but it kept kicking back errors as if all my syntax was incorrect.
Thoughts? I'm also not opposed to approaching this differently if there's a better way.
PS, there's JS for the remove and add buttons, but I didn't think it was necessary for this question. If you need me to update this with it, I can. :)
Deleting a child is a different operation than updating a child, so you should have a different method for it in your controller. Not sure about how you've got anything named, but this should give you an idea.
Define a new route:
Route::delete('/whatever/{child}', [ChildController::class, 'deleteChild'])
->name('preliminary-children.deleteChild');
Note we'll use the delete HTTP method to access this route, and the ID is passed as part of the URL.
Update your view to point to this new route:
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="label{{$i+1}}">Delete Row</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to remove this row?</div>
<div class="modal-footer">
<button class="btn bg-gray-300" type="button" data-dismiss="modal">Cancel</button>
<form id="remove-form{{$i+1}}" name="formrow[]" action="{{route('preliminary-children.deleteChild', $child->id)}}" method="POST" class="d-inline-block">
<input name="_method" value="DELETE" type="hidden"/>
<button type="submit" id="{{$i+1}}" name="removerow[]" class="btn btn-danger text-decoration-none btn_remove">Remove</button>
</form>
</div>
</div>
</div>
A web browser can't send a delete request, so we spoof the method with a hidden input.
And then we make the method:
public function deleteChild(UserFamily $child)
{
$child->delete();
return response()->json("success");
}
By type hinting the parameter, Laravel automatically looks up the relevant record in the database for you. Of course you'll want to do some error checking there too.
I am trying to send multiple files in one request using DropZone js.
<script type="text/javascript">
Dropzone.autoDiscover = false;
var file= new Dropzone(".dropzone",{
url: any url,
method:"post",
paramName:"PhotoFiles",
addRemoveLinks:true,
autoProcessQueue: false
});
//Upload file onsubmit
$('#UploadMultiFiles').submit(function(){
file.processQueue();
});
file.on("sending",function(a,b,c){
a.token=Math.random();
c.append("token",a.token);
});
The issue is I have other inputs in the same form that is why I used onsubmit, and these inputs are saved in a table and the images from dropzone are saved in another table with and both tables have one to many relationship
The Controller like:
public function university_offers($id){
$data['university_offer_name']=$this->input->post('university_offer_name');
$data['university_id_fk']=$this->input->post('university_id_fk');
$data['university_offer_details']=$this->input->post('university_offer_details');
if ($this->input->post('save')){
$data2['university_offer_id_fk'] = insertrecords('university_offers',$data);
$data2['university_id_fk'] = $data['university_id_fk'];
//when I put the next IF at the begining of the function it works but ofcourse without the other data fields that I need.
if(isset($_FILES) && $_FILES != null){
$data2['photo_name'] = upload_image('PhotoFiles');
insertrecords('university_offer_photos',$data2);
}
if (insertrecords('university_offer_photos',$data2)==true)
message('success','');
redirect('Admin/university_offers/0','refresh');
}
$data['view']="admin/universities/university_offers";
$this->load->view('index',$data);
}
What I need is sending al uploaded files and do a loop in the controller to save it right in the database, not sending each file to the controller and saving it.
Edit:
My view is like:
<form action="university_offers/<?=$id?>" id="UploadMultiFiles" autocomplete="off" method="post" enctype="multipart/form-data" class="m-t-5" novalidate>
<div class="form-body">
<h3 class="card-title">بيانات العرض</h3>
<hr>
<div class="row p-t-20">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">إسم العرض<span class="text-danger">*</span></label>
<div class="controls">
<input type="text" autocomplete="off" name="university_offer_name" class="form-control" required value="<?php if(isset($result)) echo $result['university_offer_name']?>" data-validation-required-message="يجب إدخال إسم العرض" placeholder="إسم العرض">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">إسم الجامعة<span class="text-danger">*</span></label>
<div class="controls">
<select name="university_id_fk" id="select" class="form-control" required data-validation-required-message="يجب إختيار الجامعة" aria-invalid="false">
<option value="">إختر الجامعة</option>
<?php
foreach (selectrecords("*",'universities') as $university):
$select = '';
if(isset($result) && $result['university_id_fk'] == $university->university_id_pk)
$select = 'selected';
?>
<option <?=$select?> value="<?php echo $university->university_id_pk ?>"><?php echo $university->university_name ?></option>
<?php endforeach;?>
</select>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">تفاصيل العرض<span class="text-danger">*</span></label>
<div class="controls">
<textarea id="mymce" name="university_offer_details"><?php if(isset($result)) echo $result['university_offer_details'] ?></textarea>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">صور العرض<span class="text-danger">*</span></label>
<div class="row">
<?php
if(isset($result)) {
foreach (selectrecords('*','university_offer_photos',array('university_offer_id_fk'=>$result['university_offer_id_pk'])) as $photo):
echo'
<div class="form-group text-center" id="'.$photo->photo_id_pk.'">
<div class="col-md-12">
<img style="border: none; height: 150px; width: 150px; border-radius: 10px;" class="img-thumbnail" src="'.base_url().'public/uploads/images/'.$photo->photo_name.'">
</div>
حذف الملف
</div>
';
endforeach;
}
?>
</div>
<div class="dropzone">
<div class="dz-message">
<span> Drag and Drop your files here Or Click here to upload</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-actions">
<input type="hidden" id="url" value="university_offers/<?=$id?>" />
<?php
if($id == 0)
echo '<button type="submit" id="butt" name="save" value="save" class="btn btn-primary"> <i class="fa fa-check"></i> حفظ</button>';
else
echo '<button type="submit" id="butt" name="edit" value="edit" class="btn btn-primary"><i class="fa fa-check"></i> حفظ</button>';
?>
<button type="reset" class="btn btn-inverse"><i class="fa fa-times"></i> إلغاء</button>
<?php
if($id == 0)
echo'<button type="button" id="backword" class="btn btn-second"><i class="fa fa-undo"></i> رجوع</button>';
else{
echo '<input type="hidden" name="university_offer_id_pk" value="'.$result['university_offer_id_pk'].'"/>
<button type="button" class="btn btn-second"><i class="fa fa-undo"></i> رجوع</button>';
}
?>
</div>
</div>
</form>
upload_image function, I put it in the Helper
function upload_image($file_name){
$CI =& get_instance();
$config['upload_path'] = 'public/uploads/images';
$config['allowed_types'] = 'gif|Gif|ico|ICO|jpg|JPG|jpeg|JPEG|BNG|png|PNG|bmp|BMP|WMV|wmv|MP3|mp3|FLV|flv|SWF|swf';
$config['max_size'] = '1024*8';
$config['encrypt_name']=true;
$CI->load->library('upload',$config);
if(! $CI->upload->do_upload($file_name)){
return false;
}else{
$datafile = $CI->upload->data();
thumb($datafile);
watermark($datafile);
return $datafile['file_name'];
}}
My submit button suddenly stopped working. It submits data into a MySQL database.
While I was doing some design changes, it suddenly stopped working.
Are there any apparent errors/mistakes in the code below?
I'm a noob who's currently trying to learn some PHP and so on, so any help would be much appreciated. :)
<section id="moviesearch">
<!-- Section -->
<div class="subcribe2">
<div class="container">
<!-- Container -->
<div class="row">
<!-- Row -->
<div class="col-md-4">
</div>
<body ng-app="myApp">
<div ng-controller="MyCtrl" class="col-md-4">
<form class="form-watch-list-create">
<input required="required" placeholder="Movie title" type="text" class="form-control" typeahead="item.Title for item in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-min-length="2" typeahead-wait-ms="1000" typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html" ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</body>
</div>
<!-- End Row -->
</div>
<!-- End Container -->
</div>
<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label | typeaheadHighlight:query" style="width: auto;"></span>
({{match.model.Year}})
<!-- <img ng-src="{{match.model.Poster}}" width="120"> -->
</a>
</script>
<div ng-controller="MyCtrl">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
</section>
Edit
Here's the code that sends data into the MYSQL database:
function addWatchList() {
if (isset($_GET['addtowatchlist'])) {
$name = $_GET['name'];
$conn = dbmysql();
$query ="INSERT INTO watch_list values(null,'{$name}','{$_SESSION['user_id']}','NOW()')";
if (mysqli_query($conn,$query)) {
$last = "SELECT * FROM watch_list WHERE created_by = '{$_SESSION['user_id']}' order by id desc limit 1";
$data = mysqli_query($conn,$last);
while ($row = mysqli_fetch_assoc($data)) {
include "_view.php";
}
}else {
echo "An error occurred. Please try again.";
}
exit;
}
}
I am looking to your code, missing attribute action="somefile.php" and method="get" if you are planning on submitting it using the form, you should put it or if you are planning on submitting your code using javascript you can use <form onsubmit="myFunction()"> That is what you are missing. I am not seeing you addtowatchlist name from your input so that php can catch it to your isset.
The submit button isn't inside the form.
You're missing the </form> tag to end the <form>. But you have </div> that matches the <div> just before </form>, so it's implicitly closing the <form> tag as well. Then you have <input type="submit"> after it. Since the submit button isn't inside the form, and has no form tag associating it with the form, it doesn't know which form it should submit when you click on it.
Try this:
<form class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>
This puts the form around both columns.
Use the method attribute in form tag and end the form tag properly
<form role="form" method="get" class="form-watch-list-create">
<div ng-controller="MyCtrl" class="col-md-4">
<input
required="required"
placeholder="Movie title"
type="text"
class="form-control"
typeahead="item.Title for item in getLocation($viewValue)"
typeahead-loading="loadingLocations"
typeahead-min-length="2"
typeahead-wait-ms="1000"
typeahead-on-select="onSelected($item)"
typeahead-template-url="customTemplate.html"
ng-model="asyncSelected">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
</div>
<div class="col-md-4">
<button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
</div>
</form>