admin管理员组文章数量:1417070
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 piled and minified CSS -->
<link rel="stylesheet" href=".3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href=".3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest piled and minified JavaScript -->
<script src=".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 ments = document.getElementById("ments").value;
if(forename == null || forename == ""){
alert("Forename is empty");
return false;
}
else if(surname == null || surname == ""){
alert("Surname is empty");
return false;
}
else if(ments == null || ments == ""){
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="ments" 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>
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 piled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest piled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn./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 ments = document.getElementById("ments").value;
if(forename == null || forename == ""){
alert("Forename is empty");
return false;
}
else if(surname == null || surname == ""){
alert("Surname is empty");
return false;
}
else if(ments == null || ments == ""){
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="ments" 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>
Share
Improve this question
edited Aug 9, 2017 at 14:00
j08691
208k32 gold badges269 silver badges280 bronze badges
asked Mar 18, 2016 at 22:19
user5359494user5359494
1
-
this:
action="contactus.php"
should be in your form tag, not your button tag. – devlin carnate Commented Mar 18, 2016 at 22:25
3 Answers
Reset to default 2You 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 ments = document.getElementById("ments").value;
if (forename == null || forename == "") {
alert("Forename is empty");
return false;
} else if (surname == null || surname == "") {
alert("Surname is empty");
return false;
} else if (ments == null || ments == "") {
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.
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
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./bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="https://code.jquery./jquery-1.9.1.min.js" integrity="sha256-wS9gmOZBqsqWxgIVgA8Y9WcQOa7PgSIX+rPA0VL2rbQ=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn./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 ments = $("#ments").val();
if(forename == null || forename == ""){
alert("Forename is empty");
return false;
}
else if(surname == null || surname == ""){
alert("Surname is empty");
return false;
}
else if(ments == null || ments == ""){
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="ments" placeholder="Comments">
</fieldset>
</div><!-- .row -->
<div class="row">
<div class="col-xs-8"> </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)
本文标签: javascriptHow to link bootstrap button to php codeStack Overflow
版权声明:本文标题:javascript - How to link bootstrap button to php code? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252661a2649916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论