In a form after submitting the data, if i want to edit form the data in the form should be from database i have to echo the selected value from the database if data is present else it has to show the options to select the values from another table where these options are present
here is the code
please correct it
<select name="ccname" class="form-control form-color">
<?php
if(isset($storyData)) {
echo '<option value="' . $storyData['ccname'] . '">' . $storyData['ccname'] . '</option>';
}
else{
echo "<option value="">CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'</option>';
}
?>
</select>
after working with concatenation operator finally debugged the code
here is the code
that i corrected
<select name="ccname" class="form-control form-color">
``<?php if(isset($storyData))
{
echo "<option value = '" . $storyData['ccname'] . "'>".$storyData['ccname']."</option>";
}
else{
echo "<option value='1'>CCname</option>";
foreach(getNames() as $ccname)
echo '<option value="'.$ccname.'">'.$ccname.'
</option>';
}
?>
</select>
Related
I want to make select options list with data from my database
I did something like this
<?php
if ($resultt->num_rows > 0) {
// output data of each row
while($row = $resultt->fetch_assoc()) {
?>
<select name="name" class="custom-select">
<?php
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
?>
</select>
<?php
}
} else {
echo "0 results";
}
?>
I see my output but every data is in seperated "box". I just want list to choose for example options1 or 2.
What I did wrong?
You need to take the <select> element outside the loop.
<?php
if ($resultt->num_rows > 0) {
echo '<select name="name" class="custom-select">';
// output data of each row
while($row = $resultt->fetch_assoc()) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo '</select>';
} else {
echo "0 results";
}
its happen because you put <select> inside loop so it create every time new <select>
just put your select outside loop like as follow
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='1'" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
You are nesting select elements inside each loop
<select name="name" class="custom-select">
<?php
while($row = $resultt->fetch_assoc()) {
echo "<option value='".$row['name']."'>" .$row['name']."</option>";
}
?>
</select>
And watch out for unnecessary single quote that will break your html:
"'>"
When I am setting same name in HTML 'select name' and set_rules name in form validation rules; it does not showing it is a required field.
My controller for validation as follows:
$this->form_validation->set_rules('cate', 'category name','trim|required|xss_clean|callback_select_validate');
function select_validate($abcd)
{
// 'none' is the first option that is default "-------Choose City-------"
if($abcd=="none"){
$this->form_validation->set_message('select_validate', 'Please Select Your Category.');
return false;
} else{
// User picked something.
return true;
}
}
if($this->form_validation->run() == false){
$this->index();
}
My HTML select form code as follows:
<select name="cate" >
<option value="none" selected="selected">Select Catagory</option>
<?php
foreach ($catagory_all as $value) {
if(!empty($project) && ($project->cat_id == $value->cat_id)){
echo '<option value="' . $value->id . '" selected="">' . $value->catagory . '</option>';
}
else{
echo '<option value="' . $value->cat_id . '">' . $value->catagory . '</option>';
}
}
?>
</select>
I've modified you coding... Please check with that.
View:
<select name="cate" >
<option value="" selected="selected">Select Catagory</option>
//Just specify value as empty. From that we can easily use required.
<?php
foreach ($catagory_all as $value) {
if(!empty($project) && ($project->cat_id == $value->cat_id)){
echo '<option value="' . $value->id . '" selected="">' . $value->catagory . '</option>';
}
else{
echo '<option value="' . $value->cat_id . '">' . $value->catagory . '</option>';
}
}
?>
</select>
Controller:
No need of callback. If the selection value is empty, It will return required error!
You can only specify following this,
$this->form_validation->set_rules('cate', 'category name','trim|required|xss_clean');
You call callback_select_validateselect_validate it means you code looking for function select_validateselect_validate in your code
so You have to change your function name from
function select_validate($abcd)
TO
function select_validateselect_validate($abcd)
OR remove select_validate from your callback
$this->form_validation->set_rules('cate', 'category name','trim|required|xss_clean|callback_select_validate');
I am making a page that sends data in the POST and refreshes when you click on a select list option. The data is retrieved from a database. I am using the this.form.submit() function to send the variable when you click on an option. However, for some reason, it doesn't send the variable in the value="" of the option box but the text in between the ><. Below is the piece of code I thought was relevant:
echo '<form method="post">';
echo "<select onchange='this.form.submit()' id='chose_category' name = 'chose_category'>";
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option "value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
echo '</select>';
echo'</form>';
In this case, the variable in $row['name'] is send as a POST variable, instead of $row['number']. I have checked this by printing the POST variable. Is there any way to send $row['number'] here, but still displaying $row['name']?
Change
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option "value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
To:
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
Your current produces something like :
<select onchange='this.form.submit()' id='chose_category' name = 'chose_category'>
<option "value="number">name</option>
</select>
You need to remove the double-quote.
I create this code to display a list of countries, I would retrieve the selected country but it does not work
<?php
include 'includes/combo.php';
?>
<?php
echo "<label for='pays'>Pays*</label>
<select id='pays' name='pays '>";
while ($row = mysql_fetch_array($combo)) {
echo "<option>$row[0]</option>";
}
echo "</select> ";
echo "<option>$row[0]</option>";
?>
and recover with a
if(isset($_POST['pays']))
$pays=$_POST['pays'];
else
$pays="";
echo $pays;
but the pays value is not recouped
echo"<option value=" . $row[0] . ">$row[0]</option>";
To retrieve the value of your option
Try this
<?php
include 'includes/combo.php';
?>
<?php
echo '<label for="pays">Pays*</label>
<select id="pays" name="pays">';
while ($row = mysql_fetch_array($combo)) {
echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
}
echo '</select>';
?>
You have one space after name in select
And your option hasn't value attribut
I am using two selects, the one as the category and the second one as subcategory.
I wish to make the second select menu to be determined by what option was chosen in the second select menu.
Here is the html/php code.
<select name="category" id="category">
<?php
$options = array("Stationary", "Bag", "Paper");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " Selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
</select>
<select name="subcategory" id="subcategory">
<?php
$options = array("Pencils", "Ruler", "Pens");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
<?php
$options = array("Small", "Large", "Paper");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
<?php
$options = array("Notepad", "Plain ", "Colour");
foreach ($options as $option) {
echo '<option value"' . $option . '"';
if(in_array($option, $category)){
echo " selected";
}
echo ">" . ucfirst($option) . "</option>";
}
?>
</select>
I want it so when and you select one option from category it will it show the correct options to be shown in the <select>.
I'm not sure if you can do this, but if anyone has an idea or know how to do this your help would be helpful.
If I understand your question right what you want to do is to select the sub category depending on the category.
1st load all the categories into the category select box.
2nd when the category option is selected retrive the value from select box(category_id) and make an AJAX call to get the sub categories for the selected category.
3rd populate them to the sub category select box.
Updated answer as requested
jQuery Ajax POST example with PHP
If you have any issues let me know