user5359494
user5359494

Reputation:

How to link bootstrap button to php code?

I have like a contact us form, and basically what I want is for the user to click the submit button and then for this data to be stored on a remote database on a server. Problem is I don't know how to link the javascript and php stuff to the button.

This is my code:

<html>
<head>
    <title>Contact Us</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head> 
<body>
<script type="text/javascript">
    function verify(){
        var forename = document.getElementById("forename").value;
        var surname = document.getElementById("surname").value;
        var comments = document.getElementById("comments").value;

        if(forename == null || forename == ""){
            alert("Forename is empty");
            return false;
        }
        else if(surname == null || surname == ""){
            alert("Surname is empty");
            return false;
        }
        else if(comments == null || comments == ""){
            alert("Comments is empty");
            return false;
        }
        document.register.submit();
    }
    </script>
    <div class="container">
        <h1>Contact Us</h1>
    </div>
    <form>
        <div class="container center_div">
            <div class="panel panel-default">
                <div class="row">
                <fieldset class="form-group">
                    <label for="Forename">Forename</label>
                    <input type="text" class="form-control" id="forename" placeholder="Forename">
                </fieldset>
                <fieldset class="form-group">
                    <label for="Surname">Surname</label>
                    <input type="text" class="form-control" id="surname" placeholder="Surname">
                </fieldset>
                <fieldset class="form-group">
                    <label for="Contact Us">Contact Us</label>
                    <input type="text" class="form-control" id="comments" placeholder="Comments">
                </fieldset>
                <div class="row">
                    <div class"col-md-2">
                        <button type="button" action="contactus.php" class="btn btn-success">Submit</button>
                    </div>
                </div>  
            </div>
        </div>
    </form> 
</body>
</html>

Upvotes: 0

Views: 11821

Answers (3)

devlin carnate
devlin carnate

Reputation: 8591

You have action on the wrong tag. Remove it from here:

<button type="button" action="contactus.php" class="btn btn-success">Submit</button>

Put it here:

<form action="contactus.php">

Also, change your button from type "button" to "submit"

<button type="submit" class="btn btn-success">Submit</button>

And this is just semantics, but perhaps an important distinction: the button is not a bootstrap button. It's an HTML button. You're styling it using bootstrap css.


Update: OP has asked how to get the "JavaScript stuff" working. Here's one way:

I like id's, so add an id to your form tag. I also like to be explicit about the form submit method, so add that to your form tag, too:

<form id="myForm" action="contactus.php" method="post">

Magical JavaScript Stuff

var form = document.forms.myForm

form.onsubmit = function() {
          var forename = document.getElementById("forename").value;
          var surname = document.getElementById("surname").value;
          var comments = document.getElementById("comments").value;

          if (forename == null || forename == "") {
            alert("Forename is empty");
            return false;
          } else if (surname == null || surname == "") {
            alert("Surname is empty");
            return false;
          } else if (comments == null || comments == "") {
            alert("Comments is empty");
            return false;
          }
          document.register.submit();
}

Here is a Fiddle Demo. Note: I removed the action from the form tag in the demo because it doesn't know what "contactus.php" is.


One other note: If you're only intention with the JavaScript function is to verify that the form fields have been filled out, you may be better off using the HTML input attribute required.

For example:

<input type="text" class="form-control" id="forename" placeholder="Forename" required>

Here's a Fiddle Demo showing how it works.

Upvotes: 2

cssyphus
cssyphus

Reputation: 40038

As a beginner, I first suggest you use jQuery rather than pure javascript. For one thing, it's much less typing and (imho) far easier to master. You code in jQuery, and behind the scenes jQuery turns that into javascript at runtime.

Since you are using bootstrap, you already have the jQuery library loaded (or you are supposed to have it -- your code was missing it). Press Ctrl+U to see page code in linked example. Anyway, since it's included on the page, you might as well use it.

Your sample code, fixed:

jsFiddle Demo

<html>
<head>
    <title>Contact Us</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"   integrity="sha256-wS9gmOZBqsqWxgIVgA8Y9WcQOa7PgSIX+rPA0VL2rbQ="   crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(function(){
            $('#btnSubmit').click(function(){
                var forename = $("#forename").val();
                var surname = $("#surname").val();
                var comments = $("#comments").val();

                if(forename == null || forename == ""){
                    alert("Forename is empty");
                    return false;
                }
                else if(surname == null || surname == ""){
                    alert("Surname is empty");
                    return false;
                }
                else if(comments == null || comments == ""){
                    alert("Comments is empty");
                    return false;
                }
                $('#frmContact').submit();
            });
        }); //END document.ready
    </script>
</head> 
<body>
    <div class="container">
        <h1>Contact Us</h1>
    </div>
    <div class="container center_div">
        <form id="frmContact" action="contactus.php" method="post">
            <div class="row">
                <fieldset class="form-group">
                    <label for="Forename">Forename</label>
                    <input type="text" class="form-control" id="forename" placeholder="Forename">
                </fieldset>
            </div><!-- .row -->
            <div class="row">
                <fieldset class="form-group">
                    <label for="Surname">Surname</label>
                    <input type="text" class="form-control" id="surname" placeholder="Surname">
                </fieldset>
            </div><!-- .row -->
            <div class="row">
                <fieldset class="form-group">
                    <label for="Contact Us">Contact Us</label>
                    <input type="text" class="form-control" id="comments" placeholder="Comments">
                </fieldset>
            </div><!-- .row -->
            <div class="row">
                <div class="col-xs-8">&nbsp;</div>
                <div class="col-xs-4">
                    <button id="btnSubmit" type="button" class="btn btn-success">Submit</button>
                </div>
            </div><!-- .row -->
        </form>
    </div><!-- .container -->
</body>
</html>

Since you are new to jQuery, here are some free beginner-level tutorials to get you started. (It's where I learned myself, and they're free)

Upvotes: 0

rahimli
rahimli

Reputation: 1462

use method in your form POST or GET and add action with the name of php file which handles the submit. Sample code

<form action="example.php" method="post" > <input type="text" name="user_name" placeholder="Name"> <input type="text" name="user_surname" placeholder="Surname"> <input type="email" name="user_email" placeholder="Email"> <input type="password" name="user_pass" placeholder="Password"> <input type="submit" name="submit"> </form>

more about post and get function here

and html submit here

Upvotes: 0

Related Questions