How I can send customer id from view to controller - php

I want to send customer id from view to controller when I select customer name from dropdown list it will send customer's id to controller.
here is my code of view file 'customerlifecycleanalytic.php'
<select class="form-control selectpicker customerfilter">
<option value=''>Select Customer</option>
<?php
if (isset($customername)) {
foreach ($customername as $customernames) {
echo '<option value="' . $customernames['id'] . '" >' . $customernames['firstname'].'&nbsp'. $customernames['lastname']. '</option>';
}
}
?>
</select>
here is my controller code 'Customerlifecyclecontroller'
public function actionCustomerlifecycleanalytic() {
$customername = Customer::model()->findAll("store_id='" . Yii::app()->session['store_id'] . "'");
$customerlifecycle = CustomerLifecycle::model()->findAll();
$this->renderPartial('customerlifecycleanalytic', array( 'customername' => $customername, 'customerlifecycle' => $customerlifecycle), false, true);
}

Try like this..Its working. Hope this will help.
View:
//view file code. Just add Onchange select attribute like below:
<select onchange="if(this.value) window.location.href='http://yourdomain.com/yourcontroller/youraction/'+this.value" class="form-control selectpicker customerfilter">
<option value=''>Select Customer</option>
<?php
if (isset($customername)) {
foreach ($customername as $customernames) {
echo '<option value="' . $customernames['id'] . '" >' . $customernames['firstname'].'&nbsp'. $customernames['lastname']. '</option>';
}
}
?>
</select>
Controller:
// controller file code. just add a variable parameter to hold the id in action method.
public function youaction($id){
echo "id : ".$id; // customer id
}

Related

How to retain user generated data on form

Hello guys I'm new to OOP PHP and I want to imitate the retaining of user generate data in form from procedural
here is my code:
<?php
class View_LType extends Config{
public function view_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo "
<tr class='table-head'>
<td class='numbers'>$data[leave_id]</td>
<td>$data[leave_code]</td>
<td>$data[leave_name]</td>
<td class='small'>$data[leave_description]</td>
<td><a href='#'></a></td>
</tr>";
}
}
public function select_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo '<option value="' . $data['leave_name'] . '">' . $data['leave_name'] . '</option>';
}
}
}
html page:
<div class="form-group">
<label for="ltype">Leave Type</label>
<select name="ltype" id="ltype" name="ltype" required >
<option>Select leave...</option>
<?php $view_leave->select_ltype(); ?>
</select>
</div>
$data[leave_name] is what the user will select what I want is to echo selected when the user select its prefered leave name. please help me guys
If the current $data['leave_name'] is the same as the submitted "ltype", then select that option:
echo '<option value="' . $data['leave_name'] . '"' . (($data['leave_name'] == $_REQUEST['ltype']) ? ' selected' : '') . '>' . $data['leave_name'] . '</option>';

How to avoid page reload when i select dropdown list ?? and also how to get selected values php

I am stuck with small problem.
when i change my drop down list the page is getting reloaded.
Can any one help me .
This code is used to retrieve userid, based on userid get all the application id form drop down and populate it into dropdown list.
If i select value in drop down i am storing selected value
Here is my code.
<?php
$selected = " ";
function get_options($select) {
global $wpdb;
$user_ID = get_current_user_id();
echo $user_ID;
$sql = $wpdb->get_col("select Application_ID from wp3_cteTest where $user_ID = userid");
echo $sql;
//print_r($keysql);
$options = "";
while (list($k, $v) = each($sql)) {
if ($select == $v) {
$options.='<option value="' . $v . '" selected>' . $v . '</option>';
} else {
$options.='<option value="' . $v . '">' . $v . '</option>';
}
}
return $options;
}
if (isset($_POST['applications'])) {
$selected = $_POST['applications'];
echo $selected;
}
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
<?php $default_state = 'please Select your id'; ?>
<select name="applications" onchange=this.form.submit()>
<option value=' ' ><?php echo $default_state ?></option>
<?php echo get_options($selected) ?>
</select>
</form>
</body>
</html>
I Tried removing form, if i remove form drop down doesn't work, I wont get selected value.
Then what is wrong??
Thanks in advance!
You've got onchange=this.form.submit() in your select tag, so when you change the option selected javascript is submitting the form.
Just change...
<select name="applications" onchange=this.form.submit()>
...to...
<select name="applications">
So if you just need the selected id without submit the form then replace this line:
<select name="applications" onchange=this.form.submit()>
with this :
<select name="applications" onchange="getval(this)">
and Add little script :
<script type="text/javascript">
function getval(sel) {
alert(sel.value);
}
</script>

Post dropbox selected values numerical id -codeigniter

I'm new to codeigniter and its developing. I have used html dropbox inside the form. when I press the submit button I want to retrive and post id of selected drop box index value. How do I do that? please find the code I used.
<?php
echo form_open('homepage/askquestionview');
?><div class="form-group">
<select name="cat" id="cato" onchange="activate_match()">
<?php
foreach ($catogories as $cat) {
echo'<option value="' . $cat . '" id="cato" >' . $cat . '</option>';
}
?>
</select>
<input type="submit" class="btn btn-success btn-block" value="Post Your Question" id="postQuestion">
</p>
<?php echo form_close(); ?>
homepage/askquestionview function
public function askquestionview() {
$data = array(
'Student_Email' => $this->input->post('cato'),
);
var_dump($data);
}
output
array(1) { ["Student_Email"]=> bool(false) }
Your select input name is 'cat' not 'cato'
<select name="cat"
Try a var_dump on $this->input->post() :
public function askquestionview() {
var_dump(this->input->post());
}
Tips, use form_helper to create your input : form_dropdown()
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
I don't know much about the data structure of codeigniter, but you'll probably have to modifiy for loop like this:
foreach ($catogories as $id => $cat) {
echo'<option value="' . $id . '" id="cato" >' . $cat . '</option>';
}

selected value from the listbox in php

I want the value which I select from the listbox and want to store in php variable because I want to use that variable for further use. Please help..
<form name="myform" method="post">
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
</form>
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">
access that values selectedvalue(val) in parameter
get that selected value and store it in input type= hidden as u need it for further use..
If you want to store into a php-variable, you must post the form. I've added a submit-button below. You also must speciy an action to where the form is submitted to: action="whatever.php"
<form name="myform" method="post" action="whatever.php"> <!-- added action here -->
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
<!-- added submit button -->
<input type="submit" value="ok" />
</form>
Create a php-file called whatever.php and in this file, store the value into $cust_name by doing this:
<?php
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>
If you want to post the form without having to reload the page, you must use something called ajax.
This works for me.Try this example.
public function getAptTypesBySelectedKy($valKy) {
$stmt = "SELECT ap_typ_ky, ap_typ_desc FROM apt_types WHERE ap_stat=1 order by ap_typ_desc";
try {
$con = DataHandler::connect();
$values = $con->prepare($stmt);
if ($values->execute()) {
echo '<select class="form-control" name="type" id="apt_types_slc">';
while ($row = $values->fetch(PDO::FETCH_ASSOC)) {
if ($valKy === $row['ap_typ_ky']) {
echo '<option value="' . $row['ap_typ_ky'] . '" selected>' . $row['ap_typ_desc'] . '</option>';
} else {
echo '<option value="' . $row['ap_typ_ky'] . '">' . $row['ap_typ_desc'] . '</option>';
}
}
echo '</select>';
}
} catch (PDOException $ex) {
echo 'Error on apartment types';
$error = $ex;
print_r('<pre>' . $ex->getCode() . '</pre>');
print_r('<pre>' . $ex->getMessage() . '</pre>');
}
}
In your html form page.
$__typKy = $_RA['typeky'] //this line get the selected value from database and set as parameter to the function getAptTypesBySelectedKy()
<?php
$__typKy;
if (isset($_RA)) {
$__typKy = $_RA['typeky'];// you need to find this key from database.
//this is the selected value key of `<select>`
}
$var = new DataHandler();
$var->getAptTypesBySelectedKy($__typKy);
?>

PHP foreach loop to print out html listbox options

Here goes my first post.
I am currently pulling two fields out from my MySQL database with a fetch all and am trying to get the data in those fields to become options in a listbox.
This is my code:
<fieldset class="contact">
<legend>Select a Band</legend>
<!-- Drop List -->
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '">' . $name . '</option>';
}
echo $options;
?>
</select>
and here is the result of echoing out $options:
<option value="1">Rise Against</option><option value="2">Alter Bridge</option><option value="3">Falling In Reverse</option><option value="4">Saosin</option><option value="5">Pennywise</option><option value="6">The Killers</option><option value="7">Thrice</option><option value="8">Four Year Strong</option>
I want to have the foreach loop print out the code that will be the options for my listbox lst1 but it is currently not working. Any ideas as to why?
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"]; ?>
<option value = "<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
Try this code. In your code '&lt' and '&gt' makes html entity format. So html does't accept it as a tag.
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '>' . $name . '</option>';
}
echo $options;
?>
</select>

Categories