I am trying something in my examination script. So I'd like to ask a question about my problem.
I have combobox in the exam form. like this :
<option value="<?PHP echo $answer_list['answer_value']; ?>"><?PHP echo $answer_list['answer_detail']; ?></option>
I must put another value in this. it will be like this I think:
<option value="<?PHP echo $answer_list['answer_value']; ?>,<?PHP echo $answer_list['answer_id']; ?>"><?PHP echo $answer_list['answer_detail']; ?></option>
I must save these values to the database, but I really don't know how can I save these values to the database when I exploded these values to the different columns in one table.
I tried something with explode function but I couldn't do it well.
So when I posted these values from form, I tried this function but I couldn't save them to the database.
$answers = $_POST['answers'];
$answer_explode = explode(",",$answers);
$answer_id = $answer_explode[0];
$answer_value = $answer_explode[1];
this gets only first and second value in the array. But I must make 2 variables like this :
before comma
$answer_id = values before comma
$answer_value = values after comma
how can I do that?
Name your <option> in HTML with the array syntax <option name="answers[]"> and you can access them in PHP POST as an array.
Related
I am new to stackoverflow and am after a little help. I have already tried searching for what I am about to ask, but cannot find any relevant topics, so here goes:
I have a PHP page, that gets a list of venues and it's town/city from a table within a MySQL database, which I have then concatenated to populate a dropdown, e.g. each <option> will display "[venue], [town/city]".
What I am trying to do is when the user selects one of the options, I want to store the [venue] and [town/city] as separate fields in another table within the MySQL database.
I would really appreciate any help.
More details might be needed. For instance do you need to know how to insert records on database? or are you also having trouble getting values of option tag?
But from what I understood:
First remember to set value attribute of option tags:
<option value="venue,town">venue,town</option>
Then after submitting the form, you can slice the returned string.
Assume you stored "venue,town" inside a variable named $str
$results = explode(",",$str);
$results will be an array with two elements. $results[0] contains "venue" and $results[1] contains "town"
I am not sure how much it helped but you can give more details and I'll get back
I think the best way is to put the datarecord ids (primary key value) into the value attribute of the <option> tags, then on the server side requery the data of the selected id from the database. This way your form can't be manipulated with unwanted data.
To be more clear, I would do it like in this simplified semi pseudocode:
<?php
$action = isset($_GET['action']) ? $_GET['action'] : null;
if($action == "save") {
$id = $_POST['venue'];
if(!empty($id)) {
/* fetch data belonging to $id from database and then save venue and town/city
as separate fields in another table */
}
}
/* fetch all data for the selectbox from the db and store it in $data */
?>
<form action="?action=save" method="post">
<select name="venue" size="1">
/* iterate through $data and create $id and $caption */
<option value="<?php echo $id; ?>"><?php echo $caption; ?></option>
/* iteration end */
</select>
<input type="submit" value="save" />
</form>
I need to store all the items selected from a dropdown box inside array. I have only one form now with the select dropdown list. So each time I select an item from the list and submit the form it overwrites the previous item.
How do I make it work like each time it submits the id will be stored so that I can display all the items selected?
//this is the select dropdown for addon item
<select name="addon">
<?php
mysql_select_db($database_bumi_conn, $bumi_conn);
$query="SELECT * FROM tbl_addons WHERE status=1";
$result=mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$a_id=$row['addOns_id'];
$a=$row['addOns'];
?>
<option value="<?php echo $a_id?>"><?php echo $a;?></option>
<?php
}
?>
</select>
//And this is how I store the id
$addon_id=$_POST['addon'];
//edited with session
$_SESSION['option']=array();
$_SESSION['option'][$addon_id]=array('qty'=>$qty,'date_1'=>$date_1,'date_2'=>$date_2);
print_r($_SESSION['option']);
foreach($_SESSION['option'] as $option=>$value)
{
echo $option.'=>';
foreach($value as $val)
{
echo $val;
}
}
You could use SESSION' variables to store all the ID :
session_start();
// Rest of your code here
// $addon_id=$_POST['addon']; Becomes :
if (!in_array($_POST['addon'], $_SESSION['addons']))
$_SESSION['addons'][] = $_POST['addon'];
Edit: Not sure about your edit with sessions. You're resetting $_SESSION['option'] everytime, losing previous addon_id values
Ignoring the fact that you're using a deprecated, inherently un-secure and unmaintained extension, you need to use the multiple attribute on your <select> element and let PHP know that it will be receiving an array of values by using the [] suffix on the element name. To summarise...
<select name="addon[]" multiple>
<?php foreach($collection as $val => $label) : ?>
<option value="<?= htmlspecialchars($val) ?>"><?= htmlspecialchars($label) ?></option>
<?php endforeach ?>
</select>
The PHP variable $_POST['addon'] will then contain an array of selected values (when the form is posted of course).
I am looking to populate a drop down field with a list of names from a database, and when an option is selected from that drop down, it will post all its values in the row, belonging to that chosen option.
This is probably hard to describe/understand, so to help illustrate, this is my table row:
I then proceed to populate the dropdown, and associate its value to whatever the selected option is posted
//////db_conx is db connection //////main_meal is table name
<form action="#" method="post">
<?php
$dropdown = $db_conx->query("SELECT * FROM main_meal") or die ("somethings broken");
while($array[] = $dropdown->fetch_object());
//echo '<option value ="'.$record['Mname'].'">'.$record['Mname'].'"</option>';
array_pop($array);
?>
<select name="changeCal">
<?php foreach($array as $option) :?>
<!--//get chosen value in drop down, and get its calories-->
<option value="<?php echo $option->calories;?>"><?php echo $option->Mname; ?></option>
<?php endforeach; ?>
</select>
This works great for one value, such as calories, in the above code, but I need more values.
For example, if choose Healthy egg and chips, the value will post 218, as the loop only associates calories and names at the moment.
I attempted various things, like this post:How to get multiple values from a single <select> variable in HTML/PHP?
But the foreach errors.
How can I something similar to what I have done, but store multiple values from one chosen option?
Thank you
Well, I think I understood your problem, but I think that the way you want to use is not usable, maybe I recommend that you put the id in the value of the option in the < select> and then with php you can get all the data from the data base.
For me that is the best way, but if you want do it like the example that you show, you can make an string, for example:
<option value="id:5_calories:258_protein:11g">Healthy eggs & Chips</option>
with your php should look like:
echo '<option value="id:'.$option->id.'_calories:'.$option->calories.'_protein:'.$option->protein.'">'.$option->Mname.'</option>';
you can make bigger the string with others values that you want to put.
In the backend when you send the select you can catch the data with a:
$myArray = explode("_", $_POST["changeCal"]);
Then you will get an array with values like:
$myArray[0]; // id:5
$myArray[1]; // calories:258
$myArray[2]; // protein:11g
Then if you need for example the calories you can make an explode like:
$calories = explode(":",$myArray[1]);
And you will have:
$calories[0]; //calories
$calories[1]; //258 <= Here are the calories.
Maybe if you want to do it of that way, this can be the easiest way, but I recommend send the ID.
Let me know if you need more help. Regards, Have a nice day.
I have created and html form which have a drop down list.
This drop down list is populated from database.
<select name="classes">
<?php
foreach() {
?>
<option value="<?php echo $id ?>"><?php echo $name ?></option>
<?php
}
?>
</select>
Now I want to get the $id and $name both. How will I do this?
I have tried this
echo $_POST['classes'];
But it only displays the $id of the select item. And I want $id and $name both.
You can't. One possibility would be placing both infos inside the value attribute, and then separating them back again with php (by using a delimiter):
<option value="<?php echo $id.'|'.$name; ?>"><?php echo $name ?></option>
In PHP:
$datas = explode('|',$_POST['classes']);
$id = $datas[0];
$name = $datas[1];
But that's not how the system is meant to be. Usually the $name would be used only as a "friendly" info for the user, cause the value might sometimes just be an INT and user won't understand what that int refers to, so we give him a word description in order to choose an option: but what you would only care of is that value indeed, which you can always use to get again the description that comes along with it (by a search to the database, for ex.)
As far as I know, when you submit that form, only the value is going to be carried over. If there is no value then the inner HTML becomes the value.
What I'd do is:
<select name="classes">
<?php
foreach($classes as $id => $name) //i'm guessing here, is this what you meant?
{ ?>
<option value="<?php echo $id.'|'.$name ?>"><?php echo $name ?></option>
<?php } ?>
</select>
In case you are not familiar, the period is the concatenation operator. Think of it as glue for pieces of a string. So I'm gluing $id to the left of "pipe" and then gluing $name onto the end of that.
Then in your handler, use split or explode to separate the two values on the pipe.
Actually, I'd do it a little different, echoing more and going in and out of php/html less, but I tried to leave your code intact as much as possible.
append name to Id and pass it as value,,and explode name from the other end
You want is and name both so while storing option value ,
put like this
<option value="<?php echo $id."_".$name;?>"><?php echo $name?></option>
and on posting data just explode the value you will get both is and name
Sending form means only sending 'value' for select field (treat 'name' in option as a label only). You can simply fetch 'name' from database when you have 'id' while handling form submission.
I basically need to create a value by default for a drop down menu on a PHP page that will use the value previously selected and stored in the database.
For example, lets just say I have the value '3' stored in a database column. I want to use this number as the default value for a drop down menu where the <option value = "3">Good</option>. Is there a simple solution to this problem?
Or do i literally need to loop through the values until it makes a value?
Thanks.
I usually do this
<?php
$sel = 'selected="selected"';
$current_whatever = 5;
?>
<option name="whatever">
<?php foreach($list as $listItem): ?>
<option value="<?=$listItem->id?>" <?=($listItem->id == $current_whatever)?$sel:''?>><?=$listItem->name?></option>
<?php endforeach; ?>
</option>
I'm using an inline if statement to check each one :) Looks reasonably tidy.
Assuming you're using database objects, you get the idea if you're not though :)
<?php foreach($options as $key=>$option){?>
<option value='<?=$key?>' <? echo $key==$selected?"SELECTED":"";?> ><?=$option?></option>
<? }?>