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

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
}

Related

$_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();
}

isset($_POST['submit']) issue

I'm having hard times finding out why the code doesn't work...
so there's a form with a submit button whitch is supposed to be disabled after the request is sent.
php:
<form action="buy.php" method="post" onsubmit="zapret()">
....
<input id="submitOtbor" type="submit" name="submit" value="Отправить" />
</form>
javascript:
function zapret()
{
var btn = document.getElementById(\'submitOtbor\').disabled = true;
$(\'#result_5\').show();
return false;
}
well, after the submit button is pressed isset($_POST['submit']) in buy.php can't catch variables from the form...
could someone help me with that?
You disabled the submit button. Disabled controls are, by definition, not "successful" and will not be sent in the data to the server.
If you want the data to show up, don't disable the button.
(You could also generate a hidden input with the same name, but really, don't disable submit buttons. It makes it hard to resubmit a form when a request fails).
So why depend on a control that's disabled (which in turn would not be carried over--a default behavior for all browsers to ignore disabled controls)?
You can always check !empty($_POST). Alternatively, you can add a hidden field that symbolizes the form's intent. e.g.
<input type="hidden" name="action" value="create|read|delete" />
That would be passed off in a submit.

PHP - put a variable in session only if link is clicked

how can I put a variable in session only if link is clicked?
Note: My link have to submit a form at the same time.
If you're using someting like this in your HTML to submit your form
<input type="submit" name="sender" value="Send" />
you can check the $_POST['sender'] Variable. It will output ether Send (clicked on button) or nothing (clicked on link).
You can make a hidden input inside the form
<input type="hidden" id="link_is_clicked" name="link_is_clicked" value="0"/>
then when clicking the link, change the value of that input
<a href="..." onclick="document.getElementById('link_is_clicked').value = 1" ...
then check for the value of the element on the server side.
Just check whether your form is submitted or not, if so set the session variable.
For example:
if(isset($_POST) && count($_POST) > 0)) //Assuming your form's action is post, use GET otherwise
{
$_SESSION['somevar'] = 'somevalue';
}
I assume you're using JavaScript to make the link submit the form? You can add a hidden input that has value 0. When the user clicks on the link, change the value of the input to 1 before submitting.

issue dynamically checking password

Here is my scenario, I present the user with a table of tests, which I have retrieved from my database in a loop and created a form for each test(row of table). So each has a submit button to execute that particular test.
basics of my loop:
while ($ts = mysql_fetch_assoc($test_info))
{
//PRESENT VALUES $ts['name'] in table within a unique form etc.
}
What I am trying to do and failing is, on clicking a particular submit button for a test, to run a JS function which checks; if the test has a password attached, if it does, present a popup form for password input, and on submitting that small form check if password is correct, if it is submit the original test form.
My problem is that I cannot parse the password value attached to that form to my javascript.
so ideally i want to do this:
<input id='submit' type='button' onclick='JSfunction(test_password)' value='execute test' >
So I can somehow parse a value from that particular form to a javascript function without actually submitting the form.
and I believe I know how to do the rest in my JSfunction.
I hope somebody can follow my poor explanation and point me in the right direction.
Many thanks,
When a form should have a password associated with it, add the following:
<input type="hidden" name="has_password" value="yes" />
<input type="hidden" name="password" value="" />
Then, in your check triggered by the submit button (assuming the button itself is the this context):
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'yes') {
// pop password request
return false;
}
You'll need a way to store the context of the current form, and I can suggest a few if you like, but you can then populate the password into the hidden field for it and submit as normal. By the way, you might want to consider onSubmit instead of the submit button's onClick, as onSubmit will catch attempts by scripts to perform a submit. If you do this, just remove the .parent() portion of the above, as the <form> element should be the this context.

How to check if a form is submitted via javascript?

I have this conventional submit button which submit a form like this:
<form method="post" id="form_submit">
...
<input class="button" type="submit" name="Submit" value="Submit">
</form>
And I check if the submit button is clicked using this:
if(isset($_POST['Submit'])){
//update DB
}
Now I have a submit link using jquery:
Submit
JS code:
$("#form_submit").submit();
What is the alternative way here to be used here for if(isset($_POST['Submit'])) since I'm submitting the form using javascript?
If I understand you correctly, try this:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// your code.........
}
You should add a hidden input <input type="hidden" name="formsubmit" value="yes" /> to the form which will always get submitted, and check for that instead of the button (which only gets submitted if it is clicked on ..)
If I understood your problem correctly that you can simply change input type to hidden.
<form method="post" id="form_submit">
...
<input type="hidden" name="Submit">
</form>
$_POST['Submit'] variable will be defined.
The best solution is "Don't do that". If you want to submit a form then use a submit button (don't do it as a side effect of clicking on a hyperlink to the top of the page). Any JavaScript you want to run can then be handled in the form's submit event.
If you really want to do it as a side effect, then check for the existence of any other field that you know will be set. You could add a hidden field to ensure there will be one of a given name/value combination if you like.

Categories