The following code is the HTML from a file that I am trying to out put multiple selections by clicking the submit button:
<form id="eg6b" action="example-6.php" method="post">
<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport []" size="4" multiple>
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
<input type="submit" value="submit"></p>
</form>
The following code is the PHP file to obtain multiple Sports:
<?php
foreach($_POST["favsport"] as $val) {
echo "<p>You chose $val </p>";
}
?>
I just cant find the error. If I run this code it gives me an error that the favsport is undefined and that the argument supplied to the foreach loop is invalid. I have messed around with it alot but now I am just tired.
Change favsport [] to favsport[].
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="eg6b" action="test.php" method="post">
<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport[]" size="4" multiple>
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
<input type="submit" value="submit"></p>
</form>
</body>
</html>
<?php
foreach($_POST["favsport"] as $val) {
echo "<p>You chose $val </p>";
}
?>
You have only little typo mistake as far i can see which is at below like
<select id="sport" name="favsport []" size="4" multiple>
replace above line with below line
<select id="sport" name="favsport[]" size="4" multiple>
ERROR:
The error is in the name attribute which is having extra white space like
name="favsport**here you having one space** []" and it must be like name="favsport[]" .
Hope this will solve your problem.
:)
I have the following form but would like to add a condition so that only managers can see 'clear' option.
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<option value='clear'>Remove the ticket</option>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
Something like: if(havePriv('grp_mgr'))
<?php if(havePriv('grp_mgr')) : ?>
<option value='clear'>Remove the ticket</option>
<?php endif; ?>
Does the trick
You can do this:
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<?php if(havePriv('grp_mgr')){ ?>
<option value='clear'>Remove the ticket</option>
<?php } ?>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
So "Remove the ticket" will only displayed if the condition is true.
You can say :
if(havePriv('grp_mgr'))
{
echo "<option value='clear'>Remove the ticket</option>";
}
how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');
I am new in PHP. I have no experience on Object oriented PHP. I have experience only in Raw PHP.
I am trying to inserted the value of my drop down box to the database. This is my code given here.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT * INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)"
or die (mysql_error());
$result= mysql_query($sql);
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
<form method="post">
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
<br></br>
</form>
<form method="post">
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post" action="">
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<input type="submit" name="submit" value="submit">
</form>
<br><br>
</form>
</html>
I have gotten $_post['roll'],$_post['first_exam'] these error.
You had a lot of mistakes in your code, here is a full working example:
HTMl & PHP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Divix Help</title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "
INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)
";
//echo $sql;
$result = mysql_query($sql);
if ($result){
echo 'ok doen';
} else {
echo 'dont';
}
}
?>
<form method="post">
<table>
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
</table>
<br></br>
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
As mentioned by others you had too many <form> tags, remember that you only need 1 form tag in order to send multiple fields via POST or GET method.
You had mistakes in SQL (do not put INSERT * INTO, star sign goes only into SELECT statements)
Broken HTML structure, like missing </body>, </html> tags
And last but not least or die (mysql_error()); you put that against execute or connect function of mysql not against strings.
There is only a simple mistake there. Instead you made many , just make one form & add all select into it...
Something like this:
<form method="post">
code here
</form>
There are few things I would like to point out.
Your include statements should always be at the top of your php document, it's a good programming practice.
Use require_once instead of include because it prevents the file from being included more than once in your php document.
Create and use a Database class to perform all your database operations.
Always sanitize your data using real_escape_string function before inserting to prevent SQL injection.
Always close your database connection at the end, it's a good programming practice.
And last but not the least, learn HTML and PHP properly.
Here's the complete code,
<?php
// instead of include, use require_once
require_once('db.php');
// create and use Database class for all your database operations
class Database{
public static function open_connection($host, $username, $password, $dbName){
return new mysqli($host, $username, $password, $dbName);
}
public static function check_connection(){
if(!mysqli_connect_error()){
return true;
}else{
return false;
}
}
public static function execute_query($connection,$query){
return $connection->query($query);
}
public static function close_connection($connection){
$connection->close();
}
}
?>
<?php
if (isset($_POST['submit'])){
$connection = Database::open_connection(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if(Database::check_connection()){
// sanitize your input data
$roll = $connection->real_escape_string($_POST['roll']);
$fifth_exam = $connection->real_escape_string($_POST['first_exam']);
$second_exam = $connection->real_escape_string($_POST['second_exam']);
$third_exam = $connection->real_escape_string($_POST['third_exam']);
$fourth_exam = $connection->real_escape_string($_POST['fourth_exam']);
$fifth_exam = $connection->real_escape_string($_POST['fifth_exam']);
// now insert into database
// instead of taking id as NULL, take id as AUTO_INCREMENT
$query = "INSERT INTO table(roll_no, first_exam, second_exam, third_exam, fourth_exam, fifth_exam) VALUES ($roll, '$first_exam', '$second_exam', '$third_exam', '$fourth_exam', '$fifth_exam')";
if(Database::execute_query($connection,$query)){
echo "record is successfully inserted";
}else{
echo "error: record could not be inserted";
}
}else{
echo "database connection failed";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Some Title</title>
</head>
<body>
<form action="test.php" method="post">
<table>
<tr>
<td>Roll No.</td>
<td><input type="text" name="roll" /></td>
</tr>
<tr>
<td>First exam</td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Second exam</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Third exam</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fourth exam</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fifth exam</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
// it's a good programming practice to always close database connection at the end
if(isset($connection)){
Database::close_connection($connection);
}
?>
Try this. Make 1 Form.
Change this too.
$sql= "INSERT INTO roll (Roll,1st,2nd,3rd,4th,5th) VALUES ($roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
This is also one problem. Use or die (mysql_error()); in mysql functions, not in statement.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form method="post">
<table>
<tr>
<th>Roll:</th>
<th>First Exam</th>
<th>Second Exam</th>
<th>Third Exam</th>
<th>Fourth Exam</th>
<th>Fifth Exam</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="roll" /></td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td><input type='submit' name='submit' value='submit'></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th) VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
</html>
Screenhot
I have a form with three different dropdown menus, it looks like this:
<FORM METHOD="LINK" ACTION="/search.php" method="get">
<select style="width:55px;" name="filter_name">
<option> </option>
<option>R13</option>
<option>R14</option>
<option>R15</option>
<option>R16</option>
<option>R17</option>
</select></td></tr>
<tr><td width="40%">Plotis:</td><td colspan="2"><select style="width:55px;" name="">
<option> </option>
<option>165</option>
<option>175</option>
<option>185</option>
<option>195</option>
<option>205</option>
<option>215</option>
<option>225</option>
</select></td></tr>
<tr><td width="40%">Aukštis:</td><td colspan="2"><select style="width:55px;" name="">
<option> </option>
<option>75</option>
<option>70</option>
<option>65</option>
<option>60</option>
<option>55</option>
<option>50</option>
<option>45</option>
</select></td></tr>
<tr><td colspan="2" align="center">
<INPUT style="width:80px; height:25px; font-size:14px; font-weight:600; cursor:pointer;" TYPE="Submit" VALUE="Ieškoti">
</FORM>
Basically, I need to send all 3 Options into next page but joined into one variable..
For example: If I chose
<option>165</option>+<option>70</option>+<option>R13</option>
it should be sent to index.php like this: filter_name=165/70/R13
and also, how to send all this not to index.php only, but to
index.php?route=product/search&FILTER_NAME
Changing ACTION="/index.php" to ACTION="/index.php?route=product/search" was not working.
Any help would be really appreciated.
You could try something like this. But, you have several errors in your HTML that you should check. You have a nested table within your form.
<?php
if (!isset($_POST['send'])) {
?>
<form method="post" action="">
<select style="width:55px;" name="select_one">
<option> </option>
<option value="R13">R13</option>
<option value="R14">R14</option>
<option value="R15">R15</option>
<option value="R16">R16</option>
<option value="R17">R17</option>
</select>
<select style="width:55px;" name="select_two">
<option></option>
<option value="165">165</option>
<option value="175">175</option>
<option value="185">185</option>
<option value="195">195</option>
<option value="205">205</option>
<option value="215">215</option>
<option value="225">225</option>
</select>
<select style="width:55px;" name="select_three">
<option></option>
<option value="75">75</option>
<option value="70">70</option>
<option value="65">65</option>
<option value="60">60</option>
<option value="55">55</option>
<option value="50">50</option>
<option value="45">45</option>
</select>
<input style="width:80px; height:25px; font-size:14px; font-weight:600; cursor:pointer;" type="submit" value="send" value="Ieškoti" />
</form>
<?php
}
else {
header('Location: index.php?route=product/search&filter_name='.$_POST['select_two'].'/'.$_POST['select_three'].'/'.$_POST['select_one']);
}
?>
Join the 3 filter options in php into a variable.
$options = $_POST['f1'] . '/' . $_POST['f2'] . '/' . $_POST['f3'];
It will be less confusing IMO.
The problem can be in the invalid markup. You are using table layout, and form is split between cells. That is not valid, so may be browser renders your layout in an unexpected way.
You can expect this:
<table>
<tr>
<td>
<form>
<input>
</td>
<td>
<input>
</form>
</td>
</tr>
<table>
But browser can render your markup as this (2 forms):
<table>
<tr>
<td>
<form>
<input>
</form>
</td>
<td>
<form>
<input>
</form>
</td>
</tr>
<table>
So when you submit your form not all data are sent, cause only first form is submitted