I am new to PHP and I was making this form and I wanted to print some data but it is not displaying. What is wrong with it? Here's the code:
<form name="input" action="check.php" method="get">
Unit number:
<input type="number" name="unit" />
<input type="submit" value="Submit" />
</form>
<table>
<tr><td class="check-table">
<?php
if($_GET[unit] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
</td></tr></table>
Please Help?
The better way would be:
if (empty($_GET['unit'])) {
$output="<p>Please Enter A Unit Number</p>";
echo $output;
}
The reasons:
You check if variable exists
You use ' quotes for array key name
You output $output variable only if it is necessary. And in your case - you output it even if it doesn't exist
You've also confused == (comparison operator) and = (assignment operator)
I think you missed the single quotes in the $_GET['unit']
<?php
if($_GET['unit'] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
Related
Newbie here, and self-taught in PHP. I have a questionnaire where each question has 2 answers, and users can add any combination of numbers to each, as long as they equal, i.e. 10. So far 2 questions (will be more) so each question's answers should equal 10, therefore total submitted values should equal 20. I can't find a way to only allow submit if these conditions are met. I would really appreciate any help.
Currently using this for testing purposes:
<input type="submit" value="Check!" name="check"/>
...at the bottom.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<?php
// Adding stuff
if(isset($_POST['check']))
{
$q1total=$realist[1]+$idealist[1];
$q2total=$realist[2]+$idealist[2];
$grandtotal=$q1total+$q2total;
}
?>
<body>
<form method="post">
Q1: <input type="text" name="realist[1]"/> <input type="text" name="idealist[1]"/>
<?php echo $q1total; ?>
<br>
Q2: <input type="text" name="realist[2]"/> <input type="text" name="idealist[2]"/>
<?php echo $q2total; ?>
<br>
<br><br>
Grand total: <?php echo $grandtotal; ?>
<br><br>
<input type="submit" value="Check!" name="check"/>
</form>
</body>
</html>
I'm also a self-taught PHP "newbie" as you describe it. I would honestly create a seperate PHP file that checks if the conditions are met.
Your form would be:
<form method="post" action="yourfile.php">
And in the php file:
$realist1 = $_POST["realist\[1\]"];
$idealist1 = $_POST["idealist\[1\]"];
$realist2 = $_POST["realist\[2\]"];
$idealist2 = $_POST["idealist\[2\]"];
if ($realist[1]+idealist[1]== 10 && $realist[2]+idealist[2] == 10)
{
echo "<button value='correct'/>";
}else{
echo "<button value='incorrect' disabled/>";
}
Obviously you have to adjust some stuff but hopefully this can help.
I'm learning PHP and trying to understand the if .. else statements a little better, so I'm creating a little quiz. However, I have come across an issue and I don't seem to know what the issue is. My problem is that whenever I type in the age in the input area, it will give me the $yes variable every time even if I enter the wrong age.
Here is my code so far:
My html file:
<form action="questions.php" method="post">
<p>How old is Kenny?<input></input>
<input type="submit" name="age" value="Submit"/>
</p></form>
My php file:
<?php
$age = 25;
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25){
echo "$yes";
}else{
echo "$no";
}
?>
You catch the user input inside the $_POST superglobal var (because the method of your form is POST.
So
<?php
$age = 25;
should be
<?php
$age = $_POST['age'];
There is an error in HTML too. This
<input type="submit" name="age" value="Submit"/>
should be
<input type="text" name="age" value=""/>
<input type="submit" value="Click to submit"/>
Because you want one input and one button. So one html element for each element.
and <input></input> must be cleared because it's not valid syntax :-)
<form action="questions.php" method="post">
<p>How old is Kenny?</p><input type="text" name="age"></input>
<input type="submit" value="Submit"/>
</form>
$age = (int) $_POST["age"];
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25) {
echo $yes;
} else {
echo $no;
}
<?php
/* Test that the request is made via POST and that the age has been submitted too */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['age'] ) ){
/*
ensure the age is an integer rather than a string ..
though for this not overly important
*/
$age=intval( $_POST['age'] );
if( $age==25 ) echo "Congratulations";
else echo "Bad luck!";
}
?>
<form action="questions.php" method="post">
<p>How old is Kenny?
<input type='text' name='age' placeholder='eg: 16' />
<input type="submit" value="Submit" />
</p>
</form>
A simple html form, note that the submit button does not carry the values you want to process, they are supplied via the input text element.
First of all, you need to echo the variable; echoing "$no" will keep it as a string. Remove the quotes from "$no" and "$yes" in your if then statement. Otherwise, your code seems sound!
I want to retrieve the data from a textbox which I created. Please take look at my code and help me.
<?php
if(isset($_GET['ok']))
{
$a=1;
$n=$_GET['n'];
for($i=0;$i<$n;$i++){
echo '<form action="exa.php" method="get">';
echo '<input type="text" name="kal'.$a.'"/> <br/>';
echo '</form>';
$a++;} $a=1;
for($i=0;$i<$n;$i++)
{
$txtnm="kal".$a;
$kal=$_GET['$txtnm'];
echo $kal;
$a++;
}
}
?>
<html>
<body>
<form action="exa.php" method="get">
<input type="text" name="n"/><br/>
<input type="submit" value="OK" name="ok"/>
</form>
</body>
</html>
Here I am getting an error saying 'undefined index $txtnm'
here i am getting error that undefined index $txtnm ...
$kal=$_GET[$txtnm];
remove the single quotes, you are treating it as constant if you put those single quotes
Try to do like this
echo 'Input '.$a.'<input type="text" name="kal[]"/> <br/>';
and after submitting form the kal array like
print_r($_REQUEST['kal']);
And why you are using get method,without specific need dont use get method while submitting the textarea data,because some of special characters causes your redirect
Remove single quote from $kal=$_GET['$txtnm'] . It should be
$kal=$_GET[$txtnm];
Well then, this is likely to be the n-th time someone is asking this, but honestly I didn't grab anything useful spending the last hour or so on Google. What I want to do is rather trivia, or so I thought. I have this working in Java Script but want to move it to PHP. In brief:
declare a var with a static value
add text field into which user is asked to enter value of above var
check if field is a) empty, b) non-empty mismatch, or c) non-empty match
My (limited) PHP wisdom has lead me into believing it ought to be something like the below, but apparently it's not. I'd very much appreciate any insight, tha.
<?php
$coconew = "blah";
if (isset ($_POST["cocosub"])) {
if ($_POST["cocoval"] == "") {
echo "empty";
} else {
if ($_POST["cocoval"] != $coconew) {
echo "mismatch";
} else {
echo "match";
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" id="cocosub" method="post">
<div>
<?php echo $coconew; ?>
<input type="text" id="cocoval">
<input type="submit">
</div>
</form>
You need to change
<input type="text" id="cocoval">
to
<input type="text" name="cocoval">
There are other (and probably better) ways to do this, but you are on the right track.
$_POST only looks for the name attribute of form elements, so modify your form as such:
<?php
$coconew = "blah";
if (isset ($_POST["cocoval"])) {
if ($_POST["cocoval"] === "") {
echo "empty";
} else {
if ($_POST["cocoval"] !== $coconew) {
echo "mismatch";
} else {
echo "match";
}
}
}
?>
<form id="cocosub" method="post">
<div>
<?php echo $coconew; ?>
<input type="text" id="cocoval" name="cocoval">
<input type="submit">
</div>
</form>
(I made a few other changes, you want to check isset on the element, not the form, it will POST to the same page if you don't give it an attribute [so no need to add the echo], and adding better type checking in your php)
in addition to the other answers already posted, you might also be interested in PHP's session support (depending on how "static" you need your static variables to be). That's where you'd put $cocoval and any other variables if you need to save their values across multiple requests for the same URL by the same user. See here for more info:
http://php.net/manual/en/features.sessions.php and
http://www.php.net/manual/en/book.session.php
This works:
<?php
session_start();
if(isset($_POST["cocosub"])){
$input = trim($_POST["cocoval"]);
if($input == ""){
echo "empty";
} elseif($input != $_SESSION["coconew"]){
echo "mismatch";
} else {
echo "match";
}
}
$_SESSION["coconew"] = substr(md5(uniqid()), 0, 5);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="cocosub" method="post">
<div>
<?php echo $_SESSION["coconew"]; ?>
<input type="text" id="cocoval" name="cocoval">
<input type="submit" name="cocosub">
</div>
</form>
You needed to add name="cocosub" to the Submit button element in order for the first if(isset(...)) condition to be true. That's why the script didn't work. Also, instead of id, you need to use the name="cocoval" in the input text field as well in order for it to carry over into $_POST.
I am just starting to learn php, how would I initiate a echo statement after a submit button is pushed, or even a anchor tag.
Here is my code so far
form name="myform" method="get" actions="madlib01.php"
Name: <input type="text" name="name" /> <br />
<input type="submit" name="submit" />
form
<?php
$Name = $_GET['name'];
$hello .= "Hello $Name";
echo $hello //I would prefer the echo to happen after the submit button is hit
?>
the correct attribute for your form tag is "action", not "actions"
When the form is submitted, a new request is sent to the server (in your case, using GET).
So to do it all in one page:
form.php:
<form action="form.php" method="GET">
<input type="text" name="name"/>
<input type="submit">
</form>
<?PHP
if (! empty($_GET['name'])){
echo 'Hello, ' . $_GET['name'];
}
?>
You will first need to check if PHP has received your GET parameter using isset or array_key_exists:
if(isset($_GET['name']) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
}
or:
if(array_key_exists('name', $_GET) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
} else {
//example: default to something if nothing has been passed
echo "Hello Guest";
}
Also note, if you're submitting to the same page, you can omit the action attribute from your form tag altogether:
<form method="GET">
echo $hello
You've just gained an HTML-injection vulnerability. If someone sends your user to:
http://www.example.com/madlib01.php?name=<script>stealYourCookies()</script>
you've got problems.
Yes, this is a My First PHP Script. That doesn't make security optional. This is a mistake every tutorial makes: teaching bad practice from the start, treating correctness (and security, which is a subset of correctness) as an optional extra.
The result is that most PHP code out there is full of holes. But there's no need for yours to be! Every time you place a pure-text string into a surrounding HTML context, escape it properly:
echo htmlspecialchars($hello);
I tend to define a function with a shorter name than ‘htmlspecialchars’ to do that for me, as I'm lazy.
<?php
function h($text) {
echo(htmlspecialchars($text, ENT_QUOTES));
}
$name= '';
if (isset($_REQUEST['name']))
$name= trim($_REQUEST['name']);
?>
...
<?php if ($name!=='') { ?>
<p> Hello, <?php h($name); ?>! </p>
<?php } ?>
<form method="get" action="madlib01.php">
<p>
<label for="namefield">Name:</label>
<input id="namefield" type="text" name="name" />
</p>
<p>
<input type="submit" />
</p>
</form>
Now if you say your name is Mister <script>, the page will greet you exactly as such, angle brackets and all, instead of trying to run JavaScript. This is the correct output and thus also secure.