I am currently working with an simple php code with validation and this is my code:
<form action="" method="post">
<input type="text" name="name" id="name" placeholder="Name">
<br>Number of Books:<select id="books" name="books">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
<option> 4 </option>
<option> 5 </option>
<option> 6 </option>
<option> 7 </option>
<option> 8 </option>
<option> 9 </option>
<option> 10 </option>
</select>
</br>dutedate <input type="date" name="duedate" id="duedate" placeholder="duedate">
</br>borrow <input type="date" name="datebor" id="datebor" placeholder="dateborrowed">
</br>return <input type="date" name="ret" id="ret" placeholder="date returned">
<input type="submit" class="button" name="btnsubmit" value="Submit">
</form>
<?php
if (isset($_POST['btnsubmit']))
{
$name = $_POST['name'];
$duedate = date_create(($_POST['duedate']));
$datebor = date_create(($_POST['datebor']));
$ret = date_create(($_POST['ret']));
$books = $_POST['books'];
$NOWdate = new Datetime('now');
if ($duedate < $datebor)
{
echo "<script>alert('Wrong Input Error1')</script>";
exit;
}
elseif ($datebor > $ret)
{
echo "<script>alert('Wrong Input Error2')</script>";
exit;
}
elseif ($datebor > $NOWdate )
{
echo "<script>alert('Wrong Error3')</script>";
exit;
}
elseif ($ret < $duedate)
{
$penalty = 0 ;
}
else
{
$penaltydays = date_diff($duedate,$ret) ;
$diff_date = $penaltydays->format('%a');
$penalty = $diff_date * $books * 10;
}
}
?>
<p>Penalty: <?php echo $penalty ?></p>
When I try to run it, there is an error Undefined variable: penalty.
I am pretty sure that my validation codes are correct
How could I pass the penalty variable to the page itself.
I think it would resolve the error.
The problem is that the $penalty is only set after you submit the form. You should be able to fix this by simply moving the penalty paragraph inside your elseif/else blocks where a penalty should actually be shown.
...
elseif ($ret < $duedate)
{
echo "<p>Penalty: 0</p>";
else
{
$penaltydays = date_diff($duedate,$ret) ;
$diff_date = $penaltydays->format('%a');
$penalty = $diff_date * $books * 10;
echo "<p>Penalty: $penalty</p>";
}
This way it will only be displayed if it has been calculated.
Side note, I don't think this will work the way you are expecting it to because your select options do not have value attributes. You need to add them like this:
<option value="1"> 1 </option>
Related
I want to save the selected value in post and access it from checkout.php. So far this method has worked for my other pages but here it just doesn't. This is how it looks like:
Source page of the value:
<form action = "checkout.php" method="post" >
<select name='time' required>
<option value="">-- Time --</option>
<?php
$result = getFreeAppointments($empid,$date);
while ($row = mysqli_fetch_array($result)) {
?>
<option value = <?php echo $row['StartTime'];?>><?php echo $row['StartTime'];?></option>
<?php } ?>
</select>
<input class="submit-block" type="submit" value="Checkout">
</form>
Then in checkout.php it looks like this.
if(isset($_POST['time']) && !empty($_POST['time'])) {
$time = $_POST['time'];
}
else{
$time = '10:00:00';
}
If I echo the $time variable, I always get the default value '10:00:00'. Any idea what could cause this problem?
You forgot to add name attribute in <input class="submit-block" type="submit" value="Checkout">
Like :- <input class="submit-block" type="submit" name="submit" value="Checkout">
<form action = "checkout.php" method="post" >
<select name='time' required>
<option value="">-- Time --</option>
<?php
$result = getFreeAppointments($empid,$date);
while ($row = mysqli_fetch_array($result)) {
?>
<option value = <?php echo $row['StartTime'];?>><?php echo $row['StartTime'];?></option>
<?php } ?>
</select>
<input class="submit-block" type="submit" name="submit" value="Checkout">
</form>
you have set isset($_POST["submit"]) but in HTML the form, there is no data with this name.
if (isset($_POST['submit'])){ // Problem is here
insertKunde($_POST);
if(isset($_POST['time']) && !empty($_POST['time'])) {
$time = $_POST['time'];
}
else{
$time = '10:00:00';
}
}
So I am making a programme and I am submitting a form to itself. This programme is a very basic calculator as I am new to HTML and PHP. I am trying to make it so that when you submit the form, the Select dropdown will remain on the most recently used operator.
For example, if I make the calculator do '5 + 5', then I want the submitted form to keep the operator dropdown on '+'.
Here is my code:
<?php
// grab the form values from $_HTTP_GET_VARS hash extract($_GET);
// first compute the output, but only if data has been input if(isset($calc) && $operator == "multiply") { // $calc exists as a variable
$prod = $x * $y; } elseif (isset($calc) && $operator == "plus") {
$operator = $plus;
$prod = $x + $y; } elseif (isset($calc) && $operator == "minus") {
$operator = "minus";
$prod = $x - $y; } elseif (isset($calc) && $operator == "divide") {
$sign = "/";
$prod = $x / $y; } else { // set defaults
$x=0;
$y=0;
$prod=0; } ?>
<html> <head>
<title>PHP Calculator Example</title> </head>
<body>
<h3>PHP Calculator (Version 1)</h3>
<p>Multiply two numbers and output the result</p>
<form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">
<label for="x">x = </label>
<input type="text" name="x" id="x" size="5" value="<?php print $x; ?>"/>
<select name="operator" id="operator">
<option value="plus">+</option>
<option value="minus">-</option>
<option value="divide">/</option>
<option value="multiply">*</option>
</select>
<label for="y">y = </label>
<input type="text" name="y" id="y" size="5" value="<?php print $y; ?>"/>
<input type="submit" name="calc" value="Calculate"/>
</form>
<!-- print the result -->
<?php if(isset($calc)) {
print "<p>x $sign y = $prod</p>";
} ?>
</body> </html>
In order to remain on option selected in the select element
The option has to have an attribute "selected"
<select name="operator" id="operator">
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "plus"){echo "selected";} ?> value="plus">+</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "minus"){echo "selected";} ?> value="minus">-</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "divide"){echo "selected";} ?> value="divide">/</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "multiply"){echo "selected";} ?> value="multiply">*</option>
</select>
Hope that it will help you.
A possible solution employing javascript would be this
<?php
if(isset($_GET['operator'])) {
?>
<script>document.getElementById("operator").value = '<?=$_GET['operator']?>';</script>
<?php
}
?>
I have a multiple select on my code, what I need to do is to have a validation when the user click the submit button without selecting any of the given list.
Here's the sample code:
index.php
<?php
IF(isset($_POST['Rtype'])){
$RetrieveType = $_POST['Rtype'];
IF($RetrieveType == 'date_range'){
$start_date = new DateTime($_POST['start_date']);
$sd = date_format($start_date,'Y-m-d');
$p_week=array();
$m_month=01;
$end_date = new DateTime($_POST['end_date']);
$end_date-> add(new DateInterval('P1D'));
$ed =date_format($end_date,'Y-m-d');
}
ELSEIF($RetrieveType == 'weekly'){
$p_week = $_POST['week'];
$m_week = implode(',',$p_week);
$m_month =$_POST['month_m'];
$m_year =$_POST['year_m'];
$p_lob = $_POST['lob'];
$sc_lob=implode(',',$p_lob);
} ELSE
{
$RetrieveType = 'date_range';
$sd = date('Y-m-01');
$start_date = date('Y-m-01');
$ed = date('Y-m-d');
$end_date = date('Y-m-d');
$m_month=date ('m');
$p_week=array();
$m_year= date ('Y');
$sc_lob='';
}
?>
<html>
<head>
<head>
<body>
<form action="index.php" method="POST" class="form-inline">
<label for="sel1">Week:</label>
<select data-placeholder="<?php echo (isset($_POST['week']) ? "Week/s Currently Selected: ".implode(",",$_POST['week']) : "Select Week"); ?>"
class="chosen-select" multiple tabindex="4" style="width:350px;" name= "week[]">
<option value="1" id="empty">Week 1</option>
<option value="2" id="empty">Week 2</option>
<option value="3" id="empty">Week 3</option>
<option value="4" id="empty">Week 4</option>
<option value="5" id="empty">Week 5</option>
</select>
<label for="sel1">LOB:</label>
<select data-placeholder="<?php echo (isset($_POST['lob']) ? "LOB/s Currently Selected: ".implode(",",$_POST['lob']) : "Select LOB"); ?>"
class="chosen-select" multiple tabindex="4" style="width:350px;" name= "lob[]">
<?php
$lob = array();
$q = "SELECT distinct LOB from roster where EmployStatus='active' and SDLead <> '' group by LOB";
$params = array();
$query = sqlsrv_query($conn, $q);
while($rowsss= sqlsrv_fetch_array($query)) {
$lob = $rowsss['LOB'];
echo "<option value='".$lob."'>".$lob."</option>";
}
for ($i = 0; $i <= count($lob) - 1; $i++) {
IF($sc_lob == $lob[$i])
{$isSelected = 'selected';}
elseif(1==1)
{$isSelected='';}
echo "<option ".$isSelected." value='".$lob[$i]."'>".$lob[$i]."</option>"; }
?>
<input type="hidden" name="Rtype" value="weekly">
<input class="btn btn-primary" type="submit" />
</form>
What I wanted to achieve is when the user clicked the submit button without selecting any on the "Week" list or "LOB" list, there would be an error message that says "This field is required, select at least one.". Actually I already did this before, when I'm just using a checkbox. I placed the code below after the label tag:
sample code:
<?php
if(isset($_GET['err']))
{
$err = $_GET['err'];
if($err == 1)
{
echo "<span class='text-danger'>* This field is required, select at least one.</span>";
}
else
{
echo "";
}
}
else
{
echo "";
}
?>
Try this
if(isset($_POST['submit']){
errors = 0;
if($_POST['nameOfSelect'] == ""){
errors++;
}
if(errors == 0){
return true;
}else{
return false;
}
I need to create a form that allows the user to select a number from 0 to 7 and "show all" and the script will then output the object found in that train car to the user, or if show all is picked then it runs a loop that shows all 8 objects along with the train number. But my code doesn't really run.
(Please ignore my code if it doesn't make any sense, but I don't seem to get arrays :( Please point out the mistakes and thank you so so so much)
<html>
<body>
<form name="train" method="GET" action="test.php">
<select>
<option value="0" name="object">0</option>
<option value="1" name="object">1</option>
<option value="2" name="object">2</option>
<option value="3" name="object">3</option>
<option value="4" name="object">4</option>
<option value="5" name="object">5</option>
<option value="6" name="object">6</option>
<option value="7" name="object">7</option>
<option value="8" name="object">8</option>
<option value="all" name="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['object'];
echo "<p>I have $train!</p>";
}
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
?>
</body>
</html>
There are some mistakes in the code
Select should have name attribute
Need to get object and display results based on the selected option.
Use the below code
<form name="train" method="GET" >
<select name="object">
<option value="0">0</option>
......
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
PHP code
$train[0] ="pencil";
....
$train[8] = "cup";
if ($_GET['submit']) {
$object = $_GET['object'];
if($object == 'all'){
for ($i = 0; $i < sizeof($train); $i++) {
echo "<li>" . $train[$i] . "</li>";
}
}else{
echo "<p>I have $train[$object]!</p>";
}
}
Background
I have a for loop creating a table of inputs for an html form:
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo">Foo</option>
<option value="bar">Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="" name="bar_'.$i.'">';
}
On submit, this populates a database.
Problem
Each submission needs to be editable. When I return to the form (as an admin) I need to see everything that was stored in the database by a particular user.
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo"';
if($row['waldo_'.$i] == "foo") echo " selected='selected'";
echo '>Foo</option>
<option value="bar"';
if($row['waldo_'.$i] == "bar") echo " selected='selected'";
echo '>Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="'./*...*/.'" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="'./*...*/.'" name="bar_'.$i.'">';
}
My select properly "selects" the correct option, but I don't seem to be able to populate the text input values in a similar manner.
Somehow I need to echo the content in $foo_1, $foo_2, $foo_3, ..., $foo_x.
I have tried using $foo_.$i but that doesn't seem to work.
Is there a simple solution to this problem? Or is there a better method to format everything?
If I'm not misunderstanding your question:
$_POST["foo_".$i]
Should show you the submitted data.
EDIT: Or maybe this is what you are looking for?
for ($i = 1; $i <= $x; $i++) {
echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
<option value="">...</option>
<option value="foo"';
if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "foo") echo " selected='selected'";
echo '>Foo</option>
<option value="bar"';
if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "bar") echo " selected='selected'";
echo '>Bar</option>
</select>
<label for="foo_'.$i.'">Foo '.$i.'</label>
<input id="foo_'.$i.'" type="text" value="';
if(isset($row['foo_'.$i]) && $row['foo_'.$i] != "") echo $row['foo_'.$i];
echo '" name="foo_'.$i.'">
<label for="bar_'.$i.'">Bar '.$i.'</label>
<input id="bar_'.$i.'" type="text" value="';
if(isset($row['bar_'.$i]) && $row['bar_'.$i] != "") echo $row['bar_'.$i];
echo '" name="bar_'.$i.'">';
}