admin管理员组

文章数量:1220027

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

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());
    }
?>
Share Improve this question edited Jul 14, 2019 at 19:21 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 31, 2012 at 21:05 Parth ModyParth Mody 4661 gold badge6 silver badges18 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 9

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

you need to do that using ajax. so when a user clicks this button the page won't refresh.. there should be a page behind the seen does that for the user. whenever he clicks the button it accesses that page and upload whatever data you want...

edit: or you can just add a hidden input in the form when the button is clicked the hidden input value changes to i.e. true... then from php codes you can use the isset or other functions to validate whether the user clicked the button or not...

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

'locrizak' answered right . Your button name is 'Button' and you tried to check presence of click on 'button' both are different . In such case if you are unsure of what is wrong , you may print the entire POST array using

print_r($_POST)

This will display all submitted values from form including button

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

Use

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

isset() function does not works with input type=button. so either we have to use input type=submit instead of button or some hidden type if we still want to use button.

本文标签: javascriptHow to check if input type button is pressed in PHPStack Overflow