What is the best way of checking whether or not a form has been submitted to determine whether I should pass the form's variables to my validation class?
First I thought maybe:
isset($_POST)
But that will always return true as a superglobal is defined everywhere. I don't want to have to iterate through each element of my form with:
if(isset($_POST['element1']) || isset($_POST['element2']) || isset(...etc
Whilst writing this question I thought of a much more basic solution, add a hidden field to act as a flag that I can check.
Is there a 'cleaner' way to do it than adding my own flag?
For general check if there was a POST action use:
if ($_POST)
EDIT: As stated in the comments, this method won't work for in some cases (e.g. with check boxes and button without a name). You really should use:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
How about
if($_SERVER['REQUEST_METHOD'] == 'POST')
Actually, the submit button already performs this function.
Try in the FORM:
<form method="post">
<input type="submit" name="treasure" value="go!">
</form>
Then in the PHP handler:
if (isset($_POST['treasure'])){
echo "treasure will be set if the form has been submitted (to TRUE, I believe)";
}
Use
if(isset($_POST['submit'])) // name of your submit button
if ($_SERVER['REQUEST_METHOD'] == 'POST').
Try this
<form action="" method="POST" id="formaddtask">
Add Task: <input type="text"name="newtaskname" />
<input type="submit" value="Submit"/>
</form>
//Check if the form is submitted
if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['newtaskname'])){
}
On a different note, it is also always a good practice to add a token to your form and verify it to check if the data was not sent from outside. Here are the steps:
Generate a unique token (you can use hash) Ex:
$token = hash (string $algo , string $data [, bool $raw_output = FALSE ] );
Assign this token to a session variable. Ex:
$_SESSION['form_token'] = $token;
Add a hidden input to submit the token. Ex:
input type="hidden" name="token" value="{$token}"
then as part of your validation, check if the submitted token
matches the session var.
Ex: if ( $_POST['token'] === $_SESSION['form_token'] ) ....
I had the same problem - also make sure you add name="" in the input button. Well, that fix worked for me.
if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['add'])){
echo "stuff is happening now";
}
<input type="submit" name="add" value="Submit">
You could also use:
is_array($_POST)
Related
using codeigniter mvc pattern I create form in view that take only two values form user
<form action="<?php base_url(); ?> blogs/new_post" method="POST">
<label>Title</label>
<input type="text" name="post_title" />
<label>discription</label>
<input type="text" name="post_detail" />
<input type="submit" value="post" />
</form>
now when i submit the form, data goes to the controller now here confusion created in my code that i can't able to understand i use three cases in controller fist is if i use !empty($_POST) in controller and in view weather i fill the form or not fill the form message displayed in controller is post
my question is why always displaying post why not displaying not a post when i fill nothing in the form
if(!empty($_POST)) {
echo "post";
} else {
echo "not a post";
}
my second question is same related to the first condition now i use isset instead of !empty
public function new_Post() {
if(isset($_POST)) {
echo "post";
} else {
echo "not a post";
}
}
in this case either i fill form or not fill form when i submit the form the result always same that is "post"
and in third case if i use !isset the the result is always not a post eiter i fill or not fill the form
hope so you will understand my problem when i comes to if(!empty($_POST)) this condition then my mind is confuse what is the purpose of $_post
This is because you are using the whole global variable $_POST to check empty and isset(). $_POST is not empty when you click on the button. You just Print_r the $_POST it will have the value of submit button. You need to print the value of $_POST on click and see the values in the array
Well, in CodeIgniter (and most of the framework) doesn't allow you to access $_POST directly due to security reason.
You must access $_POST values through $this->input->post()
For more information read Input Class from CodeIgniter's docs
https://www.codeigniter.com/user_guide/libraries/input.html
$_POST is exist when you click on the button:
so empty and isset cant work Correctly
the value at the first is
Array
(
)
SO U should check
if($_POST)
instead
if(!empty($_POST)) OR if(isset($_POST))
AND best way is u check
if ($this->input->post())
I have a form where when the user clicks submit, I need a php file to be ran. below is the form and the php file.
<form action="php_scripts/test.php" method="POST">
<input name="feature" type = "text" placeholder="Feature" />
<input name="feature2" type = "text" placeholder="Feature2" />
<input type="submit" value = "submit"/>
</form>
test.php
<?php
if( isset($_GET['submit']) )
{
$feature = $_POST['feature'];
// do stuff (will send data to database)
}
?>
The problem I am having is that when I press Submit on the form,
if( isset($_GET['submit']) )
Always returns false.
Can anyone explain why that is? Have I totally misunderstood how to implement form sending data to php scripts?
Apologies if I have made any syntax errors and many thanks for any help you can give.
There are a few things wrong with your code.
You're mixing GET with POST methods. Plus, add values to your inputs and your submit button isn't named, which you're trying to use as a conditional statement for.
HTML
<form action="php_scripts/test.php" method="POST">
<input name="feature" value="feature" type = "text" placeholder="Feature" />
<input name="feature2" value="feature2" type = "text" placeholder="Feature2" />
<input type="submit" name="submit" value = "submit"/>
</form>
PHP
<?php
if( isset($_POST['submit']) )
{
$feature = $_POST['feature'];
$feature2 = $_POST['feature2'];
// do stuff (will send data to database)
}
?>
Sidenote: You could/should also check against empty values.
if(isset($_POST['submit'])
&& !empty($_POST['feature'])
&& !empty($_POST['feature2']) ) {...}
Footnotes:
Seeing that you're intending on sending to DB:
I hope you plan on using mysqli with prepared statements, or PDO with prepared statements.
A couple of things:
you're using $_GET instead of $_POST
isset($_POST['submit']) is not a good check, not every browser will send the submit button in its request. (Apart from the fact that you haven't even named the submit button, so it wouldn't come through in any browser, as it stands now.)
it's better to use:
Code:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
}
You missed to name the submit button. So no entry in the $_POST/$_REQUEST array is given. Depending on the php settings you might want to use array_key_exists() to check for an index in the array as isset might throws an error.
I have a form, which redirects the user to "page1.php" after the form is submitted. What I want to do is to redirect the user to "page2.php" after the form is submitted, but I need to make sure that the POST request was sent. Example:
<form action="page1.php" method="POST">
<input type="text" name="username" />
<input type="text" name="age" />
<input type="submit" value="" />
</form>
When the user clicks on Submit, it redirects him to page1.php. I want to redirect him to page2.php, but I need to make sure that the data is sent to the server. I can't use AJAX. Is there any way to do it with cURL or something like that? Any examples?
Thanks!
I guess this works !!
In page1.php
<?php
//do establish session
//and check for the input fields obtained via $_POST
if(isset($_POST['name_of_your_field']) && !empty($_POST['name_of_your_field'])){
if(!mail($to,$subject,$message)){
header('location:form.php?msg=error');
}else{
header('location:page2.php?msg=succes');
}
}
?>
You can check if the POST request was sent with something like:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// do something...
}
You can create a hidden input in your form and send additional info about the form that is submitted, e.g. action.
Inside you will do your magic and redirect user with:
header('Location: page2.php');
exit();
In your 'page1.php' processor, add a 'header' redirect to 'page2.php'.
header("Location: page2.php");
exit;
you could do a check if query is complete
example
<?php
$query=mysqli_query(".......your query statement")or trigger_error(mysqli_error());
if($query){
header("Location:page2.php");
}
else{
die('error');
}
?>
In your situation, you can just simple check for the posted data. Like
$username = $_POST['username'];
$age = $_POST['age'];
if($username&&$age){ /* This is to check if the variables are not empty */
// redirect to page2
}
It is logical that if those are not empty, meaning they are posted. Even if they are empty, as long as you get there, that means it was posted. There is no need to check if posted or not, what needs to be checked was, if posted data was there.
I hope I made it clear. ^_^ but making sure is not bad at all. Happy coding friend.
I have two forms that are being submitted depending on the conditions.
I used
if(empty($_POST) === false)
to save data after an edit. But for the first time I encountered this situation which causes and error.
What do I need to do so that I can accommodate two forms on a page.???
put a hidden field in each of your forms...
<input type="hidden" name="do" value="edit" />
in the other form:
<input type="hidden" name="do" value="new" />
Then check $_POSTfor those specific values:
if (isset($_POST['do'])) {
// a form was posted
if ($_POST['do']=='edit') {
// do sth with form 'edit'
elseif ($_POST['do']=='new') {
// do sth with form 'new'
} // if $_P
} // if isset
I suggest you to use the RESTful approach to this problem. You can check whatever the request method is POST (new) or PUT (edit) and deal with that.
For example if you have two forms, one for edit and one for creating items, you set the method to one of them with:
<form method="PUT" ...>
<form method="POST"...>
and retrieve the method via:
$_SERVER['REQUESTED_METHOD'];
therefore dividing them.
I'm wondering if it's possible to manually change the value of an isset value. That is, to do something like this:
isset($_POST['search_user']) = true;
Why I want to do this: I have two different "submit" forms on one page. When one form is submitted, I want to capture all the values of that form into SESSION variables. However, when the other form is submitted, the SESSION variables are wiped out (since the first form is not, technically, submitted anymore).
My idea was that, if the second form is submitted, then automatically set the value of the first form to true
If I understand your question correctly, if a second form is submitted, why not just destroy the current session and start new sessions using the variables posted from the new form?
http://php.net/manual/en/function.session-destroy.php
session_destroy();
...Or, you can set another session variable if the second form is submitted:
if (isset($_POST['search_user'])) {
$_SESSION['search_user'] = "true";
}
if ($_SESSION['search_user'] == "true") {
// Second form was submitted
}
You can try to define a name and a value for each submit button, so you retrieve this in the PHP file and do what you want, according you need. For instance:
HTML to the first form:
<form name="form1" action="page2.php" method="post">
<input type="submit" value="1" name="button01">
</form>
HTML to the second form:
<form name="form2" action="page2.php" method="post">
<input type="submit" value="1" name="button02">
</form>
Then you can detect the form thas was submited doing this in page2.php:
if($_POST['button01'] == "1")
{
// Do what you need based on form1 submit
}
elseif($_POST['button02'] == "1")
{
// Do what you need based on form2 submit
}
Try this and then leave some comment telling if it helps you.