How to get value from textarea in Laravel 5.2? - php

I'm currently working on Laravel 5.2 and I'm finding difficult to fetch value from textarea inside controller.
I've tried $request->input just like we used to fetch values from inputs but it's not working in my case. I have done this far.
managecategories.blade.php
<form action="/addcategories" method="post" id="addcategory_form" name="addcategory_form">
<div>
<label>Insert Category</label>
<input type="text" name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$">
</div>
<div>
<label>Insert Category</label>
<textarea name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$" row="5" col="200"></textarea>
</div>
<input type="submit" value="Add Category" id="category_btn">
</form>
AdminAjaxController
public function addcategory(Request $request)
{
$category=$request->input('category');
$category_description=$request->input('description');
$insertcategory= DB::insert('insert into categories(category_name,description) values(?, ?)',[$category,$category_description]);
$fetch_category= DB::select('select category_name,category_id from categories');
return response()->json(array('add_category' => $category),200);
}

Oops Pity me! Both fields are named category. I need to change the name on the textarea to description caught by Aynber.
here is the edited and correct code
<form action="/addcategories" method="post" id="addcategory_form" name="addcategory_form">
<div>
<label>Insert Category</label>
<input type="text" name="category" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$">
</div>
<div>
<label>Insert Category</label>
<textarea name="description" data-validation="required" data-validation="custom" data-validation-regexp="^[a-zA-Z ]{2,30}$" row="5" col="200"></textarea>
</div>
<input type="submit" value="Add Category" id="category_btn">
thanks mate! :)

Related

Pass value between 2 html forms in the same page

this question has been asked but I haven't found a working solution.
I have 2 forms. The first is sending datas to the database (form2). The second one is uploading a photo (form1).
I would need to save the path of the uploaded picture and store it in a text input in form1 and after send it with the form2 datas. Do you have any tips? It is possible without JQuery using just Php?
I have a solution with JQuery but it is not working.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
function submitform()
{
var name=$('#name').val();
$('#variable').val(name); // set the value of name field to the hidden field
$('form#form1').submit(); // submit the form
}
</script>
Create a new product
<form action="../handlers/processNewProduct.php" id="form2">
<div class="mb-3">
<label for="productname" class="form-label">Product Name</label>
<input type="text" class="form-control" id="productname" name="productname" placeholder="Product Name">
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<input type="text" class="form-control" id="description" value="xxxxxxxxx" name="description" placeholder="Description">
</div>
<div class="mb-3">
<label for="pricelist" class="form-label">Price list</label>
<input type="number" class="form-control" id="pricelist" name="pricelist" placeholder="Price">
</div>
<input type='text' name='variable' id='variable' value=''>
<button type="" class="btn btn-primary">Insert data</button>
</form>
<form action="../handlers/processUploadProductPicture.php" method="post" enctype="multipart/form-data" id="form1" onsubmit="submitform()">
Select image to upload:
<input type='file' name='fileToUpload' id='name'>
<input type='submit' value='Upload Image' id='upload'>
</form>

Duplicate Form using button

I'm trying to duplicate 4 input text. For example I want to duplicate 'Produk Simpanan Saham', then only 4 input on 'Produk Simpanan Saham' div will be duplicated. I already tried a few methods using jQuery but it seem doesn't work.
<form action="" method="post" class="form-horizontal">
<br><br>
<div class="form-group">
Upload File :
<input type="file" name="file">
</div>
<div class="saham">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Saham</center>
</h3>
</div>
<label for="kp_produk">Kode Produk :</label>
<input type="text" name="kp_produk" class="form-control">
<br>
<label for="produk_saham">Produk :</label>
<input type="text" name="produk_saham" class="form-control">
<br>
<label for="bunga_saham">Bunga :</label>
<input type="text" name="bunga_saham" class="form-control">
<br>
<label for="ket_saham">Keterangan :</label>
<input type="text" name="ket_saham" class="form-control">
</div>
<div class="harian">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Harian</center>
</h3>
</div>
<label for="kp_harian">Kode Produk :</label>
<input type="text" name="kp_harian" class="form-control">
<br>
<label for="produk_harian">Produk :</label>
<input type="text" name="produk_harian" class="form-control">
<br>
<label for="bunga_harian">Bunga :</label>
<input type="text" name="bunga_harian" class="form-control">
<br>
<label for="ket_harian">Keterangan :</label>
<input type="text" name="ket_harian" class="form-control">
</div>
you mush have blueprint of field first like
var blueprint= '<div class="saham">'+
'<div class="w3-container w3-black">'+
'<h3>'+
'<center>Produk Simpanan Saham</center>'+
'</h3>'+
'</div>'+
'<label for="kp_produk">Kode Produk :</label>'+
'<input type="text" name="kp_produk[]" class="form-control">'+
'<br>'+
......
used [] for all name field to get array request when you send form to server
example :
'<input type="text" name="kp_produk[]" class="form-control">'+
and make button add like (jquery) example
<button onclick="$('form').append(blueprint)">add</button>
just use
var dom=$($('.saham')[0]).clone();
//copy dom
$('.form-horizontal').html(dom):
//paste dom
dont forget to add [] on name tag dom input to set data send to array, because if you not set. data with key kp_produk value is last tag name with name kp_produk
if you use [] (kp_product[]) data will set to array
kp_product=['anu1',anu2];

how to insert logic work on php

i got template php from internet. then i want make other insert. but i dont know this code work
in code have submit button like this
<input type="submit" class="btn btn-primary" value="Добави">
and form like this
<div class="form-group">
<label>Nama</label>
<input type="text" class="form-control" name="name" required="required">
</div>
<div class="form-group">
<label> Deskripsi</label>
<textarea class="form-control" name="description"></textarea>
</div>
<div class="form-group">
<label>Gambar</label>
<input type="file" name="image" class="form-control" required="required">
</div>
<div class="form-group">
<label>Harga</label>
<input type="text" name="price" class="form-control" required="required">
</div>
<div class="form-group">
<label>
Cafe Yang Menjual</label>
<select name="cafe_id" class="form-control" required="required">
<option value="">Silahkan Pilih Restoran Yang Akan Menjual Makanan Ini</option>
<?php foreach($admin->getAllMenus() as $menu) { ?>
<option value="<?php echo $menu->id; ?>"><?php echo $menu->nama; ?></option>
<?php } ?>
</select>
</div>
and php syntax from that food.php
if(isset($_POST["name"])){
$image_name=$_FILES["image"]["name"];
$image=$_FILES["image"]["tmp_name"];
$image_name=time().$image_name;
move_uploaded_file($image, "../foods/".strtolower($image_name));
$_POST["image"]=strtolower($image_name);
$admin->addFood($_POST);
}
and modul code for insert
public function addFood($data){
if($this->db->query("insert into foods(name,image,description,price,cafe_id) values('".$data['name']."','".$data['image']."','".$data['description']."',".$data['price'].",".$data['cafe_id'].")")){
echo"<script>window.location.href='index.php?page=foods';
alert('Успешно добавяне на храна');</script>";
}
else{
echo"<script>window.location.href='index.php?page=foods';
alert('Проблем при добавяне на храна');</script>";
}
}
i dont know how that code connect with that submit button. with value value="Добави"
that i know is isset($_POST["value submit button"])) but in this code there is name.
I'm not sure I totally understand your question but basically a <form> HTML element can be used to transfer data to the server, such as:
<form method="POST" action="food.php">
...
</form>
Then when the submit method is triggered, the data will be POSTed to food.php.
Just to be clear, you need to have PHP installed and running on a webserver before this will work; you can't just copy PHP code into a HTML file and hope it works.

How to insert AngularJS array into mysql database using php?

I can't find solution to my problem...
I've created some kind of deck builder and when I choose cards to deck (which is empty on the begining $scope.deck = []) I would like to insert it to database.
I have form like this:
<form name="myform">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" ng-model="showCrudData.name" class="form-control" />
</div>
<div class="form-group">
<label for="type">Deck Type</label>
<input type="text" id="type" name="type" ng-model="showCrudData.type" class="form-control" />
</div>
<div class="form-group">
<label for="class">Class</label>
<input type="text" id="class" name="class" ng-model="showCrudData.class" class="form-control" />
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" id="author" name="author" ng-model="showCrudData.author" class="form-control" />
</div>
<label for="cards">Deck</label>
<p type="value" id="cards" name="cards" ng-model="showCrudData.cards">{{cards}}</p>
Add
<button type="reset" class="btn btn-default">Reset</button>
</form>
Im trying to insert {{cards}} into database as u can see, but its not working.
Here is my testing page where Im trying to solve the problem. The Deck label before choosing cards is looking like this []
And thats part of my PHP file where I insert values into table
case "add":
if(!empty($_POST['data'])){
$sql = "INSERT INTO decks values ('',:nm,:tp,:cl,:au,:cd)";
$insert = $query = $db->prepare($sql);
$query->execute(array(":nm"=>$_POST['data']['name'],":tp"=>$_POST['data']['type'],":cl"=>$_POST['data']['class'],":au"=>$_POST['data']['author'],":cd"=>print_r($_POST['data']['cards'])));
I tried to solve problem adding print_r, but it didnt work because its just adding number "1" into column "deck" in table.

Add multiple rows with relation many-to-many

I'm trying to save a multiple database records with image name and thumb(for now only text inputs). Every image has also a category/categories (made by multiselect and pivot table between image and categories with many to many relation). The question is: how to sync/attach those categories? I do not want to use foreach ::create, because it will generate unnecessary database requests. For now I have something like this:
my blade file (part in form)
<form action="/panel/images" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="row[1][title]">Image title</label>
<input type="text" name="row[1][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[1][thumb]">Thumb</label>
<input type="text" name="row[1][thumb]" class="form-control" />
</div>
<!-- date because insert methode don't create timestamps autimatically-->
<input type="hidden" name="row[1][created_at]" value="{{ $date }}" />
<input type="hidden" name="row[1][updated_at]" value="{{ $date }}" />
<div class="form-group">
<label for="multi[0][]">Categories</label>
<select name="multi[0][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<div class="form-group">
<label for="row[2][title]">Image title</label>
<input type="text" name="row[2][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[2][thumb]">Thumb</label>
<input type="text" name="row[2][thumb]" class="form-control" />
</div>
<div class="form-group">
<label for="multi[1][]">Categories</label>
<select name="multi[1][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<input type="text" name="row[2][created_at]" value="{{ $date }}" />
<input type="text" name="row[2][updated_at]" value="{{ $date }}" />
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
And the controller
public function store(Request $request)
{
$image = new Image;
$inputs = Input::get('row');
$multiSelect = Input::get('multi');
$image::insert($inputs);
$image->images_catetories()->sync($multiSelect);
return redirect()->route('newImage')->with('status', 'images added');
}
EDIT:
Right now it throws an error "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into image_images_category (0, image_id, images_category_id, 1) values (18, , 1, 19))"
where "image_images_category" is my pivot table and values 18, 1, 19 are from multiselect.

Categories