Concatenate String in php - php

When i click on Add buttton it adds the dropdown selected text to the bottom textbox. and again when i change the value of dropdown and click on add. It rewrites the in textbox. I want to concatenate with previous value of the textbox with comma saparated whenever i click on add
Im new to php.
<?php
if(isset($_POST["in_submit"]))
{
$i1=$_POST['name'];
$i2=$_POST['dose'];
$i3 = $i1."*".$i2;
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="#" class="form-horizontal" method="post" accept-charset="utf-8">
<label for="inputEmail3" class="col-md-4 control-label">Name</label>
<select name="name" class="form-control" style="width:30%;">
<option value="Select">--SELECT--</option>
<option value="abc">abc</option>
<option value="def">def</option>
</select>
<input type="text" name="dose" value="" class="form-control"/>
<div class="col-sm-2">
<input type="submit" name="in_submit" value="Add" class="btn-danger" style="height:30px;width:60px;padding-top:2px;border-radius:5px"/>
</div>
</form>
<div class="col-sm-7">
<input type="text" name="text" value="<?php if(isset($i3)){ echo $i3;}?>" class="form-control" readonly />
</div>
</body>
</html>

Is because $i2=$_POST['dose']; is always empty
value="<?php if(isset($i3)){ echo $i3;}?>"
should be part of dose

After some changes You don't need to use dose you can concatenate like this -> if you get the solution then vote up.
<?php
if(isset($_POST["in_submit"]))
{
$i1=$_POST['name'];
$i2=$_POST['text'];
if($i2!=''){
$i3 = $i2.",".$i1;
}else{
$i3 = $i1;
}
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form action="#" class="form-horizontal" method="post" accept-charset="utf-8">
<label for="inputEmail3" class="col-md-4 control-label">Name</label>
<select name="name" class="form-control" style="width:30%;">
<option value="Select">--SELECT--</option>
<option value="abc">abc</option>
<option value="def">def</option>
</select>
<div class="col-sm-2">
<input type="submit" name="in_submit" value="Add" class="btn-danger" style="height:30px;width:60px;padding-top:2px;border-radius:5px"/>
</div>
<div class="col-sm-7">
<input type="text" name="text" value="<?php if(isset($i3)){ echo $i3;}?>" class="form-control" readonly />
</div>
</form>
</body>
</html>

Related

Submit form with a drop-down list

I got an HTML-form with a drop-down list of products. When you select a product, you will have a new input field appear. For each product type input fields are different. For example: a book will have a new input field of weight, DVD will have a new input field of size and furniture will have inputs of HxWxL dimensions.
So here is the question: how is it better to submit a form to MySQL with different product types so that they are saved in one table? Without the use of if-else and switch statements.
P.S. I am sorry if this question might be asked before here.
P.S.S. And yes, code/name and price inputs are by default with each new product type.
Here is my database related code:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'things') or die(mysqli_error($mysqli));
if (isset($_POST['save'])) {
$code= $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
$mysqli->query("INSERT INTO products (code, name, price) VALUES('$code', '$name', '$price')") or die($mysqli->error);
And here is my form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add</title>
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<h1>Add</h1>
<hr>
<?php require_once 'connect.php'; ?>
<div class="container">
<!--FORM-->
<form id="form" action="connect.php" method="POST">
<!--CODE-->
<div id="form-group">
<label for="code" id="code-label"> Code</label>
<input type="text" id="code" name="code" required>
</div>
<!--NAME-->
<div id="form-group">
<label for="name" id="name-label"> Name </label>
<input type="text" id="name" name="name" required>
</div>
<!--PRICE-->
<div id="form-group">
<label for="price" id="price-label"> Price </label>
<input type="number" id="price" name="price" required>
</div>
<div id="form-group">
<label for="product-type" id="product-type-label"> Type Switcher </label>
<select name="product-type" id="product-type" onchange="showHide()">
<option selected hidden value="">Type Switcher</option>
<option value="dvd">DVD</option>
<option value="furniture">Furniture</option>
<option value="book">Book</option>
</select>
</div>-->
<!--HIDDEN INPUTS-->
<div id="size" style="display: none">
<div id="form-group">
<label for="size" id="size-label"> Size </label>
<input type="number" name="size" required>
</div>
</div>
<div id="height" style="display: none">
<div id="form-group">
<label for="height" id="height-label"> Height </label>
<input type="number" name="height" required>
</div>
</div>
<div id="width" style="display: none">
<div id="form-group">
<label for="width" id="width-label"> Width </label>
<input type="number" name="width" required>
</div>
</div>
<div id="length" style="display: none">
<div id="form-group">
<label for="length" id="length-label"> Length </label>
<input type="number" name="length" required>
</div>
</div>
<div id="weight" style="display: none">
<div id="form-group">
<label for="weight" id="weight-label"> Weight </label>
<input type="number" name="weight" required>
</div>
</div>
<button type="submit" name="save">Save</button>
</form>
</div>
<script src="javascript/scripts.js"></script>
</body>
</html>

How do i create an array from form input

I'm trying to write a php script that takes the input from this form and creates an array with a unique key for each input box/text boxes content.
<div class="container">
<div class="row">
<div class="col-3 mx-auto">
<form action="adminhandler.php">
<div class="form-group">
<label for="product name">Enter product name</label>
<input type="text" class="form-control" id="productName" aria-describedby="emailHelp" placeholder="" name="productname">
</div>
<div class="form-group">
<label for="product name">Select product category</label>
<select name="select" class=" custom-select">
<option selected>Open this select menu</option>
<option value="1">Formal Shoes</option>
<option value="2">Sneakers</option>
<option value="3">Tracksuits</option>
<option value="3">Clothing</option>
<option value="3">Accessories</option>
</select>
</div>
<div class="form-group">
<label for="product price">Enter product price(Naira)</label>
<input type="text" class="form-control" id="productPrice" aria-describedby="emailHelp" placeholder="" name="price">
</div>
<div class="form-group">
<label for="product name">Select image</label>
<input type="file" class="form-control-file" id="productimage" aria-describedby="emailHelp" placeholder="">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
I think you want to use a form as an array and then pass the same data as an array.If you are using the $_POST, you will need to use the name attribute of HTML. And use something like this :
<input name="person[1][first_name]" value="jane" />
This when passing through PHP will be sent as Array.
Here is the reference :
http://php.net/manual/en/reserved.variables.post.php
An Excerpt from one of the contributors :
<form>
<input name="person[0][first_name]" value="john" />
<input name="person[0][last_name]" value="smith" />
<input name="person[1][first_name]" value="jane" />
<input name="person[1][last_name]" value="jones" />
</form>
When Trying To Process Values using PHP
<?php
var_dump($_POST['person']);
//will get you something like:
array (
0 => array('first_name'=>'john','last_name'=>'smith'),
1 => array('first_name'=>'jane','last_name'=>'jones'),
)
?>

how to use different forms in one blade file in laravel 5.6

I am using laravel 5.6 and I am developing auto classifieds web application. in My app I have 3 different vehicle categories as car, van, truck and I have blade file to select this three different vehicle types. when I select this vehicles my urls show like this,
http://localhost:8000/post-ad/Truck/8 <- this is category id
http://localhost:8000/post-ad/van/7
http://localhost:8000/post-ad/Car/5
now when I clicked one of above vehicle category page redirect to show.blade.php file, so, now I need 3 different forms to submit data to each vehicles, this is my vehicles form, car form
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" id="exampleFormControlSelect1" name="district">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Town</label>
<select class="form-control" id="exampleFormControlSelect1" name="town">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select class="form-control" id="exampleFormControlSelect1" name="brand">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select class="form-control" id="exampleFormControlSelect1" name="model">
</select>
</div>
</form>
van form
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" id="exampleFormControlSelect1" name="district">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Town</label>
<select class="form-control" id="exampleFormControlSelect1" name="town">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select class="form-control" id="exampleFormControlSelect1" name="brand">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select class="form-control" id="exampleFormControlSelect1" name="model">
</select>
</div>
</form>
Truck Form
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="cid" name="cid" value="{{ $catagories->id }}" />
<div class="form-group">
<label for="exampleFormControlSelect1">District</label>
<select class="form-control" id="exampleFormControlSelect1" name="district">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Town</label>
<select class="form-control" id="exampleFormControlSelect1" name="town">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select class="form-control" id="exampleFormControlSelect1" name="brand">
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select class="form-control" id="exampleFormControlSelect1" name="model">
</select>
</div>
</form>
I need show each form when some user click each vehicle links on one blade file. how can I do this?
You may add name to your routes, for example:
Route::group(['prefix' => 'post-ad'], function () {
Route::get('Truck/{id}', 'TruckController#fetch')->name('track');
Route::get('Van/{id}', 'VanController#fetch')->name('van');
Route::get('Car/{id}', 'CarController#fetch')->name('car');
})
Put your form to another files like:
truck-form.blade.php
van-form.blade.php
car-form.blade.php
In your view:
#if(request()->route()->getName() = 'track')
#include('truck-form')
#elseif(request()->route()->getName() = 'van')
#include('van-form')
#elseif
#include('van-form')
#endif

In .php file, when add php code to html, html not works

In .php file, when add php code to html, only the html that out of php code works, but within the php code the html not works. But when I delete the php code blocks, the html works again and shows the form as the html.
Please any suggestion why this happening...
My codes are here:
<div class="container" style="margin: 2em 5em";>
<h3>Update Category</h3><hr/>
<form action="http://localhost/twlp/mvc_two/Category/insertCategory" method="post">
<?php
if(isset($catById)){
foreach($catById as $value){
?>
<div class="form-group">
<label for="text">Category Name:</label>
<input type="text" class="form-control" name="name" required="1" value="<?php echo $value['name']; ?>"/>
</div>
<div class="form-group">
<label for="text">Category Title:</label>
<input type="text" class="form-control" name="title" required="1" value="<?php echo $value['title']; ?>"/>
</div>
<button type="submit" name="submit" class="btn btn-default">Update</button>
<?php } } ?>
</form>
</div>
The problem is from $catById.
I defined an example $catById. Then the page runs well.
<?php
$value1=array('name'=>'test 1-name','title'=>'test 1-title');
$value2=array('name'=>'test 2-name','title'=>'test 2-title');
$catById=array($value1,$value2);
?>
<div class="container" style="margin: 2em 5em";>
<h3>Update Category</h3><hr/>
<form action="http://localhost/twlp/mvc_two/Category/insertCategory" method="post">
<?php
if(isset($catById)){
foreach($catById as $value){
?>
<div class="form-group">
<label for="text">Category Name:</label>
<input type="text" class="form-control" name="name" required="1" value="<?php echo $value['name']; ?>"/>
</div>
<div class="form-group">
<label for="text">Category Title:</label>
<input type="text" class="form-control" name="title" required="1" value="<?php echo $value['title']; ?>"/>
</div>
<button type="submit" name="submit" class="btn btn-default">Update</button>
<?php
}
} ?>
</form>
</div>

How to retrieve values from datepicker in a datepicker input field (using PHP MySQL)?

I have this form to update a record using PHP MySQL. I want to retrieve the value of the date column in a datepicker field. Is that possible? I try using $_REQUEST['date'], but the result is empty.
This is my form code:
<script type="text/javascript">
$(function(){
$('#date_4').datepicker({
dateFormat: 'yy-mm-dd'
});
});
</script>
<form enctype="multipart/form-data" id="fm" method="post" novalidate>
<div class="fitem">
<label>No:</label>
<input name="id" id="id" class="easyui-validatebox" size="50" required>
</div>
<div class="fitem">
<label>Date:</label>
<input id="date_4" name="date_3" class="easyui-validatebox" size="50">
</div>
<div class="fitem">
<label>From:</label>
<input id="from" name="from" class="easyui-validatebox" size="50" required>
</div>
<div class="fitem">
<label>Hal:</label>
<textarea name="hal" class="easyui-validatebox" cols="48" rows="5" required></textarea>
</div>
<div class="fitem">
<label>attachment:</label>
<input type="hidden" name="max_file_size" value="20000" />
<input name="userfile" type="file" />
</div>
</form>
You are using method="post" on your form, hence it'd be more correct to grab it from $_POST['date_3']. See the code below (sorry for the length, thought it'd be better to show the full thing)
<!doctype html>
<html lang="en">
<head>
<meta ncharset="utf-8"/>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<?php
if(isset($_POST['date_3'])){
echo $_POST['date_3'];
}
?>
<form enctype="multipart/form-data" id="fm" method="post" novalidate>
<div class="fitem">
<label>No:</label>
<input name="id" id="id" class="easyui-validatebox" size="50" required>
</div>
<div class="fitem">
<label>Date:</label>
<input id="date_4" name="date_3" class="easyui-validatebox" size="50">
</div>
<div class="fitem">
<label>From:</label>
<input id="from" name="from" class="easyui-validatebox" size="50" required>
</div>
<div class="fitem">
<label>Hal:</label>
<textarea name="hal" class="easyui-validatebox" cols="48" rows="5" required></textarea>
</div>
<div class="fitem">
<label>attachment:</label>
<input type="hidden" name="max_file_size" value="20000" />
<input name="userfile" type="file" />
</div>
<input type="submit" value="Go!">
</form>
<script type="text/javascript">
$(function(){
$('#date_4').datepicker({
dateFormat: 'yy-mm-dd'
});
});
</script>
</body>
</html>
Either way, it works fine for $_REQUEST too, since $REQUEST holds $_GET, $_POST and $_COOKIE (see this post about $_REQUEST vs $_GET and $_POST) You can replace the PHP code above with the following and it will still print your date, if you select one, once you submit it:
<?php
if(isset($_REQUEST['date_3'])){
echo $_REQUEST['date_3'];
}
?>

Categories