How does this check whether the form was submitted? - php

HTML:
<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<!-- ........ -->
</form>
PHP:
if (array_key_exists('check_submit', $_POST)) {....}
Why can array_key_exists('check_submit', $_POST) check whether the form was submitted?
I've seen isset($_POST['...']) used before, but not this.
if i don't do this array_key_exists('check_submit', $_POST) decision., what may happen.

To check wether your page was called as a result of a form submit (via POST) you should use something like this:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
You shouldn't use hidden form elements just to have a value to check wether a form was submitted. If you use the request method you catch it more cleanly and don't have any trouble if the form ids/names/values are altered.

check_submit is a field in your form, so when you submit the form, that field is available in the POST data.
PHP places incoming POST-method form data into the $_POST superglobal array, and your code determines whether the check_submit field can be found in that array.
Indeed, it's quite similar to isset($_POST['check_submit']), in that it checks whether such an element exists in $_POST. It's just taking a slightly different approach.
If you did not submit the form, then of course there is no form data.

Do var_dump($_POST), you will see the $_POST associative array, which will have check_submit key, and value = 1

Don't you mean something like:
if(!empty($_POST) && isset($_POST['check_submit']) && $_POST['check_submit'] == '1'){
// do something
}

Related

Simple Q about isset($_POST['submit'])

if(isset($_POST['submit'])){
//code here
}
Correct me if I am wrong, but I believe the code above will not work if the user submits the form using the "enter" key.
Is this true? If so, is there another if statement I can use instead of this to cover both the user using the submit button and pressing the enter key?
To check if a form is posted to the server use:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
If you are not sure if submit will be in POST you can use overkill method: create hidden <input> in this <form> with some unique name and verify if it in POST data:
<input type="hidden" name="some_name" value="OK" />
And in PHP code:
if (isset($_POST['some_name']) && $_POST['some_name'] == "OK") {
// ...
}
This is not true, if submit is the default submit button.
Also, there's a $_SERVER['REQUEST_METHOD'] — I take it it would be POST when you submit your form or GET otherwise, if I understand your intention right.
I'll expand on the other answers: it's probably not true if submit is input type=submit and the enter key triggers that button. If it's type=img or a <button>, the behavior may be different with the enter key (and may not set that _POST value).
The submit should be fine if it is the only submit button on the page or the first submit button on the page if there is more than one, which is probably unlikely.
An alternative would be to use a hidden input.
e.g.
<input type="hidden" id="processed" name="processed" value="1" />
then in your submit script
if(isset($_POST['processed'])){
//code here
}

$_POST not finding a "name" value

I am trying to submit a form, it SHOULD send forth a name, but I know I'm messing something silly up and I just can't see it (3 hours of sleep last night + new coding project at work != a smart idea)
Here's my form on one page:
<form action="add.php" method="POST">
<button type="submit" name="exportcult">Export All</button>
</form>
And here's the code on the other page, meant to process the POST:
if (!isset($_POST["name"]) || $_POST["name"] == '') {
header('Location: '.$criteria."?error=data");
die();
}
I am getting the error message this sends back, so I know it isn't registering a name - why could that be?
I think you're confused how the form data actually gets submitted. "name" is the attribute, not the key value that is found in the POST data. You need to specify the name for that element, which will be the key value present in the POST data. You have specified name="exportcult" so in the POST data, the variable will be at $_POST['exportcult']. However, this value will always be an empty string since you have not indicated a value attribute for your button.
Keep in mind that when dealing with submit buttons, only the value of the button which was used to submit the form will be included along with the rest of the form data. Try using this:
<button type="submit" name="exportcult" value="export">Export All</button>
If that specific button was used to submit the form, then $_POST['exportcult'] should be equal to 'export'.
For those of you who are unsure: buttons do get submitted with the form, but they still have to have a value attribute.
Your form doesn't contain any field except the button, so $_POST will only contain a field exportcult.
Edit: Since you use <button> instead of <input> it might not go into the POSTed data.
Do:
if (!isset($_POST["exportcult"]) || $_POST["exportcult"] == '') {
header('Location: '.$criteria."?error=data");
die();
}
You're currently checking for a field named "name", when the field is named "exportcult". Additionally, it should be <input, not <button.
you should add
<input type="button" name="exportcult" value="Whatever you want" />
and check for exportcult on the isset() instead of name
use exactly this:
<form action="add.php" method="POST">
<input type="submit" name="name" value="Export All"></form>
if (!$_POST["name"])) {
header('Location: '.$criteria."?error=data");
exit();
}

how top use GET and POST simultaneusly

I have a page in php that gets from the url such as this code example
<form action="welcome.php" method="get">
//some interesting code here
</form>
The issue now is that when i do the GET depending on its value I need to do a POST,
How can I use action="get" and action="post" in the same page?, I am kind of new to PHP so I am not sure if I can use two tags ("i dont think so but please correct me if i am wrong").
PS: I am getting to the same page "welcome.php" and posting to itself again, and depending on the value I am going to show different content.
Thank you
Your question is a little unclear, but if I'm understanding it correctly, you can certainly pass GET variables via the form's action:
<form action="welcome.php?these=are&get=variables" method="post">
//some interesting code here
</form>
You may access both through $_REQUEST if necessary. So no matter what your method type is, $_REQUEST will contain all of your submitted values. This way you don't need to determine whether it was by post, or by get that the data was submitted.
You will have to trap the $_GET value as a hidden var, so when the post is submitted again, your $_GET value will be available:
if (array_key_exists('myVar', $_GET)) {
echo '<input type="hidden" name="myVar" value="' . $_GET['myVar'] .'" />
}
// Now when you resubmit the form you should have access to 'myVar'
echo $_REQUEST['myVar'];
You could also add the get value to the session super global:
if (array_key_exists('myVar', $_GET)) {
$_SESSION['myVar'] = $_GET['myVar'];
}
// Now when you resubmit the form you should have access to 'myVar'
echo $_SESSION['myVar'];

Can't understand this PHP code

What is this code doing? If Isset, and what are the variables doing, whats the $_POST doing? Can someone explain please?
if (isset($_POST['Submit'])) {
$title=$_POST['title'];
$forename = $_POST['forename'];
$surname=$_POST ['surname'];
$dateofbirth=$_POST ['dateofbirth'];
$gender=$_POST ['gender'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$passcode1=$_POST ['passcode1'];
$passcode2=$_POST ['passcode2'];
$_POST contains the data from the form the user submitted.
if(isset($_POST['Submit']))
is checking to see if a form was submitted. There is likely a hidden element with this name, or the submit button was created to include this value.
The remainder of the lines copy*1 the information out of the POST super global into easier to use variables.
(*1 not actually copied, PHP is copy on write)
If a form is being submitted, then it assigns variables.
It will check If the form is submitted set the value of html elements in to the PHP variables that's it.
These variable can be used for further processing and later to store in database.
So here is an attempt to answer your amorphous question.
if (isset($_POST['Submit']))
This line is checking to see if an entry called 'Submit' is present in the $_POST array.
isset is a function that checks the existence of variables in PHP.
$_POST is a global variable in PHP that captures the key-value pairs sent in an HTTP POST request. $_POST will contain the result of an HTML form submission in 99% of the use cases.
$title=$_POST['title'];
is setting the title variable in the PHP script to the title field in the post array. title was probably a text field submitted by a form.
Looking at the use of $_POST in your example code, the request to the php script probably comes from a form that looks like:
<form method="post" name="someForm" action="thatScript.php">
<input type="text" name="title"/>
<input type="text" name="forename"/>
<input type="text" name="surname"/>
... other inputs that correspond to $_POST entries...
<input type="submit" value="Submit" name="Submit"/> <!-- the submit field that is checked in the isset -->
</form>
Notice: <form method="post"
This tells the browse to submit the key-value pairs for the form in a post request. If that line read <form metho="get" then the browse would submit the key-value pairs as a get request and the php script would check and use $_GET instead of $_POST.
It checks if the form has been submitted and if its true then values from post array (that are sent by the sending page) are assigned to variables.
for example value for this $_POST['title'] is assigned to variable $title.

php custom post variables

is there a way to create custom post variables when a user presses submit, like this:
$_POST['var'] = 'hi';
In order to set post values on the page with the form you should use hidden input tags.
i.e.
<input type="hidden" name="var" value="hi" />
It will be invisible and your receiving script will see that key/value passed along.
Variables POSTed by the browser to your PHP script will only correspond to the fields of the form that was used in the browser -- which means you have to put your custom data in that form.
If you don't want them displayed, you can use a hidden input field :
<input type="hidden" name="var" value="hi" />
But note that the data will still be sent by the browser -- which means you have to escape/filter/protect it, like any other value that comes from the user ; and it cannot be trusted : anyone can pretty easily modify the value of that form field, even if it's not visible.
while $_POST variable is an array, you can also define var like this
$_POST['var'] = 'hi';
it is same like hidden field. :)

Categories