How to check if a form is submitted via javascript? - php

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.

Related

Multiple submit buttons in a single form with both submit buttons accessed in PHP

Currently I am working on a HTML page where I can upload an Excel and also get data from database between a specified date range. One submit button would bring out data from excel and database and compare value from both list and shows up the conflicting values. I need to add another submit button where the user can set how the mapping must be done for the conflicting values in the same form.
How to use two submit buttons simultaneously with data posted in first submit button accessed in second submit button in PHP?
As long as the submit buttons are placed inside the same form element, clicking on any of them will submit the form they are placed in.
With that, all fields in the same form will be submitted.
However, you probably want to know which submit button was clicked to do things differently.
You can add name property to specify which submit button was clicked.
<input type="submit" name="submit1">
<input type="submit" name="submit2">
Assuming this form is submitted with POST method, you can check which submit button was clicked using the example below.
$importData = isset($_POST["submit1"]);
$mapSettings = isset($_POST["submit2"]);
So, you can achieve this by placing all the fields in different forms into the same form element. All fields within the form element will be submitted despite of which submit button was clicked.
You can then use the boolean result above to determine what to do.
The best answer I have seen so far for this situation is:
<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Delete" />
Then in the code check to see which was triggered:
if ($_POST['action'] == 'Update') {
//action for update here
} else if ($_POST['action'] == 'Delete') {
//action for delete
} else {
//invalid action!
}
The only problem with that is you tie your logic to the text within the input. You could also give each one a unique name and just check the $_POST for the existence of that input:
<input type="submit" name="update_button" value="Update" />
<input type="submit" name="delete_button" value="Delete" />
And in the code:
if (isset($_POST['update_button'])) {
//update action
} else if (isset($_POST['delete_button'])) {
//delete action
} else {
//no button pressed
}
There is a nice post about this here: Two submit buttons in one form

How does the submit button on a form work?

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.

Submit button showing in GET URL

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>

JavaScript form submission doesn't send value of input type=submit

I'm using jQuery to, in some cases, automatically submit a form. Here's my code:
$("form [data-autosubmit]").closest("form").submit();
It works, however it doesn't send the name and value of the input type=submit button.
To elaborate, the following code:
<form method="POST">
<input type="text" name="input" value="text they entered">
<input type="submit" name="submit" value="Submit">
</form>
would send the following POSTdata when submitted by the user:
input: text they entered
submit: Submit
However when the form is submitted by JavaScript, only this POSTdata is sent:
input: text they entered
My PHP scripts rely on the presence of a "submit" value in the POSTdata.
I was thinking I could do:
$("form [data-autosubmit]").closest("form").find("input[type=submit]").click();
But it seems to defy logic, when there is a submit event on the form intended for this purpose.
HTML forms were built to support multiple submit inputs (so the same form could have an "Insert" and "Edit" button, for instance). Therefore the form relies on the click to actually register the chosen field in the _POST submission. Basically, HTML allows a form that looks like this:
<form [...]>
<input type="submit" name="Insert" value="Insert">
<input type="submit" name="Update" value="Update">
</form>
to be handled like this on the server-side:
if(!empty($_REQUEST['Update'])){
//Run update logic here..
}
elseif(!empty($_REQUEST['Insert'])){
//Run insert logic here...
}
If the Javascript submit() function registered both buttons in the POST array it would break applications using this valid markup/logic, so they default to omitting submit inputs.
So your Javascript workaround (triggering a click on the submit button rather than just submit()ing the form) is the correct approach.

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.

Categories