I am new in PHP.
Please check the following code; multiple select is not working. Not sure why. Thanks in advance.
<form action="test.php">
<select name="fin[]" multiple ="multiple" class = "multiple_select" id = "selected_options_fin" style="height: 200px">
<option value="week_period">Week Period</option>
<option value="comments_approved">Comments Approved</option>
<option value="comment_replies">Comment Replies</option>
<option value="avarage_response_time">Average Response Time</option>
<option value="avarage_response_time_in_minutes">Average Response Time in Minutes</option>
<option value="commenter_visits">Commenter Visits</option>
<option value="percentage_of_new_visits">% New Visits</option>
<option value="number_of_goal_completions"># of Goal Completions</option>
<option value="conversions_rate">Conversions Rate</option>
</select>
<input type="submit" name = "submit1" Value = "SUBMIT" id="submit_fin">
</form>
<?php
if (isset($_POST['fin'])) {
$fin_array = $_POST['fin'];
$loop_count = 0;
foreach ($fin_array as $key => $value) {
$loop_count++;
echo 'Column $loop_count || Array Key = $key || Value = $value<br />';
}
exit();
}
Sorry for bothering
First line need to have
form action="test.php" method="post"
Related
I want to create a form using array that user can select a number and echoes the number's corresponding object name after submit. I don't know why this code does not work, could someone please teach me how to do it the right way :( Thank you so much for your time.
<form name="train" method="GET" action="test.php">
<select name="object">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] = "pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
$train = $_GET['obejct'];
echo "<p>I have $train!</p>";
}
?>
Thank you so much!
Looks like you're setting $train to the value of whatever the form passes for the "object" select field, and then echoing that. You would expect then to see a number between 0 and 8, or the word "all" print out, but your reference of the object key has the word "object" misspelled as "obejct", so my guess is you're getting nothing to print as the value of $train.
Either way, what you really want to do is print the value at the key in the $train array that corresponds with what was provided by the user. This means that once you've created your array, which functions as a map, you must select the item from the array that you want to print.
You also need to handle the "all" case or you will get an error.
Here's how it would look if you continue using the array option:
<form name="train" method="GET" action="test.php">
<select name="object">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] = "pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
if ($_GET['object'] != 'all') {
//Handle the non-all case
$value = $train[$_GET['object']]; //This references a key in your array, like $train[0]
echo "<p>I have $value!</p>";
} else {
//Handle the all case here
}
}
?>
For training purposes i need to make a function which tells me the 'travel cost' between 2 cities. The book tells me to type this function:
<?php
function travelcost($start, $destination)
{
$travelcost = array();
$travelcost[1] = array();
$travelcost[2] = array();
$travelcost[3] = array();
$travelcost[4] = array();
$travelcost[1][1] = 0;
$travelcost[1][2] = 30;
$travelcost[1][3] = 60;
$travelcost[1][4] = 90;
echo($travelcost[$start][$destination] . " Euro's");
}
?>
In addition i've created this form to ask for a start and a destination:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Start: <select name="start" value="true">
<option value="start[]">Amsterdam</option>
<option value="start[]">Utrecht</option>
<option value="start[]">Den Haag</option>
<option value="start[]">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="destination[]">Amsterdam</option>
<option value="destination[]">Utrecht</option>
<option value="destination[]">Den Haag</option>
<option value="destination[]">Rotterdam</option>
</select>
<p><input type="submit" name="calculate" value="Calculate"</p>
</form>
Followed by:
<?php
if(isset($_POST["start"])&& isset($_POST["destination"]))
{
travelcost($_POST['start'], $_POST['destination']);
}
?>
This gives me Undefined index: start[]
I know im doing it wrong, but i just can't see the logic in the function and the array. I assume the function is correct because it's right out of the book but i'm also not sure about that.
Can someone help me out?
This is wrong,
<option value="start[]">Amsterdam</option>
^ ^
It should be
<option value="start">Amsterdam</option>
or
<option value="Amsterdam">Amsterdam</option>
Same for all options in start and destination.
According to your function `travelcost(), your select should be
Start: <select name="start" value="true">
<option value="1">Amsterdam</option>
<option value="1">Utrecht</option>
<option value="1">Den Haag</option>
<option value="1">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>
I am trying to make a form submit.
Form is select a task, then show several rows with checkbox and select with values 1-10
If checkbox is selected is need to add values from select into database.
this is PHP code
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] = 'POST'){
if(isset($_POST['submita'])){
foreach($_POST['nota'] as $key => $value)
if (isset($_POST['boxes'])){
foreach($_POST['boxes'] as $key => $value2){
if(isset($value2)){
$nota = htmlent($_POST['nota']);
$box = htmlent($_POST['boxes']);
$task = htmlent($_POST['task']);
$db->insert(array(
"task" => $task,
"nota" => $value,
"box" => $value2,
),
"erp_notes");
}
else
{
echo 'Select';
}
}
}
}
}
HTML
<input type="checkbox" name="boxes[]" value="<?=$row['id'];?>"></input>
<?=$row['name'];?>
<select name="nota[]" >
<option value="">Select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
I try many time but nothing.
In the very first if you have = when you mean ==.
You haven't posted the entire HTML so I'll have to assume there is a submita element there that's being set. Your best bet is to do var_dump($_POST) to see what you are actually getting, and then build your code to match.
Your code is incorrect because it loops over every boxes for each nota, when what I suspect you want is to just check the checkbox that corresponds to that element.
You probably want something more like this:
if (isset($_POST['nota']))
{
for ($i = 0; $i < count($_POST['nota']); ++$i)
{
if (isset($_POST['boxes'][$i]))
$db->insert(array('task' => $task, 'nota' => $_POST['nota'][$i], 'box' => $_POST['boxes'][$i], 'erp_notes');
}
}
i was edited cod , but work like in comment from first answer
PHP
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['nota']))
{
for ($i = 0; $i < count($_POST['nota']); ++$i)
{
if (isset($_POST['boxes'][$i]))
$task = htmlent($_POST['task_id']);
$db->insert(array(
"nota" => $_POST['nota'][$i],
"date" => time(),
"box" => $_POST['boxes'][$i],
"task" => $task
),
"erp_note");
}
}
}
HTML
<form class="grid_12" action="" method="post" enctype="multipart/form-data">
<input type="checkbox" name="boxes[]" value="<?=$row['id'];?>"></input><?=$row['box_name'];?>
<select name="nota[]" multiple><option value="">select</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>
<input name="task" type="text" value="<?=$row['task_id'];?>" style="display:none;">
</form>
This is my list box sample code
<form id="myForm" action="somePage.php" method="post">
<p>myList<br>
<select name="select">
<option value="Option 1" selected>---</option>
<option value="Option 2">sel1</option>
<option value="Option 3">sel2</option>
<option value="Option 4">sel3</option>
</select>
</p>
</form>
But the problem is that i would like to fill the list with the result of a query. Leaving out for a moment the query, the real point is "How can i print the <option value= ..." programmatically so that with a for cycle i can fulfill the list? For example i thought something like this
<form id="myForm" action="somePage.php" method="post">
<p>myList<br>
<select name="select">
<?php
for(i;i < myQueryResultArray length;i++){
$counter = i;
echo <option value="Option $counter">$myArrayValue[i]</option>
}
?>
</select>
</p>
</form>
This is for sure wrong but that's the idea i had. It may be correct with proper syntax? Or better other ways? Thanks
You'd want a for loop like this
for($i = 0; $i < sizeof($myQueryResultArray);$i++){ //note the changes here
//declare variables with $
//sizeof() will return length
echo "<option value='Option $i'>$myArrayValue[$i]</option>"; //notice the quotes
}
$res = $db->query($query);
foreach($res as $item) {
?>
<option value = "<?=$item['key1']?>"><?=$item['key2'] ?></option>
<?php
}
I use select as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select option and store it into a variable for future use, in PHP?
Use this way:
$selectOption = $_POST['taskOption'];
But it is always better to give values to your <option> tags.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
You can access values in the $_POST array by their key. $_POST is an associative array, so to access taskOption you would use $_POST['taskOption'];.
Make sure to check if it exists in the $_POST array before proceeding though.
<form method="post" action="process.php">
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
process.php
<?php
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
if ($option) {
echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
} else {
echo "task option is required";
exit;
}
You can do it like this, too:
<?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch ($select1) {
case 'value1':
echo 'this is value1<br/>';
break;
case 'value2':
echo 'value2<br/>';
break;
default:
# code...
break;
}
}
?>
<form action="" method="post">
<select name="select1">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
<input type="submit" name="submit" value="Go"/>
</form>
for php8+ versions, you can use match expression:
$select = $_POST['select1'] ?? '';
$result = match ($select) {
'value1' => 'this is value1',
'value2' => 'this is value2',
default => 'unknown value',
};
echo $result;
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
$var = $_POST['taskOption'];
Depends on if the form that the select is contained in has the method set to "get" or "post".
If <form method="get"> then the value of the select will be located in the super global array $_GET['taskOption'].
If <form method="post"> then the value of the select will be located in the super global array $_POST['taskOption'].
To store it into a variable you would:
$option = $_POST['taskOption']
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php
Like this:
<?php
$option = $_POST['taskOption'];
?>
The index of the $_POST array is always based on the value of the name attribute of any HTML input.
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
try this
<?php
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>
-- html file --
<select name='city[]'>
<option name='Kabul' value="Kabul" > Kabul </option>
<option name='Herat' value='Herat' selected="selected"> Herat </option>
<option name='Mazar' value='Mazar'>Mazar </option>
</select>
-- php file --
$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);