PHP if/else form - php

I am trying to create a form where if you fill in a number like 11 you get a response with a color type. I know that I have to change $tulp, but I don't know what to do.
<form name= "gemiddelden" action= "" method="post">
<p>Geelzweem (%): <input type="text" name="getal1"/></p>
<p><input type="submit" name="Bepaal" value="Bepaal"/></p>
</form>
$tulp = date("H");
if($tulp == 10) {
echo("Lichtgeel");
}
else if($tulp >10 && $tulp <20)
{
echo("Geel");
}
else if($tulp >20 && $tulp <30)
{
echo("Zon geel");
}
else if($tulp >30 && $tulp <40)
{
echo("Diep Geel");
}
else if($tulp >40 && $tulp <50)
{
echo("Zwavel Geel");
}
else if($tulp >50 && $tulp <60)
{
echo("Intens Geel");
}
else if($tulp >60 && $tulp <70)
{
echo("Verzadigd Geel");
}
else if($tulp >70)
{
echo("Dit mag niet");
}
else {
echo("Error");

According to google translate Geelzweem represent the shade of yellow. Then the echoed texts are different shades. I am guessing that you need to display a color based on the input in the getal1 field. In that case you need to change the line
$tulp = date("H");
to
if (isset($_POST["getal1"])) {
$tulp = $_POST["getal1"];
} else {
// some default value here
$tulp = 20;
}
Also, although the standard says that it is not mandatory, it is best practice to specify the action of the form
<form name= "gemiddelden" action= "SOME URL HERE" method="post">

Needed to change $tulp to $tulp = $_POST['getal1'];

Related

How to get the value which user has selected in php?

I have a little problem here. I have a list, which is:
<form action="display.php" method="post">
<select name="holiday">
<option value="month">Kiekvieno mėnesio skaičiavimas</option>
<option value="day">Kiekvienos dienos skaičiavimas</option>
</select>
<br> <br>
<input type="submit" name="select" value="Pasirinkti" />
</form>
What I need to do is that when the user selects value ="month", php code would do one action, and when the user selects value ="day", php code would do another action?
My php code looks like this:
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
else if ($selectedValue == $_POST['month']) {
$todaysDate = new DateTime();
while ($employee = $select->fetch()){
$employmentDateValue = new DateTime($employee['employment_date']);
// Comparing employment date with today's date
$differenceBetweenDates = date_diff($employmentDateValue, $todaysDate);
$workMonths = $differenceBetweenDates->y * 12 + $differenceBetweenDates->m;
$holidayDays = $workMonths *4;
echo "<tr>";
echo "<td>".$employee['name']."</td>";
echo "<td>".$employee['surname']."</td>";
echo "<td>".$employee['employment_date']."</td>";
echo "<td>".$workMonths."</td>";
echo "<td>".$holidayDays."</td>";
echo "</tr>";
}
}
else {
echo "Lalalala";
}
?>
I've tried to do so with $_POST['select'], but it doesn't work. Thanks for any help guys
<?php
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
if ($selectedValue == 'month') {
}
else if ($selectedValue == 'day') {
}
else{
echo "Lalalala";
}
}
?>
You need to do $_POST['holiday'], so change:
if($_POST['select']){
// Storing selected value in a variable
$selectedValue= $_POST['holiday'];
}
to
if($_POST['holiday']){
// Storing selected value in a variable
$selectedValue = $_POST['holiday'];
}
You also need to change the line:
else if ($selectedValue == $_POST['month']) {
So it's not part of the original if statement:
if ($selectedValue == 'month') {
// your code
}
else {
echo "Lalalala";
}

Is checkboxes checked or not?

Need som serious help!
<?php
$php1 = !isset($_GET['php1']);
$php2 = !isset($_GET['php2']);
$php3 = !isset($_GET['php3']);
$php4 = !isset($_GET['php4']);
$php5 = !isset($_GET['php5']);
if ($php1 == 'one' && $php2 == 'two' && $php5 == 'five') {
echo "<h2>R</h2>WRONG";
} else {
echo "<h2>R</h2>CORRECT";
}
?>
HTML
<form action="One.php" method="get">
<input type="checkbox" name="php1" value="one"> q1<br>
<input type="checkbox" name="php2" value="two"> q1<br>
<input type="checkbox" name="php3" value="three"> q1<br>
<input type="checkbox" name="php4" value="four"> q1<br>
<input type="checkbox" name="php5" value="five"> q1<br>
<br>
<input type="submit" value="Send!">
</form>
If all checkboxes are empty, there will be a message saying that. How do I do it?
If I dont check any boxes, it tells me the answers are correct. That's wrong.
<?php
If(isset($_GET['php1'])){
$php1 = 1;
}else{
$php1 = 0;
}
If(isset($_GET['php2'])){
$php2 = 1;
}else{
$php2 = 0;
}
If(isset($_GET['php3'])){
$php3 = 1;
}else{
$php3 = 0;
}
If(isset($_GET['php4'])){
$php4 = 1;
}else{
$php4 = 0;
}
If(isset($_GET['php5'])){
$php5 = 1;
}else{
$php5 = 0;
}
if ($php1 && $php2 && $php5) {
echo "<h2>Resultat</h2>Du svarade rätt på frågan";
} else {
echo "<h2>Resultat</h2>Du svarade fel på frågan";
}
Try this.
Each php variable will have the value of 1 or 0 (true/false). Thus the if only needs to check it if it is or not.
You have to remember that UN-CHECKED Checkboxes are not even sent to the PHP script $_POST/$_GET by the browser.
So there existance is normally all you need to know.
First you must check that they were passed to the script and then check its value, otherwise you will receive undefined index errors for each checkbox that was not checked by the user
if (isset($_GET['php1']) && $_GET['php1'] != '' ) {
Although as the checkbox called php1 can only be set to the value you give it, its value can be assumed and all you need to do is
if (isset($_GET['php1']) ) {
// php1 was checked
Also isset() will test more than one variable exists using an AND. So you could write your code as
if ( isset($_GET['php1'], $_GET['php2'], $_GET['php5']) ) {
echo "<h2>Resultat</h2>Du svarade rätt på frågan";
} else {
echo "<h2>Resultat</h2>Du svarade fel på frågan";
}
$php1 = !isset($_GET['php1']);
This means $php1 is either true or false. If you want the value of php1 get parameter then remove isset cover, just
$_GET['php1'];
isset is used to check whether the variable exist or not after that use $_GET['php1'];

make sure atleast one checkbox is selected php

I have a little mailing form with a few checkboxes. At least one of the boxes need to be selected before mailing should start.
My HTML:
<input type='checkbox' id='part1' name='box1' value='box1' checked>
<label for="part1">Voor Leden agenda</label>
<br/>
<input type='checkbox' id='part2' name='box2' value='box2' checked>
<label for="part2">Voor Leiding agenda</label>
<br/>
<input type='checkbox' id='part3' name='box3' value='box3' checked>
<label for="part3">Verhuur agenda</label>
<br/>
<button type='submit' name='send'/>send</button>
My PHP:
if (isset($_POST['box1'])) {
$box1 = 'yes';
} else {
$box1 = 'No';
}
if (isset($_POST['box2'])) {
$box2 = 'yes';
} else {
$box2 = 'No';
}
if (isset($_POST['box3'])) {
$box3 = 'yes';
} else {
$box3 = 'No';
}
i would like to have a script that gives a message like below if no checkbox is selected:
if()
{
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}
edit: how can I give this message with php, only if all boxes are unchecked
if(!isset($_POST['box1']) && !isset($_POST['box2']) && !isset($_POST['box3']))
{
// none is set
}
You could even apply De Morgan's law and write this equivalent expression
if(isset($_POST['box1']) || isset($_POST['box2']) || isset($_POST['box3']))
{
// at least one of them is set
}
You could even send those 3 parameters to 1 isset call but then that would check if all of them are set, which is not your requirement.
Try this:
if(isset($_POST["box1"]) || isset($_POST["box2"]) || isset($_POST["box3"])) {
if(isset($_POST['box1'])) {
$box1 = 'yes';
} else {
$box1 = 'No';
}
if(isset($_POST['box2'])) {
$box2 = 'yes';
} else {
$box2 = 'No';
}
if(isset($_POST['box3'])) {
$box3 = 'yes';
} else {
$box3 = 'No';
}
} else {
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}
This one is more readable I think:
$boxes = ['box1', 'box2', 'box3'];
$checked = [];
foreach($boxes as $box){
if(isset($_POST[$box])){
$checked[] = $box;
}
}
if(count($checked) == 0){
// no boxes checked
echo "<p class='redfont'>no checkboxes are selected</p>";
echo "<p><a href='javascript:history.back();'>Click to go back</a></p>";
}else{
// at least one box is checked, you can do another foreach statement
with the $checked variable to do stuff with the checked ones
}
To do this for ANY number of checkboxes (Webistes are bound to expand), I assume all of you checkboxes are of the form box**i** :
if( strpos( implode(',' , array_keys($test)) , 'box' ) !== FALSE )
I use this function in JQuery:
jQuery.validation = function(){
var verif = 0;
$(':checkbox[id=list_exp]').each(function() {
if(this.checked == true){
verif++
}
});
if(verif == 0){
alert("no checkboxes are selected");
return false;
}
}

PHP overriding isset value, showing new output

So, I have this code
<?php
if(!isset($_POST["step1_submit"]))
{
echo "Fill step1!";
step1();
}
function step1()
{
if(isset($_POST["step1_submit"]))
{
step2();
}
else
{
echo '<form action="" method="post">STEP 1: <input name="step1_input"/><input name="step1_submit" type="submit" value=">>STEP 2>>"/></form>';
}
}
function step2()
{
echo "reached step 2";
if(isset($_POST["step2_submit"]))
{
echo '<br/>'.$_POST["step2_input"];
step3();
}
else
{
echo '<form method="post">STEP 2: <input name="step2_input"/><input name="step2_submit" type="submit" value=">>STEP 3>>"/></form>';
}
}
function step3()
{
echo '<strong>WELL DONE</strong>';
}
?>
It shows the input for step1, but never gets to show step2. In my opinion, it gets stuck on calling the function step2, because while doing it, the isset($_POST(step1_submit)) changes it value to NULL.
How can I manage to make this code work? It should be like: filling step 1 input >> getting to fill step 2 input >> submit step 2 >> get to see the 'WELL DONE' echo.
Basically the conditional statement on top needs an else branch:
if(!isset($_POST["step1_submit"]))
{
echo "Fill step1!";
step1();
} else {
step2();
}
...
However, if you have more steps it should look like this:
switch(TRUE) {
case isset('step1_submit') :
step2();
break;
case isset('step2_submit') :
step3();
break;
...
default:
echo "Fill step1!";
step1();
}
Then change your functions to:
function step1()
{
echo '<form action="" method="post">STEP 1: <input name="step1_input"/><input name="step1_submit" type="submit" value=">>STEP 2>>"/></form>';
}
function step2()
{
echo '<form action="" method="post">STEP 2: <input name="step2_input"/><input name="step2_submit" type="submit" value=">>STEP 3>>"/></form>';
}
...
The if conditionals aren't required there.

Delete using PHP is not working

I am having a rather strange issue and for the likes of me, cannot figure it out!
Basically, I have users who are allowed to upload documents, which are then associated with their profile.
If the user decides to delete a document, the only thing that gets deleted here, is the document, but not the content included, e.g. comments, title, etc - it's as if nothing ever happened - except of course - the physical document has been deleted - sql entries however have not.
mydocs.php:
if ($_SESSION['USERID'] != "" && $_SESSION['USERID'] >= 0 && is_numeric($_SESSION['USERID']))
{
if($_REQUEST['submitdelete']!="")
{
$deletedoc = $_POST['deletedoc'];
$svcount = count($deletedoc);
for ($i = 0; $i < $svcount; $i++)
{
if ($deletedoc[$i] != "" && $deletedoc[$i] >= 0 && is_numeric($deletedoc[$i]))
{
$query = "SELECT * FROM docs WHERE DID='".mysql_real_escape_string($deletedoc[$i])."'";
$executequery = $conn->execute($query);
$theuserid = $executequery->fields['USERID'];
$doc_name = $executequery->fields['doc_name'];
if(mysql_affected_rows()>=1)
{
$docpath = $config['docdir']."/".$doc_name;
#chmod($docpath, 0777);
if (file_exists($docpath))
{
#unlink($docpath);
}
if($theuserid == $_SESSION['USERID'])
{
$deletefrom[] = "docs";
$deletefrom[] = "docs_comments";
$deletefrom[] = "docs_favorited";
for($j=0;$j < count($deletefrom);$j++)
{
$query = "DELETE FROM ".$deletefrom[$j]." WHERE DID='$deletedoc[$i]'";
$conn->Execute($query);
}
$tempthumbs = $config['thumbdir']."/".$deletedoc[$i].".jpg";
if(file_exists($tempthumbs))
{
#unlink($tempthumbs);
}
if ($svcount > 1)
{
$message = $lang['643'];
}
else
{
$message = $lang['644'];
}
}
else
{
if ($svcount > 1)
{
$error = $lang['645'];
}
else
{
$error = $lang['646'];
}
}
}
}
}
}
mydocs.tpl:
<form id="deleteform" name="deleteform" action="{$baseurl}/mydocs.php" method="post">
{section name=i loop=$docs}
{insert name=seo_clean_titles assign=title value=a title=$docs[i].title}
<div class="column {if $smarty.section.i.iteration % 6 == 0}last{/if}">
<div class="image"><img src="{$vthumburl}/{$docs[i].doc_name|truncate:-4:"":true}.jpg" alt="{$docs[i].title|stripslashes|truncate:25:"...":true}" ></div>
<h3>{$docs[i].title|stripslashes|truncate:17:"...":true}
<br />{$lang485}: <input type="checkbox" name="deletedoc[]" value="{$docs[i].DID}">
<br />{$lang318}</h3>
</div>
{/section} <div class="btndelete">
<input type="submit" value=" " name="submitdelete"></div>
</form>
Urgently awaiting a solution / assistance.
Many thanks in advance!
There is nothing wrong with the code.
For some reason, the connect.php was using write only permissions to the sql db.
Change it to All Privileges and now it works.
Now to secure it.

Categories