How to preserve space in HTML select option list? - php

<select name='test'>
<option value='north' india>north india</option>
<option value='goa'>goa</option>
</select>
If I select "north india" in the list PHP echos only "north". Why is that?
This HTML is generated by the PHP code below:
$cond_city= "WHERE state=? GROUP BY cityName ORDER BY cityName ASC";
$data_city= array($state);
$select_city=$this->select_rows(TABLEPREFIX.'region','*',$cond_city,$data_city);
while($arr_city=$select_city->fetch(PDO::FETCH_ASSOC)){
echo '<option value='.$arr_city['cityName'].'>'.$arr_city['cityName'].'</option>';
}

In your code, value is equal to 'north' because you misplaced your quote:
<option value='north' india>north india</option>
You need this instead:
<option value='north india'>north india</option>
To get this result, you will need to add quotes in your PHP code, like this:
<?php
$cond = "WHERE state=? GROUP BY cityName ORDER BY cityName ASC";
$data = array($state);
$select=$this->select_rows(TABLEPREFIX.'region','*',$cond_city,$data_city);
while ($arr_city=$select_city->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$arr_cit.' '.$sel_cnt.' >'.$arr_city['cityName']."'</option>';
}
?>

Because you have set option's value as 'north'. If you want 'north india'as whole then replace below code.
<select name='test'>
<option value='north india'>north india</option>
<option value='goa'>goa</option>
</select>

it's happening because when you are submitting your data, you have value for the name of the input (select in your case). To get the desired value you should indicate it in the value attribute of the select.
<select name='test'>
<option value="north india">North India</option>
</select>

Related

PHP search with few parameters

I have an query that I am trying to do search with multiple parameters to sort my data response from sql.
The query code :
$sql = "SELECT * FROM `apartments` WHERE `building_num`='{$i}' ORDER by `apartment_num` ASC";
The search form :
<form method="GET">
<input type="hidden" name="page" value="price">
<select class="styled-select2" name="type">
<option value="0">Apartment type</option>
<option value="דופלקס">Duplex</option>
<option value="דירה">Apartments</option>
<option value="דירת גן">דירת גן</option>
</select>
<select class="styled-select2" name="rooms">
<option value="0">rooms</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="5">5</option>
</select>
<select class="styled-select2" name="price">
<option value="0">Price</option>
<option value="700000-800000">700,000-800,000 ₪ </option>
<option value="800000-900000">800,000-900,000 ₪</option>
<option value="900000-1000000">900,000-1,000,000 ₪</option>
<option value="1000000-1100000">1,000,000-1,100,000 ₪</option>
<option value="1100000-1200000">1,100,000-1,200,000 ₪</option>
<option value="1200000-1300000">1,200,000-1,300,000 ₪</option>
<option value="1300000-1400000">1,300,000-1,400,000 ₪</option>
<option value="1400000">1,400,000 up</option>
</select>
<input type="hidden" name="action" value="search">
<button type="submit">Search</button>
</form>
There are three parameters but they are not required, could the customer only use 'apartment type' AND 'Price' OR only one parameter, I tried for few hours to write query with no success, Any one can help ?
// Vars on table apartments
// Rooms - > Rooms
// Price -> price
// Apartment type -> type
1st of all, there's no direct link between and query (or queries) and tge options you're giving to the customer...
If you want to then you can allow the customer to fill some of the fields and if you don't then no... it's a matter of decision, from which derives the development
There's missing example code, so I can obly answer regarding the query and add some remarks
Since you've given specific select options, then you should use it for specific searches
I would also use some validations on the $_POST information returned
Regarding the query, I need to assume that all of the options are saved in the apartments table, under that, then the query should be something like this :
$sql = "SELECT * FROM `apartments` WHERE
rooms = $rooms
OR `type` = $type
OR (price BETWEEN *$range_min* AND *$range_max*)
ORDER by `apartment_num` ASC";
If you want all parameters combined you should use AND instead of OR
and, again, you can decide you want both and use different queries for each scenario the user givea
In this case you have to build dynamic query as below :-
$sql = "SELECT * FROM `apartments` WHERE ";
$condition = "";
$condition .= ($rooms=='')?"":" or 'rooms'= $rooms ";
$condition .= ($type=='')?"":" or 'type'= $type ";
$condition .= ($price=='')?"":" or prince bewteen 'range_min' AND 'range_max'";
$sql .= $condition;
$sql .= " ORDER by `apartment_num` ASC";
In this way you can make all three filters free to select or not.

Value of Multiple select input text field in CodeIgniter controller

How can I get the value of a multiple select input field in CodeIgniter controller?
I want a multiple select input field shown here
I have added below html code.
<select class="js-example-basic-multiple multiple_selection form-control" name="student[]" multiple="multiple" >
<option value="1">Student1</option>
<option value="2">Student2</option>
<option value="3">Student3</option>
<option value="4">Student4</option>
<option value="5">Student5</option>
<option value="6">Student6</option>
</select>
I am using given code to fetch the value of this input field. But I didn't get the value.
$studentname = array();
$studentname =$this->input->post('student[]');
echo 'studentName:'.$studentname;
Do you have any suggestions?
Your values will be posted in an array:
$studentNames = $this->input->post('student');
You can then access each value using loop:
foreach($studentNames as $name){
echo "Student name is $name";
}
there is no need to use [] while you are echoing post value, you already define in name='student[]';
$studentname =$this->input->post('student');
echo "<pre>";
print_r($studentname);
echo "</pre>";
for naming sake make it student students since its a multislect
<select class="js-example-basic-multiple multiple_selection form-control" name="students[]" multiple="multiple" >
<option value="1">Student1</option>
<option value="2">Student2</option>
<option value="3">Student3</option>
<option value="4">Student4</option>
<option value="5">Student5</option>
<option value="6">Student6</option>
</select>
in controller:
//get the post values of students (if you dump the post you will see that $_POST['students'] is an array
$students = $this->input->post('students');
https://www.codeigniter.com/user_guide/libraries/input.html
//will contain the values of the selected students ex. var_dump($students) will produce [1,2,4];
//you will need to loop over students since it is an array
foreach($students as $id){
echo "Student id is {$id}";
}

getting the value in dropdown and use it as table name when inserting

What is wrong with this? any help will be appreciated. its not inserting. I need to specify which table will be inserted to depend on the value of the dropdown thats why i put insert into $newCandidatePosition. It's working when i put insert into president. But when i put insert into $newcandidateposition it's not working. What should i do?
This is my dropdown list
<select name="position" id="position">select
<OPTION value="select">select
<option value="president">PRESIDENT</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
and this is the submit
if(isset($_POST['Submit'])){
$id = $_POST['name'];
$newCandidatePosition = $_POST['position'];
$query=mysql_query("select * from voting_tbCandidates where student_id='".$id."'")or die(mysql_error());
$duplicate=mysql_num_rows($query);
if($duplicate==0){
$sql = mysql_query("INSERT INTO '$newCandidatePosition'(`stud_id_no`,`stud_name`, `lname`,`stud_grade`,`stud_img`,`stud_section`)"." SELECT `stud_id_no`,`stud_name`, `lname`,`stud_grade`,`stud_img`,`stud_section`"." FROM sid_senior_high_table WHERE id_no='$id'");
}else{
}
}
Try the following way:
$sql = mysql_query("INSERT INTO
'" .$newCandidatePosition. "'
(`stud_id_no`,`stud_name`, `lname`,`stud_grade`,`stud_img`,`stud_section`)".
" SELECT `stud_id_no`,`stud_name`, `lname`,`stud_grade`,`stud_img`,`stud_section`".
" FROM sid_senior_high_table WHERE id_no='$id'")";
Please close option tag properly
<select name="position" id="position">
<option value="select">select</option>
<option value="president">PRESIDENT</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
first thing do not use table name as "select" .. second thing '$newCandidatePosition' change $newCandidatePosition remove single code
//echo "INSERT INTO $newCandidatePosition (`stud_id_no`)"." SELECT `stud_id_no`"." FROM test2 WHERE id_no='$id'";
$sql = mysql_query("INSERT INTO $newCandidatePosition (`stud_id_no`)"." SELECT `stud_id_no`"." FROM test2 WHERE id_no='$id'");
echo mysql_insert_id();
above is my demo code it's work for me

Creating select list from database

I am making select list from database table everything works fine only issue is in option value if the value consist of two words like "New York" it only returns "New". Here is the code
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['CityName']?>><?=$row['CityName']?></option>
<? } ?>
</select>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
The " " .
You'll have to surrond the value in "", the HTML code thinks the York part is just another attribute of the option tag, so:
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
You haven't included the "" while giving the values. if you look at the html created for the select box by your code is something like
<option value="new" york>new york</option>
correct solution is given below, just included the quotation while giving value of option tag.
<? $country=intval($_GET['country']);
include('connect-db.php');
$query="SELECT CityName FROM cities WHERE CountryId='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option value="">Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['CityName']?>"><?=$row['CityName']?></option>
<? } ?>
</select>
Just use "" for the value of value same as you write HTML tag.

optgroup get in php label associated

I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array

Categories