将Bootstrap Form Helpers Country picker保存到我的数据库中

时间:2015-09-26 14:30:07

标签: php html twitter-bootstrap

我使用Bootstrap-Form-Helpers通过下拉框显示国家/地区和旗帜。我使用了以下Html,

<div class="bfh-selectbox bfh-countries" data-country="US" data-flags="true">
    <input type="hidden" value="">
    <a class="bfh-selectbox-toggle" role="button" data-toggle="bfh-selectbox" href="#">
        <span class="bfh-selectbox-option input-medium" data-option=""></span>
        <b class="caret"></b>
    </a>
    <div class="bfh-selectbox-options">
        <input type="text" class="bfh-selectbox-filter" name="country">
        <div role="listbox">
            <ul role="option">
            </ul>
        </div>
    </div>
</div>

要保存到数据库的PHP代码是

if (isset($_POST['btn_signup'])) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $pass = $_POST['password'];
    $cpass = $_POST['conf_pass'];
    $country = $_POST['country'];
    $dropdown = $_POST['dropdown'];
    $query3 = "SELECT * from tbl_users WHERE email = '" . $email . "'";
    $result3 = mysql_query($query3, $con) or die(mysql_error());
    $query = "SELECT * from tbl_users WHERE username = '" . $username . "'";
    $result = mysql_query($query, $con) or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-danger alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Username is not available! Please Try another username!!.' . mysql_error();
        echo '</div>';
        echo '</div>';
    }
    elseif ($pass != $cpass) {
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-danger alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Password typed doesnt match please retype the password correctly!!.' . mysql_error();
        echo '</div>';
        echo '</div>';
    }
    elseif (mysql_num_rows($result3) > 0) {
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-danger alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Email Already exist please try another email!!.' . mysql_error();
        echo '</div>';
        echo '</div>';
    }
    else {
        $query2 = "INSERT INTO tbl_users (first_name,last_name,username,password,confPass,email,country,user_type)VALUES('{$fname}','{$lname}','{$username}','{$pass}','{$cpass}','{$email}','{$country}','{$dropdown}')";
        $result2 = mysql_query($query2);
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-success alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Success! Well done its submitted.';
        echo '</div>';
        echo '</div>';
    }
}

我的所有其他数据都输入到我的数据库中。但不是国家。 请帮忙!!

3 个答案:

答案 0 :(得分:4)

您可以在div标签下方使用 data-name =“country”。然后你可以进入$ _POST中的选定国家。

<div class="bfh-selectbox bfh-countries" data-country="US" data-name="country" data-flags="true"> ..............remaining code here.............. </div>

$country = $_POST['country'];

如果有这个帮助,请告诉我。

由于

答案 1 :(得分:2)

表单帮助程序

> 覆盖的输入字段

尝试以下代码

<?php
session_start();
require_once ("includes/db.php");

print_r($_POST);
if (isset($_POST['btn_signup'])) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $pass = $_POST['password'];
    $cpass = $_POST['conf_pass'];
    $country = $_POST['country'];
    $dropdown = $_POST['dropdown'];
    $query3 = "SELECT * from tbl_users WHERE email = '" . $email . "'";
    $result3 = mysql_query($query3, $con) or die(mysql_error());
    $query = "SELECT * from tbl_users WHERE username = '" . $username . "'";
    $result = mysql_query($query, $con) or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-danger alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Username is not available! Please Try another username!!.' . mysql_error();
        echo '</div>';
        echo '</div>';
    }
    elseif ($pass != $cpass) {
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-danger alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Password typed doesnt match please retype the password correctly!!.' . mysql_error();
        echo '</div>';
        echo '</div>';
    }
    elseif (mysql_num_rows($result3) > 0) {
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-danger alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Email Already exist please try another email!!.' . mysql_error();
        echo '</div>';
        echo '</div>';
    }
    else {
        $query2 = "INSERT INTO tbl_users (first_name,last_name,username,password,confPass,email,country,user_type)VALUES('{$fname}','{$lname}','{$username}','{$pass}','{$cpass}','{$email}','{$country}','{$dropdown}')";
        $result2 = mysql_query($query2);
        echo '<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">';
        echo '<div class="alert alert-success alert-dismissable">';
        echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
        echo 'Success! Well done its submitted.';
        echo '</div>';
        echo '</div>';
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Mycricprofle</title>
<link rel="shortcut icon" href="http://faviconist.com/icons/be7b568b1a766b3888e5326879e1a9b8/favicon.ico" />
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<link rel="stylesheet" href="css/bootstrap-theme.min.css"/>
<link rel="stylesheet" href="css/bootstrap-formhelpers-countries.flags.css"/>
<link rel="stylesheet" href="css/bootstrap-formhelpers.min.css"/>
<script src="js/vendor/jquery-1.11.2.min.js"></script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/bootstrap-formhelpers.min.js"></script>
<script src="js/bootstrap-formhelpers-countries.js"></script>
<style>
.style {
    color: #761c19;
    text-decoration: none;
}
.style a {
    text-decoration: none;
    font-weight: bold;
}
</style>
</head>

<body>
<div class="container">
    <div class="col-md-6 col-sm-6 col-xs-12">
        <h2>Signup to Create a New Account</h2>
        <br/>
        <form action="signup.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="text">First Name:</label>
                <input type="text" class="form-control" placeholder="Firstname" name="fname" required/>
            </div>
            <br/>
            <div class="form-group">
                <label for="text">Last Name:</label>
                <input type="text" class="form-control" placeholder="Last name" name="lname" required/>
            </div>
            <br/>
            <div class="bfh-selectbox bfh-countries" data-country="US" data-flags="true">
                <input type="hidden" id="country" name="country" value="">
                <a class="bfh-selectbox-toggle" role="button" data-toggle="bfh-selectbox" href="#"> <span class="bfh-selectbox-option input-medium" data-option=""></span> <b class="caret"></b> </a>
                <div class="bfh-selectbox-options">
                    <input type="text" class="bfh-selectbox-filter">
                    <div role="listbox">
                        <ul role="option">
                        </ul>
                    </div>
                </div>
            </div>
            <br/>
            <div class="form-group">
                <label for="text">Email:</label>
                <input type="email" class="form-control" placeholder="Email" name="email" required/>
            </div>
            <br/>
            <div class="form-group">
                <label for="text">Username:</label>
                <input type="text" class="form-control" placeholder="Username" name="username" required/>
            </div>
            <br/>
            <div class="form-group">
                <label for="text">Password:</label>
                <input type="password" class="form-control" placeholder="Password" name="password" required/>
            </div>
            <br/>
            <div class="form-group">
                <label for="text">Confirm Password:</label>
                <input type="password" class="form-control" placeholder="Confirm Password" name="conf_pass" required/>
            </div>
            <br/>
            <select class="form-control selectpicker" name="dropdown" id="type_user" required/>

            <option value="player">Player</option>
            <option value="recruiter">Recruiter</option>
            </select>
            <br/>
            <br/>
            <button class="mdl-button mdl-js-button mdl-button--raised mdl-button--accent" id="btn_signup" name="btn_signup">Submit</button>
        </form>
        <br/>
        <p class="style">Back to <a href='index.php'>Home</a></p>
        <p class="style">Signin to Get in <a href='signin.php'>Sigin in</a></p>
    </div>
</div>
<script>
$(document).ready(function(e) { 
$(document).find(".bfh-countries input[type=hidden]").attr("name","country");
}); 
</script>
<?php
// include('includes/footer.php');
 ?>
</body>
</html>

关于DOM提供此

关于DOM manupilation提供此

答案 2 :(得分:0)

假设您至少使用了formhelpers库的2.3版,您应该可以替换它:

<div class="bfh-selectbox bfh-countries" data-country="US" data-flags="true">
            <input type="hidden" id="country" name="country" value="">
            <a class="bfh-selectbox-toggle" role="button" data-toggle="bfh-selectbox" href="#"> <span class="bfh-selectbox-option input-medium" data-option=""></span> <b class="caret"></b> </a>
            <div class="bfh-selectbox-options">
                <input type="text" class="bfh-selectbox-filter">
                <div role="listbox">
                    <ul role="option">
                    </ul>
                </div>
            </div>
        </div>

有了这个:

<div class="country bfh-selectbox bfh-countries" data-flags="true" data-filter="true" data-name="country"></div>

data-name属性指定用于存储两个字符国家/地区代码的隐藏字段的名称。您还可以指定data-country的值,该值将提供默认值或在回发后保持该值,但这取决于您如何处理它。