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();
}
Related
I have a simple form that collects data and sends it to a PHP script using POST.
<form method="post">
<input type="text" name="cost">
<button name="submit" type="submit">Submit</button>
</form>
The PHP script is,
if(isset($_POST['submit'])){
echo "set";
}
I want to know what happens when I click on the submit button?
The PHP manual says the following about isset,
isset — Determine if a variable is set and is not NULL
When exactly is the submit button SET? When I echo out echo $_POST['submit']; it outputs nothing.
It's only when I use the value attribute along with the submit button that I get something on $_POST['submit'];. Why should I use the value with the submit button? What exactly does it do?
I want to know what happens when I click on the submit button?
It submits the form.
When exactly is the submit button SET?
When the user submit's the form.
When I echo out echo $_POST['submit']; it outputs nothing.
You didn't specified a value for it, so it returns an empty string ($_POST['submit'] === "")
Why should I use the value with the submit button? What exactly does it do?
Well on an button the value is not needed, it is enough when it is set, so you can check if the button was submitted and not an other form f.ex.
Try with this
<form action="" method="post">
<input type="text" name="cost" />
<input type="submit" name="submit" value="Submit" />
</form>
In php side
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
echo "set";
}
It submits the whole form data into targeted location and the GET and POST methods are used to send encoded data to the targeted location
The GET method is restricted to send upto 1024 characters only.
The POST method does not have any restriction on data size to be sent.
I am pretty new to web programming and I cannot figure this problem out. To keep things simple, say I have 2 pages. The first page has a form with two selection boxes and a submit button. The second page has a form with two text input boxes and a submit button. After the form on the first page is submitted it goes to the second page and the two text input boxes are filled with the values from the first form with a $_POST.
My problem is, when I submit the second form (which goes to the same second page on submit) the $_POST variables of the form are empty.
I thought the if-else in the value would fix it. The purpose of that was because after submitting the $_POST from the previous page no longer has a value and I want the value in this forms field to be displayed after submitting (which is still the same value from the first form). Not only do I want it to display the value in these fields, I want to use them for a database query (which is why them being blank is a problem).
The values in the form when the second page is reached are correct. Also in the else case if I echo "test" instead of the $_POST it is displayed in the box so I believe I have it narrowed down to the $_POST being blank after submitting but I have no idea why.
<form method="post" action="newSerialNumber.php">
JON: <input type="text" name="newSNJON" value="<?php if ($_POST['JON'] != ""){ echo $_POST['JON'];}else{ echo $_POST['newSNJON'];} ?>" disabled>
Part Number: <input type="text" name="newSNPN" value="<?php if ($_POST['PN'] != ""){ echo $_POST['PN'];}else{ echo $_POST['newSNPN'];} ?>" disabled>
<input type="submit" name="Button" value="Add">
</form>
You have the disabled attribute set in those input fields.
Disabled form elements are not send when the form is submitted.
http://www.w3.org/TR/html5/forms.html#attr-fe-disabled:
“The disabled attribute is used to make the control non-interactive and to prevent its value from being submitted.”
If you don’t want the user to be able to change the values, but still send them with the form when it is submitted – use the readonly attribute instead.
Yes, it's right.
Disabled fields are not included in the submit. Remove the disabled attributes and you can see it works.
I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>
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
}
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.