validate options[] with isset - php

Hey i am having some trouble getting an options[] array to work, if anyone can help that will be great
Form
<form method="post" action="array2.php">
<select name="options[]">
<option value=""></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="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
The array2.php
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options[]'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
Any help would be great, also what happens is even if it's an empty feild, the thing progress's to the rest of the script
rest of script
<?php
for($i=0; $i < count($checked); $i++){
echo "You have selected to recive " . $checked[$i] . " tickets<br/>";
}
for($i=0; $i < count($checked2); $i++){
echo "And you have selected to recive " . $checked2[$i] . " for accommodation are you sure? <br/>";
}
?>
Sorry I am unable to reply to people for now soon as I posted it a class came to the empty room so need to wait an hour :/

You need the change your options[] to option as it mean you are submitting multiple select with the same name
<form method="post" action="array2.php">
<select name="options">
<option value=""></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="9">9</option>
<option value="10">10</option>
</select>
<input name="submit" type="submit" value="submit">
</form>
in your array2.php file
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
?>
if you really need to send options[]
<?php
session_start();
if(isset($_POST['submit'])){
if(is_array($_POST['options']){
if($_POST['options'][0] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'][0];
$_SESSION['checked'] = $checked;
}
}else{
if($_POST['options'] == "" ){
header("Location: error.html");
exit;
}else{
$checked = $_POST['options'];
$_SESSION['checked'] = $checked;
}
}
}
?>
you can clean this if else as you like

You can use this example code
<?php
if($_POST) {
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
echo '<p>Please select an option from the select box.</p>';
}
else {
echo '<p>You have selected: <strong>', $_POST['state'], '</strong>.</p>';
}
}
}
?>
and your html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="AK">AK - Alaska</option>
<option value="AL">AL - Alabama</option>
<option value="WY">WY - Wyoming</option>
</select>
<input type="submit" name="submit">
</fieldset>
</form>

in your case options[] can be used for sending multiple fields as array, the index starts with 0, for example:
<input name="test[]"> : index 0
<input name="test[]"> : index 1
then, you can get these values in $_POST['test'] like this:
$input_one = $_POST['test'][0];
$input_one = $_POST['test'][1];
if you look at this inside $_POST it will be like this:
$_POST = array (
...,
'test' => array(0=> ..., 1 => ...)
)
for your form, if you only had one options[], then the value is the $_POST will be
if(isset($_POST['options'][0])){
}

So let's clean it up
<select name="options">
You do not need options[].
And then the result of $_POST['options'] will be the value of option. And add there <option value="0">Select</option> and then check whether the POSTed data is higher then 0

in html
<select name="options">
<option value="0"></option>
<option value="1">1</option>
.
.
in php
if(isset($_POST['options']) && !empty($_POST['options'])) {
$checked = $_POST['options'];
}

Related

How to unset and remove blank options from a submitted drop down menu?

Here is my code to use Condorcet:
use CondorcetPHP\Condorcet\Condorcet;
use CondorcetPHP\Condorcet\Election;
use CondorcetPHP\Condorcet\Candidate;
use CondorcetPHP\Condorcet\CondorcetUtil;
use CondorcetPHP\Condorcet\Vote;
if (isset($_POST) && $_POST != "") {
Condorcet::setDefaultMethod('Schulze');
$election = new Election ();
print("<pre>");
print_r($_POST['item']);
// To get Candidates by Category via Doctrine orm
$category = $em->getRepository('Entities\Category')->findOneBy(['id' => 1]);
$candidates = $category->getCandidates();
$total_items = 0;
foreach ($candidates as $candidate) {
$candidate = $candidate->getCandidate();
$election->addCandidate(new Candidate($candidate));
++$total_items;
}
$votes = array();
foreach ($_POST['item'] as $item => $ranking) {
$vote_factor = 1 + $total_items - $ranking;
if (isset($votes[$vote_factor])) {
$votes[$vote_factor] = (array) $votes[$vote_factor];
$votes[$vote_factor][] = $item;
} else {
$votes[$vote_factor] = $item;
}
}
$result = $election->addVote(new Vote($votes));
print("<br />");
foreach ($election->getResult('Schulze') as $rank => $candidates) :
echo 'Rank ' . $rank . ': ';
echo implode(', ', $candidates);
echo '<br />';
endforeach;
print_r($result->getSimpleRanking()); // To be saved in db.
echo 'Schulze winner is : ' . $election->getWinner('Schulze')->getName() . '<br />';
}
?>
<html>
<body>
<form method="post">
Wingspan: <select name="item[Wingspan]" id="Wingspan">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Scythe: <select name="item[Scythe]" id="Scythe">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Spirit Island: <select name="item[Spirit Island]" id="Spirit Island">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
Everdell: <select name="item[Everdell]" id="Everdell">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
I want to add a blank select option, that my users if they don't want to have some candidates in voting just use the blank select option and don't be forced to select a ranking number from the drop down menu. How to omit blank options completely from listing and voting? I think I have to unset blank options in a foreach? If yes, how?

how to edit state and country list and update it using php

Hi i am storing countries and states in database i need to edit and update it in database but when i click on edit option iam not able to fetch the record from database.Here is my code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country" value="<?php echo $row['country'];?>">
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state" value="<?php echo $row['state'];?>">
<option >Select State</option>
</select><br/>
If i click on console the value is printing but iam not able to display in frontend.Can anyone help me regarding this.
please use this code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country">
<option value="<?php echo $row['country'];?>"><?php echo $row['country'];?></option>
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state">
<option value="<?php echo $row['state'];?>"><?php echo $row['state'];?></option>
<option >Select State</option>
</select><br/>
There were few things which were wrong.
Option should be inside select box
select value should be based on database condition
Thanks
Amit
Select box don't contain value attribute. You are going by wrong way.
You can use select box by this way:-
<select name="country">
<option value="country1" <?php if ($row['country'] == 'country1') { echo ' selected="selected"'; } ?>>country1</option>
<option value="country2" <?php if ($row['country'] == 'country2') { echo ' selected="selected"'; } ?>>country2</option>
<option value="country3" <?php if ($row['country'] == 'country3') { echo ' selected="selected"'; } ?>>country3</option>
</select>
<select name="state">
<option value="state1" <?php if ($row['state'] == 'state1') { echo ' selected="selected"'; } ?>>state1</option>
<option value="state2" <?php if ($row['state'] == 'state2') { echo ' selected="selected"'; } ?>>state2</option>
<option value="state3" <?php if ($row['state'] == 'state3') { echo ' selected="selected"'; } ?>>state3</option>
</select>
Hope it will help you :)
You can't assign value attribute to Select Box.So you can use Like this:
<select name="country">
<option value="country" <?php if ($row['country'] == 'country') { echo ' selected="selected"'; } ?>>country</option>
<select name="state">
<option value="state12" <?php if ($row['state'] == 'state12') { echo ' selected="selected"'; } ?>>state12</option>

how to change select tag values based on value coming from database

i want to change tag option based on values coming from database.if value is "1" i want to show different options and different for other values.
<<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
Because
No closing ; on here <option value='4'>Maths</option></select>"
and here too <option value='8'>Maths</option></select>"
First if() condition missing end }.
So final code is
<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
//echo $number;
if ( $number == "1" )
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
You are missing the ; off of the end of both echos:
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
Note: Sorry about poor layout, quick posting of answer!
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"; //missing ; here
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"; //missing ; here
}
} // missing } here

PHP object-oriented multiple insert values to MySQL database

I am building a lightweight school management system, that takes in grades directly from the database, how do I update all values at the same time. Please help
<?php
$mydb->setQuery("SELECT * FROM tblbible");
loadresult();
function loadresult(){
global $mydb;
$cur = $mydb->loadResultList();
foreach ($cur as $result) {
echo '<tr>';
echo '<td> <input type="hidden" name="studentID" value="'. $result->studentID.'" /></td>';
?>
<?php
$rowz = mysql_query("SELECT * FROM tblstudent where studentID = '$result->studentID' ")or die(mysql_error());
$detailz = mysql_fetch_array($rowz);
echo '<td>'.$detailz['name'].' '.$detailz['initial'].' '.$detailz['lastname'].'</td>';
?>
<?php
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
?>
<?php
echo '</tr>';
}
?>
then the insert function below:
<?php
function doInsert(){
if (isset($_POST['save'])){
if ($_POST['value1'] == "" OR $_POST['value2'] == "" OR $_POST['value3'] == "") {
$messageStats = false;
message("All fields are required!","alert alert-success alert-dismissable");
redirect('index.php?page=5');
}else{
$studentID = $_POST['studentID'];
for($i = 0; $i<10; $i++) {
foreach( ($studentID) as $key => $value ) :
$cla = new Bible();
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$value3 = $_POST['value3'];
$cla->sing =$value1;
$cla->verses =$value2;
$cla->prayer =$value3;
$cla->update($studentID);
redirect('index.php?page=5');
message("Report generated successfully!", "info");
endforeach;
}
}
}
}
And I would love to add all those in a one click button.

PHP Add value to each table row data

i'm working on a php script wherein i must add certain score value at each row. I was able to display all the rows but i'm not sure on how would I able to store each of the given score in a variable and what query should I make to add all of them.
Here's my code
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
$query = mysql_query('insert query here');
}
?>
</table>
</body>
</html>
any suggestions are appreciated. Thanks in advance.:D
I think you need the id of each changed row (maybe as a hidden field for each row. Then just do a loop through all received rows and UPDATE each one.
You might also want to change all of your form field names to use the array format. This way it's easier to make your PHP loop throught them.
Sample row:
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score['.$i["id"].']" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
Now just loop through the $_POST["score"] array and use the appropriate ID for your update.
foreach($_POST["score"] as $id => $value{
// ESCAPE your db values!!!!!
// query stuff with $value and $id
}
Also keep in Mind
mysql is deprecated! Use mysqli
Escape anything from outside sources like $_POST before use in SQL
You just needs to make an array of your drop down box like below,
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score[".$i['com_id']."]" id="score" size="1">
<option valyue="5">5</option>
<option valyue="5">10</option>
<option valyue="5">15</option>
<option valyue="5">20</option>
<option valyue="5">25</option>
<option valyue="5">30</option>
<option valyue="5">35</option>
<option valyue="5">40</option>
<option valyue="5">45</option>
<option valyue="5">50</option>
</select></td></tr>';
echo'<br>';
}
and you can access it for all of your comments
<option valyue="5">50</option>
should be
<option value="5">50</option>
To send the value of a comment to database you need to add a ID of the comment
you should loop something like this.
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
echo '<input type="hidden" name="id" value="'.$i['com_id'].'">';
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
echo'<br>';
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
}
I guess the easiest way for you is the following (a mix of the other solutions and comments):
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$x = 0;
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
$x++;
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score_'.$x.'" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
for($y = 0;$y <= $x; $y++)
{
//This query is no sql injection save. Please add filters for productive uses!!
$query = mysql_query('UPDATE table_name SET score = '.$_POST["score_".$y].' WHERE id='.$id);
}
?>
</table>
</body>
</html>
Code is no tested!

Categories