I am trying to get the user input and verify if the answer is correct using an if and statement. How do I prompt the user for input within the same webpage? This is the code I have so far.
<?php
$answer = //user input
if ($answer = "4") {
echo " “Correct!!!! Congratulations!";
} else {
echo "Your answer was blank but the correct answer is 4.";
}
?>
This is the full form view to let you know how to perform the basic form submit and get values and displaying the result
<?php
if(isset($_POST['submit']))
{
$answer=$_POST['answer'];
if ($answer == "4") {
echo " “Correct!!!! Congratulations!";
} else {
echo "Your answer was blank or wrong input but the correct answer is 4.";
}
}
?>
<html>
<body>
<form method="post" action="index.php">
<input type="number" name="answer">
<input type="submit" name="submit" value="Submit">
</form>
Related
I'm very new to PHP and I've cobbled this together from some other answers on here. Can anyone show me how to get the $errMsg to display? At present, a blank or incorrect name leads to a blank page. Is this because the form isn't being displayed again? If so, how should I go about 'reloading' the form with the error message?
<?php
$name = "Fred";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["name"])) {
if ($_POST["name"] == $name) {
include("welcomeFred.php");
}
else {
$errMsg = "Incorrect name";
}
}
else {
$errMsg = "Name required";
}
}
else { ?>
<html>
...
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" name="name" required>
<span><?php echo $errMsg;?></span>
<input type="submit" value="Submit">
</form>
...
</html>
<?php } ?>
You shouldn't put the rendering of the form in the else of your if structure. This is the reason your form isn't loaded when you submit the form.
Remove the else { ?> and <?php } ?> at the end of your file and it should work fine.
This link leads to where my code is ran.
http://erobi022.pairserver.com/phpvalidate1.php
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input type="text" name="First"></p>
Please enter your last name: <input type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
Once the submit button is hit, all data is sent and posted here. I can get first and last name to output fine, but if i leave them blank it will only say that i left my first name field blank.
http://erobi022.pairserver.com/send_phpvalidate1.php
<!DOCTYPE html>
<html>
<body>
Welcome,
</P>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
if (empty($name1)) {
echo "You've entered nothing for first name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your first name is $name1 ";
}
}
echo "<br>";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name2 = $_POST["Last"];
if (empty($name2)) {
echo "You've entered nothing for last name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your last name is $name2 ";
}
}
?>
<?php // can have multiple php sections
echo "<a href='phpvalidate1.php'>Return to form</a></p>";
//have to use a simple qoute within html to make it work
?> </p>
Return to home page
</body>
</html>
You can just use that using HTML 5, simple
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input required="required" type="text" name="First"></p>
Please enter your last name: <input required="required" type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
No need to use two big ifs, just replace your PHP code with this :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>
First, whenever there is a die() statement,only the codes that appear before that run, the ones which come after don't get to run(in relation to your code.)
you can also trim your codes down this way and it will still work
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>
I have this piece of code and I want to show result in the third input box not in the input box and hole page and i want to know if i can a $_GET['$vairbles'] whiten functions
NOTE : I’m beginner in php programming
Here is my code :
<?php
$email = "mail#somedomain.com";
if (isset($_GET['txt1']) and isset($_GET['txt2'])) if (!empty($_GET['txt1']) and!empty($_GET['txt2'])) {
$tex1 = $_GET['txt1'];
$tex2 = $_GET['txt2'];
echo "They are all filled.<br>";
} else {
echo "Please fill in first and second Fields";
}
shwor();
Function shwor() {
global $email;
global $tex1;
global $tex2;
$len1 = strlen($tex1);
$len2 = strlen($tex2);
if ($len1 and $len2 > 0) if ($tex1 == $tex2) echo "matched ";
else echo "Does not match";
}
?>
<form action:"email.php" method="GET">
<input 1 type="text" name="txt1"><br><br>
<input 2 type="text" name="txt2"><br><br>
<input 3 type="text" name="Result" value = "<?php shwor();?>"><br><br>
<input type="submit" value="Check matches"><br>
</form>
everything works, just misspelled in this line:
<form action:"email.php" method="GET">
should be:
<form action="email.php" method="GET">
..and remember to name your file email.php that the data will be sent to the file itself ;-)
I have a simple php calculator which code is:
<html>
<head>
<title>PHP calculator</title>
</head>
<body bgcolor="orange">
<h1 align="center">This is PHP Calculator</h1>
<center>
<form method="post" action="phptest.php">
Type Value 1:<br><input type="text" name="value1"><br>
Type value 2:<br><input type="text" name="value2"><br>
Operator:<br><input type="text" name="sign"><br>
Result:<br><input type"text" name="result">
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</center>
<?php
if(isset($_POST['submit'])){
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
if($value1=='') {
echo "<script>alert('Please Enter Value 1')</script>";
exit();
}
if($value2=='') {
echo "<script>alert('Please Enter Value 2')</script>";
exit();
}
if($sign=='+') {
echo "Your answer is: " , $value1+$value2;
exit();
}
if($sign=='-') {
echo "Your answer is: " , $value1-$value2;
exit();
}
if($sign=='*') {
echo "Your answer is: " , $value1*$value2;
exit();
}
if($sign=='/') {
echo "Your answer is: " , $value1/$value2;
exit();
}
}
?>
All I want to do is that answer should be displayed in the result input field instead of echoing them separately. Please help? I Know it's simple but I am new in PHP.
One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input> tag.
Try this -
<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
...
//Adding to $result variable
if($sign=='-') {
$result = $value1-$value2;
}
//Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">
inside the Form, You can use this code. Replace your variable name (i use $variable)
<input type="text" value="<?php echo (isset($variable))?$variable:'';?>">
Try this
<input class="qtytext-box" type="number" value= <?php echo $colll2; ?> >
I am using this code in my *.php file
$check = $_POST['dati'];
if (strlen($check) != 0) {
// calculations
}
else {
echo "contact me at <a href='mailto:myemail'></a>";
}
The dati inside that POST is the name attribute of an input field. If the input has a length = 0, that echo must be displayed.
I have the same code in another part of my server and it works perfectly. I'm having troubles here because when the length of dati is 0, the script makes the calculations instead of showing the echo.
<input name="dati" id="dati" style="width:310px" type="text">
This is the code of the input. Any ideas?
You code works well as you can see here: http://codepad.org/fCvlokOJ and here http://codepad.org/t2TvFozt
<?php
$_POST['dati']= "text";
if (strlen($_POST['dati']) != 0) {
echo" calculations";
}
else {
echo "contact me at <a href='mailto:myemail'></a>";
}
In the following example, if field is left blank, echo'ed message is "sorry".
If text entered, then Email link appears. TESTED
<?php
if(isset($_POST['submit']))
$check = $_POST['dati'];
if (strlen($check) == 0) {
echo "Sorry";
}
else {
echo "Contact me at <a href='mailto:myemail'>LINK</a>";
}
?>
FORM
<form method="post" action="your_handler.php">
<input name="dati" id="dati" style="width:310px" type="text">
<input type="submit" name="submit" value="Submit">
</form>