How to check if input type button is pressed in PHP?

Parth Mody picture Parth Mody · Jan 31, 2012 · Viewed 43.6k times · Source

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

In my code the button does nothing but call a function on the .Onclick() event which then refreshes the page and makes a database entry inside PHP...and I want it to make the entry only after the button is pressed...and I can't use the submit type for other reasons...following is some 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());
    }
?>

Answer

locrizak picture locrizak · Jan 31, 2012

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