Auro
Auro

Reputation: 1638

My HTML button won't open PHP link

I'm trying to invoke a PHP script from my HTML file.

Following is my HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Welcome to B.O.A.T</title>
    <link rel="stylesheet" type="text/css" href="CSS/css.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>

<body>
    <!-- Navigation Bar -->



    <div class="tab_content">
        <div id="home" class="tab_active">
                <div id="banner-container">
                    <div id="banner">
                        <p id="boat-banner"> Welcome to Bike Operation Automation Technology </p>
                    </div>
                </div>



                <div id="buttons">
                    <form method="post" action="lock.php">
                    <input type="button" name="Lock" class="button" value="LOCK" />
                    </form>

                    <form method="post" action="unlock.php">
                    <input type="button" name="Lock" class="button" value="UNLOCK" />
                    </form>

                    <form method="post" action="ledOn.php">
                    <input type="button" name="Lock" class="button" value="IGNITION ON" />
                    </form>

                    <form method="post" action="ledOff.php">
                    <input type="button" name="Lock" class="button" value="IGNITION OFF" />
                    </form>


                   <form method="post" action="GPSTracker.php">
                    <input type="submit" name="GPSTracker" class="button" value="GPS TRACKER"/>
                    </form>
                </div>
                </div>

         </div>

</body>
</html>

Following is my ledOn.php file that I want to invoke (It's one of those PHP scripts that are not being invoked)

<html>
<head>
<title>LEDON</title>
</head>
<body>
<?php
    define('HOST','mysql.hostinger.in');
    define('USER','u414932932_usr');
    define('PASS','xyz123');
    define('DB','u414932932_bdb');

    $con = mysqli_connect(HOST,USER,PASS,DB);

    //$ign = $_POST['ignition'];


    $sql = "UPDATE BOATOP SET ignition='1' WHERE user_name='boatusr'";
    if(mysqli_query($con,$sql))
    {
        echo 'SUCCESS'; ?>
        <H3> SUCESS </H3>
    <?php 
    }
    else
    {
        echo "FAILURE";
        ?>
        <H3> FAILURE </H3>
      <?php
    }
    mysqli_close($con);
?>

</body>
</html>

When I click the Ignition On button, nothing happens. I don't understand.. Neither do any of the other buttons open their respective PHP files. Where am I going wrong?

Upvotes: 0

Views: 115

Answers (4)

SML
SML

Reputation: 1265

Edit: Someone has pointed out that, we can use input type submit or button type submit.

Functionality wise, they are the same but buttons created with button element offer richer rendering possibilities.

https://www.w3.org/TR/html4/interact/forms.html#h-17.5

<div id="buttons">
        <form method="post" action="lock.php">
            <button type="submit" class="button" value="lock"> LOCK </button>
        </form>
        <form method="post" action="unlock.php">
            <button type="submit" class="button" value="unlock"> UNLOCK </button>
        </form>
        <form method="post" action="ledOn.php">
            <button type="submit" class="button" value="on"> IGNITION ON </button>
        </form>
        <form method="post" action="ledOff.php">
            <button type="submit" class="button" value="off"> IGNITION OFF</button>
        </form>
        <form method="post" action="GPSTracker.php">
            <button type="submit" class="button" value="gps'> GPSTracker </button>
        </form>
    </div>

Upvotes: 1

Deepak Dholiyan
Deepak Dholiyan

Reputation: 1912

You can do this in two ways

1) Need to change the button into input type submit like you did for last form:-

<input type="submit" name="GPSTracker" class="button" value="GPS TRACKER"/>

2) You need to use jQuery for this you need to add id in the form and click event on button like:-

 <form method="post" action="lock.php" id="form_id">
  <input type="button" name="Lock" class="button" value="LOCK" onclick=" $('#form_id').submit();" />

Upvotes: 0

phreakv6
phreakv6

Reputation: 2165

It looks to me like you will be well-served using links instead of buttons for what you are trying to do since there is absolutely no data inside the forms that you are using.

<form method="post" action="ledOn.php">
    <button type="submit" class="button" value="on"> IGNITION ON </button>
</form>

can become

<a href="ledOn.php">
    IGNITION ON
</a>

Upvotes: 0

Jeff Puckett
Jeff Puckett

Reputation: 41031

it's an input type="button" which won't submit the form. these should be type="submit"

<div id="buttons">

    <form method="post" action="lock.php">
      <input type="submit" name="Lock" class="button" value="LOCK" />
    </form>

    <form method="post" action="unlock.php">
      <input type="submit" name="Lock" class="button" value="UNLOCK" />
    </form>

    <form method="post" action="ledOn.php">
      <input type="submit" name="Lock" class="button" value="IGNITION ON" />
    </form>

    <form method="post" action="ledOff.php">
      <input type="submit" name="Lock" class="button" value="IGNITION OFF" />
    </form>


    <form method="post" action="GPSTracker.php">
      <input type="submit" name="GPSTracker" class="button" value="GPS TRACKER"/>
    </form>

</div>

Upvotes: 2

Related Questions