I tried to make a quadratic equation solver in php:
index.html:
<html>
<body>
<form action="findx.php" method="post">
Find solution for ax^2 + bx + c<br>
a: <input type="text" name="a"><br>
b: <input type="text" name="b"><br>
c: <input type="text" name="c"><br>
<input type="submit" value="Find x!">
</form>
</body>
</html>
findx.php:
<?php
if(isset($_POST['a'])){ $a = $_POST['a']; }
if(isset($_POST['b'])){ $b = $_POST['b']; }
if(isset($_POST['c'])){ $c = $_POST['c']; }
$d = $b*$b - 4*$a*$c;
echo $d;
if($d < 0) {
echo "The equation has no real solutions!";
} elseif($d = 0) {
echo "x = ";
echo (-$b / 2*$a);
} else {
echo "x1 = ";
echo ((-$b + sqrt($d)) / (2*$a));
echo "<br>";
echo "x2 = ";
echo ((-$b - sqrt($d)) / (2*$a));
}
?>
the problem is that it's returning wrong answers (d is right,x1 and x2 are not) seems like sqrt() is returning zero or maybe something else.
There's a typo in this line:
elseif($d = 0)
which is assigning the value 0 to $d instead of comparing it. That means you are always evaluating sqrt(0), which is 0, in your else block.
It should be:
elseif($d == 0)
Related
I am trying to create a simple grading system where if a certain value is inputted in a textbox and submitted, it will show a grade on the next page, what I get instead is an error 500 code, I believe it has something to do with the way I'm writing the code.
I'm not sure whether writing it like this is correct:
if($a <= 60) {
echo = "F" }
elseif...
<body>
<center>
<div class="container">
<form action="cek_grade.php" method="post"> <!--Action: Sent to "cek_grade.php" Method: The data will be displayed by "post"-->
Nilai: <input type="integer" name="nilai"> <!--The name of the numbers inputted are "nilai"-->
<br><br>
<input type="submit" id="submit" name="submit" value="Submit">
<input type="reset" id="reset" name="reset" value="Reset">
</form> <!--All the data above should get sent to the page called "cek_grade.php"-->
</div>
</center>
</body>
<?php
nilai = $_POST ['nilai'];
echo = "$nilai"
if($nilai <= 60) {
echo = "F"
} elseif ($nilai <= 70) {
echo="C"
} elseif ($nilai <= 80) {
echo="B"
} else {
echo="A"
>
What I'm trying to do is if variable "$nilai" is a certain range of value,, it will display a grade of either F, C, B or A
There are several things wrong with your script:
Your first reference to $nilai is missing the $.
You can't assign to echo.
You're missing a semicolon at the end of most lines.
You have an invalid space in $POST ['nilai].
Here's a working version:
$nilai = $_POST['nilai'];
echo "$nilai";
if($nilai <= 60) {
echo "F";
} elseif ($nilai <= 70) {
echo "C";
} elseif ($nilai <= 80) {
echo "B";
} else {
echo "A";
}
You missed a lot of things
<body>
<center>
<div class="container">
<form action="cek_grade.php" method="post"> <!--Action: Sent to "cek_grade.php" Method: The data will be displayed by "post"-->
Nilai: <input type="integer" name="nilai"> <!--The name of the numbers inputted are "nilai"-->
<br><br>
<input type="submit" id="submit" name="submit" value="Submit">
<input type="reset" id="reset" name="reset" value="Reset">
</form> <!--All the data above should get sent to the page called "cek_grade.php"-->
</div>
</center>
</body>
<?php
$nilai = $_POST['nilai'];
echo $nilai;
if($nilai <= 60) {
echo "F";
} elseif ($nilai <= 70) {
echo "C";
} elseif ($nilai <= 80) {
echo "B";
} else {
echo "A";
}
?>
I will not go into HTML part but here is your corrected PHP script
<?php
$nilai = $_POST ['nilai'];
echo $nilai;
if($nilai <= 60) {
echo "F";
} elseif ($nilai <= 70) {
echo "C";
} elseif ($nilai <= 80) {
echo "B";
} else {
echo "A";
}
?>
I am trying to create an adding game PHP and HTML, where the user guesses the sum of two random integers. However, when the user submits the answer, the integers randomize again, changing the sum and making the user's guess incorrect.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit']))
{
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I am expecting the output to be "nice, you got it right" when the $guess==$sum, but $sum changes when the form is submitted, making the 2 variables unequal.
Try to use session and store the two random number. PHP Session
just like this.
<?php
session_start();
if (isset($_POST['submit']))
{
$sum = $_SESSION['y'] + $_SESSION['x'];
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
$_SESSION['x']= (rand(1,10));
$_SESSION['y']= (rand(1,10));
echo "<br>What is " . $_SESSION['x'] . " + " . $_SESSION['y'] . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I hope I answer your problem. just read the PHP session it helps you a lot.
You can create a hidden input field to post the sum value. Then compare that value with the guessed value.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit'])) {
$guess = $_POST['guess'];
$value = $_POST['sum'];
if ($value == $guess) {
echo "nice, you got it right.";
} else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input type="hidden" name="sum" value="<?=$sum?>" />
<input name="submit" type="submit" />
</form>
You should store your $x and $y in a hidden input.
Calculate the sum by using the post value instead of generating new value.
<form id="form1" name="form1" method="post" action="">
<input name="txt1" type="text" /><br />
<input name="txt2" type="text" /><br />
<input name="txt3" type="text" /><br />
<input name="s" type="submit" value="Find No"/>
</form>
Here is my html code.
<?php
$a = 10;
$b = 25;
$c = 20;
if($a > $b)
{
if($a > $c)
{
echo "a is biggest number";
}
else
{
echo "c is biggest number";
}
}
if($b > $c)
{
echo "b is biggest number";
}
?>
` Here is the code for finding
largest among three no.s . I wish to get largest and second largest no
Given an array of numerical strings obtained from the input fields, for example one that mirrors your original values:
$a = array('1', '5', '19', '200', '999');
you can convert it into a true numerical array using the following approach:
$a = array_map('intval', $a);
and then proceed exactly as you did before:
$last = max($a);
$second = max(array_diff($a,[$last]));
Fiddle here.
you have to use input box for this and a submit button for very simple basic working model.
HTML:
<form action="/path to your php file or leave blank if both html and php are in same file">
<input type="text" name="arr" value=''>
<input type="submit" name="submit" value="submit">
</form>
// now user can provide input with space in input box such as 9 11 13 15 16 29 etc.
PHP
<?php
if(isset($_REQUEST['arr']) && !empty($_REQUEST['arr'])){
$a = explode(' ', $_REQUEST['arr']);
$last = max($a);
$second = max(array_diff($a,[$last]));
echo $second;
echo $last;
?>
I think this will resolve your query.
Here we use reverse array sort with largest and second largest number
$result = array(1,5,19,200,999);
$count = count($result);
for($i=0;$i<$count;$i++){
for($j=$i+1;$j<$count;$j++){
if($result[$i]<$result[$j]){
$m = $result[$i];
$result[$i] = $result[$j];
$result[$j] = $m;
}
}
}
echo "Largest Number : ".(!empty($result[0])?$result[0]:FALSE)." Largest Second Number : ".(!empty($result[1])?$result[1]:FALSE);
I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.
<form name="calculator" action="" method="post">
From: <input class="inputbox" type="number" name="one" value="" /><br />
<p>to</p><br>
To: <input class="inputbox" type="number" name="two" value="" /><br />
<input type="submit" class="submit" name="submit" value="Get Conversions!" />
</form>
<br>
<table border='1' cellpadding='5px'>
<tr>
<th>Degrees Celsius</th>
<?php
if ($_POST['submit']) {
$one = $_POST['one'];
$two = $_POST['two'];
}
if ($two < $one) {
echo "<p>Please put the lowest number in the first input box.</p>";
} else if ($one < (-273) OR $two < (-273)) {
echo "<p> Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
} else {
$c = $one - 1;
do {
$c++;
echo "<td>" . $c . "</td>";
} while ($c < $two);
}
?>
</tr>
<tr>
<th>Degrees Fahrenheit</th>
<?php
$f = (1.8 * $c) + 32;
do {
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
</table>
Your have 2 problems in your code.
One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.
you should "reset" C after The first while loop like here:
<th>Degrees Fahrenheit</th>
<?php
$c = $one - 1;
Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this
Degrees Fahrenheit
<?php
$c = $one - 1;
do {
$f = (1.8 * $c) + 32;
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
Hi guys I am doing this user calculation form and for some reason when I addition button 0 + 0 it gives me this error Notice: Undefined variable: add in /var/www/html/1018/5356684c357e7ff13ae21450a17d877d/Test-YT/index.php on line 44
other than that, it works good for all other numbers this is my php code
<html>
<body>
<style><?php include "style.css" ?></style>
<form action="index.php" method="POST">
<table border="1">
<td>
<p>insert value one: <input type="text" name="num1"> <br>
<p>insert value two: <input type="text" name="num2"> <br>
</td>
<td>
<input type="submit" name="add" value="Addition">
<input type="submit" name="sub" value="Subtraction">
<input type="submit" name="mult" value="Multiplication">
<input type="submit" name="div" value="Division">
<input type="submit" name="all" value="Display All">
</td>
</table>
</form>
<div id="answers">
<?php
if (isset($_POST['num1']) && ($_POST['num2'])){
$val1 = $_POST['num1'];
$val2 = $_POST['num2'];
$add = $val1+$val2;
$sub = $val1-$val2;
$mult = $val1*$val2;
$div = $val1/$val2;
}
if (isset($_POST['add'])){
echo $add;
}
else{
echo "zero";
}
if (isset($_POST['sub'])){
echo $sub;
}
if (isset($_POST['mult'])){
echo $mult;
}
if (isset($_POST['div'])){
echo $div;
}
if (isset($_POST['all'])){
echo $add . "<br>" . $sub . "<br>" . $mult . "<br>" . $div . "<br>";
}
?>
</div>
</body>
</html>
if (isset($_POST['num1']) && ($_POST['num2'])){
if $_POST['num2'] is 0 this expression calculated to false and variable $add is not initialized, as other variables as well.
edit:
As suggested below, you probably wanted something like:
if (isset($_POST['num1']) && isset($_POST['num2'])) {
But also looks like you tried to prevent division by zero error. As a better solution, you can define all vars at first and also check non-zero value only before division.
$add = $sub = $mult = $div = 0;
if (isset($_POST['num1']) && isset($_POST['num2'])){
$val1 = $_POST['num1'];
$val2 = $_POST['num2'];
$add = $val1+$val2;
$sub = $val1-$val2;
$mult = $val1*$val2;
if ($val2) {
$div = $val1/$val2;
}
}
So you can be sure script will not die even if num1 and num2 were not submitted.