I am new in OOP PHP and I am still learning, I have a form where name of locations is populated through JQuery/HTML form using drop-down field with same name, my problem is when I try to insert in mysql database, I did not get the selected value,
<div id="NCR" class="drop-down-show-hide">
<div class="field">
<label for="region"> NCR </label>
<select name="region" id="ncr region">
<option value="">-- Choose --</option>
<option value="1"> Region 1 </option>
<option value="2"> Region 2 </option>
<option value="3"> Region 3 </option>
</select>
</div>
</div>
<div id="CAR" class="drop-down-show-hide">
<div class="field">
<label for="field"> CAR </label>
<select name="region" id="car region">
<option value="">-- Choose --</option>
<option value="4"> Region 4 </option>
<option value="5"> Region 5 </option>
<option value="6"> Region 6 </option>
</select>
</div>
</div>
<div id="region3" class="drop-down-show-hide">
<div class="field">
<label for="region"> CAMANABA </label>
<select name="region" id="camanaba region">
<option value="">-- Choose --</option>
<option value="7"> Region 7 </option>
<option value="8"> Region 8 </option>
<option value="9"> Region 9 </option>
</select>
</div>
</div>/HTML
PHP
if(Token::check(Input::get('token'))){
$validate = new Validate();
$validation = $validate->check($_POST, array(
'apz_account' => array(
'required' => true
),
'api_account' => array(
'required' => true
),
'wupos_tid' => array(
'required' => true
),
));
if($validation->passed()){
// check APZ account
$agent = DB::getInstance()->get('agent', array('apz_account', '=', Input::get('apz_account')));
if(!$agent->count()){
// check API account
$agent = DB::getInstance()->get('agent', array('api_account', '=', Input::get('api_account')));
if(!$agent->count()){
$agent = new Agent();
try {
$agent->createAgentAccount(array(
'apz_account' => Input::get('apz_account'),
'api_account' => Input::get('api_account'),
'wupos_tid' => Input::get('wupos_tid'),
'region' => Input::get('region'),
'registered' => date('Y-m-d H:i:s'),
));
// print_r($agent);
// Session::flash('success', 'You have registered successfully.');
// Redirect::to('../../index.php');
} catch(Exception $e){
die($e->getMessage());
}
} else {
echo Input::get('api_account'), ' account is already exists';
}
} else {
echo Input::get('apz_account'), ' account is already exists';
}
} else {
foreach ($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
Please advice. Thank you
You need unique names for each form element, with php you can use region[]. this will give you an array in your server script.
Then when you get the value do current(array_filter(Input::get('region')));
Related
I have an Employee Form Edit.
My problem is, my function for the edit is working fine but I can't get the value of my employee type.
I have 4 employee type:
Staff
Supervisor
Manager
Super User
Every time I go to edit form, the employee type on drop down is always on staff (I want to display, if the employee is "Manager" then the drop box show Manager).
This is the code for editroledetails.blade.php
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff">Staff</option>
<option value="Supervisor">Supervisor</option>
<option value="Manager">Manager</option>
<option value="Super User">Super User</option>
</select>
</div>
Controller function
public function editroledetails(Request $request)
{
$user = \Auth::user();
$userphone = 0;
$reportTo = DB::select(DB::raw("select username from customer_type where customer_type = 'Supervisor' or customer_type ='Manager' "));
$select = DB::select(DB::raw("select customer_type from customer_type "));
$data = [
'editUsername' => $request->editUsername,
'editNik' => $request->editNik,
'editEmail' => $request->editEmail,
'editRegIdentities' => $request->editRegIdentities,
'editReportTo' => $request->editReportTo,
'editID' => $request->editID
];
return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo', 'select'));
}
You can do something like this:
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
#if($user->customer_type == 'Staff')
<option value="Staff">Staff</option>
#endif
#if($user->customer_type == 'Supervisor')
<option value="Supervisor">Supervisor</option>
#endif
#if($user->customer_type == 'Manager')
<option value="Manager">Manager</option>
#endif
#if($user->customer_type == 'Super User')
<option value="Super User">Super User</option>
#endif
</select>
</div>
Use the selected attribute on the select options
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff" #if($user->customer_type == 'Staff')selected#endIf>Staff</option>
<option value="Supervisor" #if($user->customer_type == 'Supervisor')selected#endIf>Supervisor</option>
<option value="Manager" #if($user->customer_type == 'Manager')selected#endIf>Manager</option>
<option value="Super User" #if($user->customer_type == 'Super User')selected#endIf>Super User</option>
</select>
</div>
You can use this to show the selected item in select list
<?php
$customerTypes = [
'Staff' => 'Staff',
'Supervisor' => 'Supervisor',
'Manager' => 'Manager',
'Super User' => 'Super User',
];
?>
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
#foreach($customerTypes as $type)
<?php
$selected = "";
#if($user->customer_type == $type) {
$selected = "selected";
}
?>
<option value="{{ $type }}" {{ $selected }} >{{ $type }}</option>
#endforeach
</select>
</div>
I have a form from which i take some datas and using session i want to keep that datas as a history on my page, and also when i click on one line from my history i want my datas to be autocomplete in my form. I saw an example on one page and i tried doing it to apply to what i want but it's not quite functional.
This is my code for the form:
<form method="get">
Create:<br><br>
Name:<br> <input type="text" id="name" value="" /><br>
surname:<br> <input type="text" id="surname" value="" /><br>
Sex:<br> <div class="select-wrapper">
<select name="sex" id="sex">
<option value="">- Sex -</option>
<option value="woman">Woman</option>
<option value="male">Male</option>
</select>
</div>
Role: <div class="select-wrapper">
<select name="rol" id="rol">
<option value="">- Role -</option>
<option value="visitor">Visitor</option>
<option value="Professor">Professor</option>
<option value="Student">Student</option>
</select>
</div>
Text color: <div class="select-wrapper">
<select name="cul" id="cul">
<option value="">- Text color -</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="black">Black</option>
</select>
</div>
Font text: <div class="select-wrapper">
<select name="font" id="font">
<option value="">- Font text -</option>
<option value="15px Arial">Arial</option>
<option value="15px Times New Roman">Times New Roman</option>
<option value="15px Georgia">Georgia</option>
<option value="15px Comic Sans MS">Comic Sans MS</option>
<option value="15px Lucida Sans Unicode">Lucida Sans Unicode</option>
<option value="15px Courier New">Courier New</option>
</select>
</div>
Format : <div class="select-wrapper">
<select name="format" id="format">
<option value="">- Format -</option>
<option value="portrait">Portrait</option>
<option value="landscape">Landscape</option>
</select>
</div>
Style text: <div class="select-wrapper">
<select name="stil" id="stil">
<option value="">- Style text -</option>
<option value="stil1">Stil1</option>
<option value="stil2">Stil2</option>
<option value="stil3">Stil3</option>
<option value="stil4">Stil4</option>
<option value="stil5">Stil5</option>
<option value="stil6">Stil6</option>
</select>
</div>
</form>
And this is where i tried to use session:
<?php
session_start();
$createpas = parseRequest();
storecreatepas($createpas);
include "form.php";
$createpases = $_SESSION['createpases'];
include "history.php";
function storecreatepas($createpas) {
if (!isset($_SESSION['createpases'])) {
$_SESSION['createpases'] = [];
}
if (!$createpas->isEmpty()) {
$_SESSION['createpases'][] = $createpas;
}
}
function parseRequest() {
$createpas = new createpasRequest;
$createpas->cul = !empty($_GET['cul']) ? $_GET['cul'] : "";
$createpas->font = !empty($_GET['font']) ? $_GET['font'] : "";
$createpas->format = !empty($_GET['format']) ? $_GET['format'] : "";
$createpas->stil = !empty($_GET['stil']) ? $_GET['stil'] : "";
return $createpas;
}
/**
* createpas request
*/
class createpasRequest
{
public $cul = "";
public $font = "";
public $format = "";
public $stil = "";
function toQueryString() {
$params = [
'cul' => $this->cul,
'font' => $this->font,
'format' => $this->format,
'stil' => $this->stil
];
return http_build_query($params);
}
function isEmpty() {
return !$this->cul || !$this->font || !$this->format || !$this->stil;
}
function culAsObject() {
return new DateTime($this->cul);
}
function fontAsObject() {
return new DateTime($this->font);
}
function formatAsObject() {
return new DateTime($this->format);
}
function stilAsObject() {
return new DateTime($this->stil);
}
}
And the display code:
<ul>
<?php
foreach ($createpases as $s) {
?>
<li><a href="search.php?<?php echo $s->toQueryString() ?>">
<?php echo $s->cul?> - <?php echo $s->font?> - <?php echo $s->format?> - <?php echo $s->stil?>
</a></li>
<?php
}
?>
</ul>
It works just fine until some point. It gets my datas from my form posts them on the page but when i click on them they don't autocomplete in in my form. And also if i want the options to be unique in the list how can i do that? Right now if i complete the same datas 2 or 3 times they appear multiple times in my list.
Thank you for your help!
You need to start the session in the Form page and then check if the SESSION array contains the values to be echoed.
So your code will become:
<?php session_start();
if(!isset($_SESSION['name']){$_SESSION['name']=''}
if(!isset($_SESSION['sex']){$_SESSION['sex']=''}
if(!isset($_SESSION['rol']){$_SESSION['rol']=''}
?>
//this way you will not have issues on page first load before the user fills in the form
<form method="get">
Create:<br><br>
Name:<br> <input type="text" id="name" value="<?php echo $_SESSION['name']; ?>" /><br>
surname:<br> <input type="text" id="surname" value="" /><br>
Sex:<br> <div class="select-wrapper">
<select name="sex" id="sex">
<option value="" <?php if($_SESSION['rol']==''){echo 'selected'} ?>>- Sex -</option>
<option value="woman" <?php if($_SESSION['rol']=='woman'){echo 'selected'} ?>>Woman</option>
<option value="male" <?php if($_SESSION['rol']=='visitor'){echo 'selected'} ?>>Male</option>
</select>
</div>
Role: <div class="select-wrapper">
<select name="rol" id="rol">
<option value="" <?php if($_SESSION['rol']==''){echo 'selected'} ?>>- Role -</option>
<option value="visitor" <?php if($_SESSION['rol']=='visitor'){echo 'selected'} ?>>Visitor</option>
<option value="Professor"<?php if($_SESSION['rol']=='Professor'){echo 'selected'} ?>>Professor</option>
<option value="Student"<?php if($_SESSION['rol']=='student'){echo 'selected'} ?>>Student</option>
</select>
</div>
and so on.
It appears that your form does not auto-fill with the data because you do not have such a functionality implemented. This auto-complete functionality is not automatic, as in, the server does not do it for you.
Here is an example of what you need to do in order to get the autocomplete to work:
Name:<br> <input type="text" id="name" value="<?= $_SESSION['name'] ?>" /><br>
For the select to auto-complete, you'll need to do something like this:
<select name="cul" id="cul">
<option value="">- Text color -</option>
<option value="red" <? echo ($_SESSION['cul']==="red")?"selected='selected'":""; ?>>Red</option>
<option value="blue" <? echo ($_SESSION['cul']==="blue")?"selected='selected'":""; ?>>Blue</option>
<option value="black" <? echo ($_SESSION['cul']==="black")?"selected='selected'":""; ?>>Black</option>
</select>
This sets the value for each form element by directly setting the value / selected item using the $_GET data from a previous submission, or if there is no data, everything will be empty / default values.
Per a suggestion from one of the comments to this answer, I would like to point out that you must include "session_start()" to the top of the document if you wish to use the session variables on the document (if you have not done so already).
One question about the original question: is that first code block procedurally generated, or hand-typed?
How do I ignore empty values in MySQL-Statement?
Like if e.g. the Manfacturer Name A and Name B are selected, Color Red is selected and the Price is ignored(value is empty).
HTML-Part
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="container">
<div class="row">
<div class="col-xs-8">
<select multiple class="form-control" name="multiMan[]">
<option value="" selected> Manufacturer </option>
<option value="A"> Name A </option>
<option value="B"> Name B </option>
<option value="C"> Name C </option>
</select>
<br><br>
<select multiple class="form-control" name="multiCol[]">
<option value="" selected> Select color... </option>
<option value="red"> Red </option>
<option value="blue"> Blue </option>
<option value="green"> Green </option>
</select>
<br><br>
<select multiple class="form-control" name="multiPri[]">
<option value="" selected> Price... </option>
<option value="100"> 100 </option>
<option value="200"> 200 </option>
<option value="300"> 300 </option>
</select>
<br><br>
<button type="submit" name="submit_filter" class="btn"> GO </button>
</div>
</div>
</div>
</form>
<center>
PHP-Part
<center>
<?php
if(isset($_POST["submit_filter"])){
function splitArray($element){
return "'$element'";
}
$array1 = $_POST['multiMan'];
$searchMultiMan = implode(',', array_map('splitArray', $array1));
$array2 = $_POST['multiCol'];
$searchMultiCol = implode(',', array_map('splitArray', $array2));
$array3 = $_POST['multiPri'];
$searchMultiPri = implode(',', array_map('splitArray', $array3));
echo $searchMultiMan . "<br>";
echo $searchMultiCol . "<br>";
echo $searchMultiPri . "<br>";
$multiFilter = "SELECT data.manufacturer, data.color, data.price
FROM data
WHERE data.manufacturer IN ($searchMultiMan)
AND data.color IN ($searchMultiCol)
AND data.price IN ($searchMultiPri);";
echo $multiFilter;
$multiFilterErg = mysqli_query($db, $multiFilter);
}
?>
</center>
The problem here is, that the query is searching for a price with an empty entry.
What I want to do is that if the price is not setted to ignore this and show all entries with Name A and B and color red and all prices.
So, you need to only return non-empty elements from $_POST inside of splitArray(), check for string length to build the $where array, and only include the WHERE clause if there is something to filter with.
Complete Code :
function splitArray($element){
if($element){
return "'$element'";
}
}
$searchMultiMan=implode(',',array_map('splitArray',$_POST['multiMan']));
$searchMultiCol=implode(',',array_map('splitArray',$_POST['multiCol']));
$searchMultiPri=implode(',',array_map('splitArray',$_POST['multiPri']));
$where=[];
if(strlen($searchMultiMan)>0){$where[]="data.manufacturer IN ($searchMultiMan)";}
if(strlen($searchMultiCol)>0){$where[]="data.color IN ($searchMultiCol)";}
if(strlen($searchMultiPri)>0){$where[]="data.price IN ($searchMultiPri)";}
$multiFilter="SELECT data.manufacturer, data.color, data.price FROM data";
if(sizeof($where)>0){
$multiFilter.=" WHERE ".implode(' AND ',$where);
}
echo $multiFilter;
I have a problem regarding insertion of data after explode. In this gridview column, I throw function in value like this:
[
'attribute' => 'CONNECTOR_ACTION',
'value' => function($model){
$apps = \app\models\APPLICATION::find()
->where(['ID' => $model->ID_APPLICATION])
->one();
$options = $apps['CONNECTOR_PARAM'];
$optionsArr = explode(', ', $options);
return Html::activeDropDownList($model, 'CONNECTOR_ACTION', $optionsArr, ['class'=>'form-control', 'disabled' => true]);
},
'format' => 'raw'
],
And in HTML view like this:
<td>
<select id="requestapplication-connector_action" class="form-control" name="REQUESTAPPLICATION[CONNECTOR_ACTION]" disabled>
<option value="0">create</option>
<option value="1">addrole</option>
<option value="2">defaultrole</option>
<option value="3">removerole</option>
<option value="4" selected>disable</option>
<option value="5">enable</option>
<option value="6">setpassword</option>
</select>
</td>
If I want to change dropdown like this:
<td>
<select id="requestapplication-connector_action" class="form-control" name="REQUESTAPPLICATION[CONNECTOR_ACTION]" disabled>
<option value="create">create</option>
<option value="addrole">addrole</option>
<option value="defaultrole">defaultrole</option>
<option value="removerole">removerole</option>
<option value="disable" selected>disable</option>
<option value="enable">enable</option>
<option value="6">setpassword</option>
</select>
</td>
How do I do that?
This is because the keys of $optionsArr are 0-6 instead of the values. To combine it set the keys the same as the values.
$combined = array_combine($optionsArr, $optionsArr);
And then use $combined in Html::activeDropdownList()
How to check the duplication of following fields in controller.
this is my view code
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Name</label>
<div class="col-lg-4">
<select id ="choose" class="form-control" name="name">
<option value="">Select One</option>
<option value="create_user">New user creation</option>
<option value="register_user">New user registration</option>
<option value="forgot_password">Forgot Password</option>
</select>
</div>
<div class="col-lg-6" id="email_tmpl"></div>
</div>
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Notification to</label>
<div class="col-lg-10">
<select id ="notification" class="form-control" name="notification">
<option value="">Select One</option>
<option value="User">User</option>
<option value="Site Admin">Site Admin</option>
</select>
</div>
</div>
if I selected the option "New user creation" in 1st combo box, then I selected "User" option in next combo box.
I want to show an error message when again select the "User" option under "New user creation". And only "Site Admin" can select under "New user creation" option.
This is my controller code.
public function save_email_tmpl()
{
$this->load->model("Email_templates");
$user_data = $this->session->userdata("logged_user");
$post_data = $this->input->post();
if($post_data != FALSE )
{
$check_name = $this->Email_templates->get_all_records
("",array("name" => $post_data["name"]));
if(count($check_name)< 2)
{
$data = array("name" => $post_data["name"],
"notification" => $post_data["notification"],
"cc" => $post_data["cc"],
"subject" => $post_data["subject"],
"email_template" => $post_data["email"]);
$this->Email_templates->insert_record($data,$user_data);
echo json_encode(array(
"status" => "success",
"msg" => lang("email_tmpl_added")));
}
else
{
echo json_encode(array(
"status" => "failed",
"msg" => lang("invalid_data")));
}
}
else
{
echo json_encode(array(
"status" => "failed",
"msg" => lang("invalid_data")));
}
}
I want to check the condition in this code.