How to check if the enter button is pressed in PHP?

The isset () function can be used to check if the input button is of type submit , but is there a way to check whether the button of type input is pressed?

In my code, the button does nothing except call the function for the .Onclick () event, which then refreshes the page and records in the database inside PHP ... and I want it to record only after the button is clicked ... and I I can’t use the send type for other reasons ... the following code:

 function send() { var events = document.getElementById("event").value; location.href = "calm.php?day=" + xx + "&month=" + yy + "&year=" + zz + "&events=" + events; } <input name="Button" type="button" id="submit" onclick="send(this.form)" value="Button" /> <?php session_start(); include_once "connect_to_mysql.php"; $day = $_GET['day']; $month = $_GET['month']; $year = $_GET['year']; $events = $_GET['events']; $userid = $_SESSION['id']; if (isset($_POST['button'])) { $sql = mysql_query("INSERT INTO events (userid,month,day,year,events) VALUES('$userid','$month','$day', '$year','$events')") or die (mysql_error()); } ?> 
+4
source share
5 answers

isset($_POST['Button']) you just missed capital B You must match it in the same way as the input attribute name .

+9
source

you need to do this with ajax. therefore, when the user clicks on this button, the page will not be updated. there should be a page after what he does for the user. whenever he presses a button, he accesses this page and downloads any data you want ...

change or you can simply add hidden input to the form when the button is pressed to change the value of the hidden input, i.e. true ... then from php codes you can use isset or other functions to check if the user clicked the button or not ...

+1
source

In the example that you have, the simplest approach would be to add an additional variable to the parameters passed in & button_press = true or something like that, and then you would know that the button was pressed when you receive the information

0
source

'locrizak' answered correctly. The name of your button is 'Button', and you tried to check for a click on the 'Button', they are both different. In this case, if you do not know what is wrong, you can print the entire POST array using

 print_r($_POST) 

This will display all the submitted values ​​from the form, including the button

Using isset () is the correct method to check for a form element or not

Using

 if(isset($_POST['Button'])){ //code block for insertion,validation etc // } 
0
source
Function

isset () does not work with the input button type =. therefore either we must use the input type = submit instead of the button or some kind of hidden type if we still want to use the button.

0
source

Source: https://habr.com/ru/post/1394063/


All Articles