As stated above, when I try to send a form using php my if statement is not being triggered and the variable is not being set to my desired value.
Relevant PHP:
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['time_pickedm']) == "9:00"){
$timepicked = "09:00:00";
}
}
Relevant HTML:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<input type="button" class="<?php echo $buttoncolour ?>" name= 'time_pickedm' value = "9:00">
<input type="submit" class="btn btn-primary" name="Submit">
</form>
Any help would be appreciated
Your condition is wrong, try:
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['time_pickedm']) && $_POST['time_pickedm']== "9:00"){
$timepicked = "09:00:00";
}
}
EDIT:
I think i found it!
in your html you have 2 buttons, so the time_pickedm is not going to your php. try replacing button for text:
<input type="text" class="<?php echo $buttoncolour ?>" name= 'time_pickedm' value = "9:00">
You if condition invalid .If time_pickedm is present in array is always true .
its just like if(true == "9:00")
So Change to separate validate the condition
if(isset($_POST['time_pickedm']) && $_POST['time_pickedm'] == "9:00")
Related
I can't seem to apply the $_POST function properly to retrieve the data from a simple HTML form. I'm new to PHP, so I may be overlooking something simple.
I have a form with action directing to the same page, but if the form is filled out, the value of $_POST['input_name'] will have changed, so I can trigger the php function using that condition. Is this the best way to structure this kind of action?
Here's my HTML code (thispage.php is the current/same page):
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" />
</form>
Here's my PHP code from the same page:
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
Thanks a lot in advance!!
First remove the action from form if your server side code is in the same page. And Use the empty() or isset() functions for checking the value.
For Example:
if(!empty(trim($_POST['userName']))){
echo $_POST['userName'];
}
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
if("" == trim($_POST['userName'])){
echo $_POST['userName'];
}
This is actually checking if the value is empty and echoes it if it is.
You probably want to use !empty($_POST['userName']) to check if it's not empty and then echo it if it is not.
try this:
HTML code
<form action="thispage.php" method="post">
Name: <input type="text" name="userName" id="userName" />
<input type="submit" name="submit" />
</form>
PHP code on the same page:
if(isset($_POST['submit']))
{
if(isset(trim($_POST['userName']))){
echo $_POST['userName'];
}
}
Try this...
if(trim($_POST['userName']) != ' '){
echo $_POST['userName'];
}
you can try it:
if(isset($_POST['userName'])){
$name = $_POST['userName'];
echo $name;
}
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!
In PHP is this enough to guarantee a form has been submitted by clicking the form submit button and to verify the content posted is not empty?
if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['field_data']))
{
echo "ok";
}
I think, there may be a way to be sure the form was submitted using your form.
If I would like to do it, I think I will make something like this :
$secure = $_SESSION['form']['submit'] = MD5(time());
<form>
<input type='hidden' name='secure_form' value='<?php echo $secure ?>' />
</form>
And else when submitted check the value :
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['secure_form']) && $_SESSION['form']['submit'] == $_POST['secure_form']) {
//do stuff
}
Of course, you have to add session_start() at the top of the page!
first you need to check $_SERVER['REQUEST_METHOD'] output so best way to convert output in upper
if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
Then you can check with submit button name also like
<input type="hidden" id="submitted" name="submitted" value="yes"/>
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST' && isset($_POST['submitted']) && $_POST['submitted'] == 'yes'){
also you can check all values of form which will be submitting by isset() or empty()
I tend to use a hidden form field
<?php
$csrf_token = md5(time().'random string');
$_SESSION['csrf'] = $csrf_token;
?>
<input type="hidden" id="submitted" name="submitted" value="yes"/>
<input type="hidden" id="csrf" name="csrf" value="<?php echo $csrf_token; ?>"/>
Then in my PHP I'd use something like:
if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submitted'] == 'yes' && $_POST['csrf'] == $_SESSION['csrf']){
// Do something
echo 'Form submitted via POST';
}
Updated to include a CSRF field
I followed the suggestion in this post:
How to read if a checkbox is checked in PHP?
I am trying to get values of checkboxes in form. I have tried separating the conditionals and using them together like this, checking isset and trying to get value. Either way it always returns "NO". What am I doing wrong?
if (isset($_POST['sign']) && $_POST['sign'] == 'yes-checked-sign') {
$check2 = "YES";
} else {
$check2 = "NO";
}
<input type="checkbox" name="sign" value = "yes-checked-sign" />
Checking for isset($_POST['sign']) should be sufficient.
Make sure the checkbox is surrounded by a form element because you might be not actually POSTing it.
<form method="POST" action="foo.php">
<!-- Your checkbox goes here. -->
</form>
For checking if checkbox is checked:
if (isset($_POST['sign'])) {
$check2 = "YES";
} else {
$check2 = "NO";
}
Form with pre-checked checkbox:
<form method="post">
<input type="checkbox" name="sign" checked/>
<input type="submit" value="submit"/>
</form>
Hope this helps.
every time i am refreshing the page and i am getting the same value stored in the post array.
i want execution of echo statement only after submit and after refreshing no echo results..
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
From just a form, you won't be able to check if it was a refresh, or a first submit, regardless of using GET or POST method.
To ensure a single message, you need to:
a. redirect the user to somewhere else after you processed the request.
if(isset($_POST['submit'])) {
// process data
header("Location: new-url");
}
And display the message on the other URL.
b. set a cookie / session variable, which tells you the form was already processed.
if(isset($_POST['submit']) && !isset($_SESSION['form_processed'])) {
$_SESSION['form_processed'] = true;
}
This second approach will kill your form until the user closes the browser, so you should do something more complex - like storing another hidden field in the form, and storing that in the session.
If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.
An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.
Within the form:
<input type="hidden" name="time" value="<?php echo $time; ?>" />
In the PHP:
session_start();
if(isset($_POST['submit']))
{
if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
{
echo "User name : <b> $name </b>";
}
}
$time = $_SESSION['time'] = time();
Another option is to redirect after processing the post data:
if(isset($_POST['submit']))
{
...
...
header('Location: ' . basename($_SERVER['PHP_SELF']));
exit();
}
You need to maintain a state as to whether $name has already been displayed or not. The easiest way is probably to maintain that state in a browser cookie.
<?php
$nonce = $_COOKIE['nonce'];
$new_nonce = mt_rand();
setcookie('nonce', $new_nonce);
if(isset($_POST['submit']) && $_POST['nonce'] == $nonce)
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="nonce" value="<?php echo $new_nonce ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Problems
you are polluting the user “session” with stale variable.
this will break if your user opens several windows (or tabs) to the same page. To fix this you would have to change the nonce cookie into an array of nonces, and update it accordingly.
if you want refresh page after submit use
<form method="get"
sure if your form hasn't a lot of data and also need to use $_GET instead of $_POST variable:)
correct way for you, but this logic is not good, need to refactor this script:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
unset($_POST['submit']);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>