I am new to PHP. I am trying to create a simple form where user will submit there name and mark they got in the exam.
<form action="" method="post">
<input type="text" name="name" id="" placeholder="Your name">
<input type="number" name="mark" id="" placeholder="Your Mark">
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST["submit"])){
$name = $_POST['name'] ;
$mark = $_POST['mark'];
if( $mark > 80 ){
$result = "A+";
}elseif($mark > 70 && $mark < 80){
$result = "A";
}
elseif($mark > 60 && $mark < 70){
$result = "A-";
}
elseif($mark > 40 && $mark < 60){
$result = "B";
}else{
$result = "Fail";
}
echo $result;
}
?>
But if I echo the result variable it is always showing 'Fail'. What is I am doing wrong? thanks
As mentioned in the comments, when comparing ranges, you need to consider the corner cases:
$mark >= 70 && $mark < 80
Also there are few ways to simplify your code: If you check for marsk >= 80 in the first IF, there is no need to check for that in the next elseif:
if( $mark >= 80 )
{
$result = "A+";
}
elseif($mark >= 70)
{
$result = "A";
}
elseif($mark >= 60)
{
$result = "A-";
}
elseif($mark >= 40)
{
$result = "B";
}
else
{
$result = "Fail";
}
echo $result;
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I have tried to create a code which is
<html>
<body>
<form method = "post">
<?php
Number : <input type = "text" name = "prime">;
<input type = "submit" value ="Submit">;
$num = $_REQUEST["prime"];
$flag = 0;
for($i = 2; $i <= $num/2; $i++)
{
if( $num % $i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo "$num is a prime number";
else
echo "$num is not a prime number";
?>
</form>
</body>
</html>
Whenever i try to run it, i get the error
Parse error: syntax error, unexpected '<', expecting end of file in
C:\xampp\htdocs\pc.php on line 5
Any help would be appreciated
Your code is wrong. There is no statement in line 5. Corrected code:
<html>
<body>
<?php
if($_SERVER['REQUEST_METHOD']=='post'){
$num = $_POST["prime"];
$flag = 0;
for($i = 2; $i <= $num/2; $i++)
{
if( $num % $i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo $num." is a prime number";
else
echo $num." is not a prime number";
}
?>
<form method = "post" action="#">
Number : <input type = "text" name = "prime">
<input type = "submit" value ="Submit">
</form>
</body>
</html>
This will check current request method. If is is pot
You can follow the below procedure for solving your problem
<html>
<body>
<?php
if(isset($_POST['prime']))
{
$num = $_POST["prime"];
$flag = 0;
for($i = 2; $i <= $num/2; $i++)
{
if( $num % $i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo "$num is a prime number";
else
echo "$num is not a prime number";
}
?>
<form method = "post">
<input type = "text" name="prime">
<input type = "submit" value ="Submit">
</form>
</body>
</html>
I have a problem with my if and else statement in PHP where it never runs the else statement.
The input form is in HTML:
<input type="radio" name="marital_stat" id="single" value="single" />Single
<input type="radio" name="marital_stat" id="married" value="married" />Married
<input name="age" type="text" size="5" maxlength="3" placeholder="30" required/>
<input name="work" type="radio" id="employee" value="employee" />Employee
<input name="work" type="radio" id="own" value="own" />
Own Business
<input name="work" type="radio" id="jobless" value="jobless" />Jobless
<input name="place" type="radio" id="urban" value="urban" />Urban
<input name="place" type="radio" id="rural" value="rural" />Rural</td>
Here is the PHP Code:
if ($marital_stat == 'married')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'jobless')
{
if ($place == 'rural') { $loan_credibility == 5; }
}
}
}
else if ($marital_stat == 'single')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'employee')
{
if ($place == 'rural') { $loan_credibility == 1; }
}
}
}
Here is a condition that will display some output:
$A = 'positive';
$B = 'negative';
if ($loan_credibility == 5 ){
echo $B ;}
else{
echo $A;
}
As I see you make $loan_credibility == 5; or 1; the == only used in equality statement it checks the two sides if they equal and you have to use = to set value, not == so it will be $loan_credibility = 5; or 1;
You have no else clause (which logically is necessary when using else if)...
if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Do any of the following (#1 or #2 make the most sense)...
Make your else if into a separate if block
Change else if to else
Or add an else clause
Also, as #Mohamed Belal pointed out, when setting variables use = not ==.
So two things to fix your problem: 1) if-else if-else logic and 2) = vs ==...
if ($marital_stat == 'married')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'jobless')
{
if ($place == 'rural') { $loan_credibility = 5; }
}
}
}
if ($marital_stat == 'single')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'employee')
{
if ($place == 'rural') { $loan_credibility = 1; }
}
}
}
i need help to generate series of number and it should be generate series of number when
form is submitted i have code but it not working properly?
For Example I Want Like This
00500
00501
00502
00503 and so on...
Here My Code
$random = rand(500,999);
$new_val = $random+1;
for($i=1;$i<=$new_val;$i++)
{
if( strlen($i)==1 )
{
$say='0000'.$i;
}
elseif( strlen($i)==2 )
{
$say='000'.$i;
}
elseif( strlen($i)==3 )
{
$say='00'.$i;
}
elseif( strlen($i)==4 )
{
$say='0'.$i;
}
elseif( strlen($i)==5 )
{
$say=$i;
}
}
<form method="post">
<input class="input_field" readonly="readonly" type="text"
name="sno" id="sno" value="<?=$say?>" >
<input type="submit" name="test" value="submit" />
</form>
Use sprintf to format it:
<?php
$start = rand(500, 999);
for ($i = 1; $i <= $start; $i++) {
$num = sprintf("%05d", $i);
echo $num;
}
?>
As Blender says, use sprintf to format your numbers
$start = rand(500, 999);
for ($i = $start; $i <= 999; $i++) {
$num = sprintf("%1$05d", $i);
echo $num . '<br>';
}
Note that the form must contain an "action" attribute
I really don't get why this isn't working, so please help. I'm trying to convert a str to an int and do if statements with it, but I can't for some reason. The code jumps right over the if statement like it's not even there???
<?php
$cost = $_REQUEST['cost'];
$cost = (int) $cost;
if($cost < 2){
header('Location: page.php?say=numerror');
}
?>
<input name="cost" id="cost" type="text" class="tfield" />
I have a suspicion you need:
if ($cost < 2) {
exit(header('Location: page.php?say=numerror'));
}
why do you need a conversion just use this:
<?php
$cost = $_REQUEST['cost'];
if($cost < 2 or !is_numeric($cost)){
header('Location: page.php?say=numerror');
}
?>
<input name="cost" id="cost" type="text" class="tfield" />
Try this:
<?php
$cost = $_REQUEST['cost'];
$cost = intval($cost);
if($cost < 2){
header('Location: page.php?say=numerror');
}
?>
// HTML
<input name="cost" id="cost" type="text" class="tfield" />
Here is more about intval() function at intval() PHP reference manual. I hope, that will be helpful.
If this not help you. Here is PHP function where you from string can separate integers.
<?php
function str2int($string, $concat = true) {
$length = strlen($string);
for ($i = 0, $int = '', $concat_flag = true; $i < $length; $i++) {
if (is_numeric($string[$i]) && $concat_flag) {
$int .= $string[$i];
} elseif(!$concat && $concat_flag && strlen($int) > 0) {
$concat_flag = false;
}
}
return (int) $int;
}
// Callings
echo var_dump(str2int('sh12apen11')); // int(12)
echo var_dump(str2int('sh12apen11', false)); // int(1211)
echo var_dump(str2int('shap99en')); // int(99)
echo var_dump(intval('shap99en')); // int(0)
?>
P.S Function copied from link above. Isn't mine.
I am trying to perform some calculations with a form but every time i try to work with checkboxes it goes wrong.
The checkboxes are beign set on value 1 in the form itselff and are being checked if there checked or not.
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0;
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
When i try to do calculations every works expect for the options with the checkboxes.
When both checkboxes (telefoon & netwerk) are selected the value should be 30.
If only one is selected the value should be 20.
But no mather what i have tried to write down it always give problem, and it always uses 20, never the value 30.
How do i solve this problem? Or suppose i am writing the syntax all wrong to lay conditions to a calculation? Any input appreciated.
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];
if ($oppervlakte <= 10)
$tarief = 100;
if ($oppervlakte > 10 && $oppervlakte <= 20)
$tarief = 90;
if ($oppervlakte > 20)
$tarief = 80;
if($verdieping == 1)
{
$prijsVerdieping = $oppervlakte * 120;
}
else
{
$prijsVerdieping = 0;
}
if(($telefoon == 1) && ($netwerk == 1))
{
$prijsCom = 30; // never get this value, it always uses 20
}
if(($telefoon == 1) || ($netwerk == 1))
{
$prijsCom = 20;
}
$prijsOpp = $tarief * $oppervlakte; // works
$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom; //prijsCom value is always wrong
Regards.
EDIT: full code below in 2 php files
<?php
if (!empty($_POST))
{
$standnaam = $_POST["standnaam"];
$oppervlakte = $_POST["oppervlakte"];
//value in the form van checkboxes op 1 zetten!
$verdieping = isset($_POST["verdieping"]) ? $_POST["verdieping"] : 0; //if checkbox checked value 1 anders 0
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
if (is_numeric($oppervlakte))
{
$_SESSION["standnaam"]=$standnaam;
$_SESSION["oppervlakte"]=$oppervlakte;
$_SESSION["verdieping"]=$verdieping;
$_SESSION["telefoon"]=$telefoon;
$_SESSION["netwerk"]=$netwerk;
header("Location:ExpoOverzicht.php"); //verzenden naar ExpoOverzicht.php
}
else
{
echo "<h1>Foute gegevens, Opnieuw invullen a.u.b</h1>";
}
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
<tr>
<td>Standnaam:</td>
<td><input type="text" name="standnaam" size="18"/></td>
</tr>
<tr>
<td>Oppervlakte (in m^2):</td>
<td><input type="text" name="oppervlakte" size="6"/></td>
</tr>
<tr>
<td>Verdieping:</td>
<td><input type="checkbox" name="verdieping" value="1"/></td>
<!--value op 1 zetten voor checkbox! indien checked is value 1 -->
</tr>
<tr>
<td>Telefoon:</td>
<td><input type="checkbox" name="telefoon" value="1"/></td>
</tr>
<tr>
<td>Netwerk:</td>
<td><input type="checkbox" name="netwerk" value="1"/></td>
</tr>
<tr>
<td><input type="submit" name="verzenden" value="Verzenden"/></td>
</tr>
</table>
2nd page with calculations:
<?php
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
$verdieping = $_SESSION["verdieping"];
$telefoon = $_SESSION["telefoon"];
$netwerk = $_SESSION["netwerk"];
if ($oppervlakte <= 10)
$tarief = 100;
if ($oppervlakte > 10 && $oppervlakte <= 20)
$tarief = 90;
if ($oppervlakte > 20)
$tarief = 80;
if($verdieping == 1)
{
$prijsVerdieping = $oppervlakte * 120;
}
else
{
$prijsVerdieping = 0;
}
if(($telefoon == 1) && ($netwerk == 1))
{
$prijsCom = 30;
}
if(($telefoon == 1) || ($netwerk == 1))
{
$prijsCom = 20;
}
$prijsOpp = $tarief * $oppervlakte; // werkt
$totalePrijs = $prijsOpp + $prijsVerdieping + $prijsCom;
echo "<table class=\"tableExpo\">";
echo "<th>Standnaam</th>";
echo "<th>Oppervlakte</th>";
echo "<th>Verdieping</th>";
echo "<th>Telefoon</th>";
echo "<th>Netwerk</th>";
echo "<th>Totale prijs</th>";
echo "<tr>";
echo "<td>$standnaam</td>";
echo "<td>$oppervlakte</td>";
echo "<td>$verdieping</td>";
echo "<td>$telefoon</td>";
echo "<td>$netwerk</td>";
echo "<td>$totalePrijs</td>";
echo "</tr>";
echo "</table>";
?>
Terug naar het formulier
</body>
</html>
One problem I've noticed are these lines,
if(($telefoon == 1) && ($netwerk == 1)) {
$prijsCom = 30; // will get set to 30.
}
if(($telefoon == 1) || ($netwerk == 1)) {
$prijsCom = 20; // will now be set to value of 20.
}
Here is why, if $telefoon and $netwerk are both 1, $prijsCom is set to the value of 30. It leaves that if block and goes down onto the next one, i.e.
if(($telefoon == 1) || ($netwerk == 1)) {
$prijsCom = 20;
}
It will evaluate to true since $telefoon == 1 evaluates to true and will override the value of $prijsCom to be 20.
Depending on how the code will be used, as a possible work-around, you could add the || condition first, so the value is set to 20 whether $telefoon or $netwerk is set to 1 and then check to see if they both are 1.
Update:
When looking at your code, I notice you are using $_SESSION variables, but you have not called session_start() at the beginning of the file,
<?php
session_start(); // <--you need to call this first
$standnaam = $_SESSION["standnaam"];
$oppervlakte = $_SESSION["oppervlakte"];
...
This may or may not be where your other problem lies, but whenever you use $_SESSION you need to call session_start first.
Call session_start(); after <?php.