PHP从DB加载表单值

时间:2016-01-19 15:46:55

标签: php jquery forms

我有一个HTML / PHP表单,客户可以填写并提交。

这一切都很好,但我想要实现的是,当表单加载时,如果客户登录,那么它会将表单字段值转换为以前预订中存储在数据库中的值,这样他们就可以了必要时修改但不必再填写整个表格。

表格页面(booking.php):

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="0">
    <meta name="description" content="">
    <meta name="author" content="Freshify">

    <title>MooWoos</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/moowoos.css" rel="stylesheet">

    <!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.js"></script>

     <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <? php

    include "ASEngine/AS.php"
    if($login->isLoggedIn()) 


</head>

<body>

    <!-- Navigation -->
        <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header mw-nav">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand mw-font mw-nav-active" href="index.html">MooWoos</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse mw-nav-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li class="mw-font mw-nav-text">
                        <a class="mw-nav-active mw-nav-text" href="about.html">About</a>
                    </li>
                    <li>
                        <a href="#" class="dropdown-toggle mw-font mw-nav-text" data-toggle="dropdown">Events<b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li class="mw-font mw-nav-text">
                                <a class="mw-nav-text" href="#">Preloved Markets</a>
                            </li>
                            <li role="separator" class="divider"></li>
                            <li class="mw-font mw-nav-text">
                                <a href="#">Courtneys Fun Day</a>
                            </li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle mw-font mw-nav-text" data-toggle="dropdown">Stallholder Info<b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li class="mw-font mw-nav-text">
                                 <a href="/stallholders/stallholder_faq.html">FAQ</a>
                             </li>
                             <li class="mw-font mw-nav-text">
                                 <a href="/stallholders/selling_tips.html">Selling Tips</a>
                             </li>
                             <li class="mw-font mw-nav-text">
                                 <a href="/stallholders/terms_conditions.html">T &amp; C&#39;s</a>
                             </li>
                             <li class="mw-font mw-nav-text">
                                 <a href="/stallholders/health_safety.html">Health &amp; Safety</a>
                             </li>
                        </ul>
                    </li>
                   <li class="mw-font">
                        <a class="mw-nav-text" href="contact.php">Contact</a>
                    </li>
                    <li class="mw-font">
                         <a class="mw-nav-text" href="/booking.html">Book Now!</a>
                     </li>                        
                        </ul>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>
    <!-- Page Content -->
    <div class="container">

        <hr>

        <div class="row">
            <div class="col-md-6 col-md-offset-3 bookingform">
                <h1>Stall Booking Form</h1>
                <p class="lead">
                    Fill out the form to book and pay for your stall!
                </p>
                <form method="post" id="bookingForm" action="submission.php">
                    <div class="form-group">
                        <label for="name">Name: </label>
                        <input type="text" name="name" class="form-control" placeholder="Your Name" value="" title="Please enter your name"/>
                    </div>
                    <div class="form-group">
                        <label for="address">Address: </label>
                        <textarea name="address" class="form-control" placeholder="Your Address" value="" title="Please enter your address"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="phone">Telephone Number: </label>
                        <input type="text" name="phone" class="form-control" placeholder="Your Telephone Number" value="" title="Please enter the best telephone number to contact you on"/>
                    </div>
                    <div class="form-group">
                        <label for="email">Email: </label>
                        <input type="text" name="email" class="form-control" placeholder="Your Email" value="" title="Please enter your Email address"/>
                    </div>
                    <div class="form-group">
                        <label for="c-rail">Will you be bringing a clothes rail?: </label>
                        <input type="radio" name="c-rail" value="Yes" /> Yes
                        <input type="radio" name="c-rail" value="No" /> No
                        <input type="radio" name="c-rail" value="N/A"/> N/A
                    </div>
                    <div class="form-group">
                        <label for="date">Which date would you like to book?: </label>
                        <input type="radio" name="date" value="13th September" /> Sunday 13th September
                    </div>
                    <div class="form-group">
                       <label>What type of stall do you require?</label>
                       <div>
                        <input type="radio" name="stallType" id="stallType-Preloved" value="Preloved">
                        <label for="stallType-Preloved">Preloved</label>
                        </div>
                        <div>
                        <input type="radio" name="stallType" id="stallType-Craft" value="Craft">
                        <label for="stallType-Craft">Craft</label>
                        </div>
                        <div>
                        <input type="radio" name="stallType" id="stallType-Business" value="Business">
                        <label for="stallType-Business">Business</label>
                        <div class="reveal-if-active">
                           <label for "bizname">What is your Business Name?</label>
                           <input type="text" id="bizname" name="bizname" class="require-if-active" placeholder="Business Name" title="Please provide us with your Business name" value="" />
                            <label for="insurance">Do you have Public Liability Insurance?</label>
                            <input type="radio" id="insurance" name="insurance" class="require-if-active" data-require-pair="#stallType-Business" title="We will require proof of this prior to market day" value="Yes"/> Yes
                            <input type="radio" id="insurance" name="insurance" class="require-if-active" data-require-pair="#stallType-Business" title="Our insurance does not cover other businesses. Please ensure you have adequate cover and provide us with proof prior to market day" value="No"/> No
                            <label for="goodyBag">Would you like to feature in the Goody Bags?</label>
                            <input type="radio" id="goodybag" name="goodBag" class="require-if-active" data-require-pair="#stallType-Business" title="You will need to provide us with 100 leaflets/flyers or samples. There is an additional charge for this which will be invoiced separately" value="Yes"/> Yes
                            <input type="radio" id="goodybag" name="goodyBag" class="require-if-active" data-require-pair="#stallType-Business" value="No" /> No
                        </div>
                        </div>
                    </div>
                     <div class="form-group">
                       <label>Where did you hear about us??</label>
                       <div>
                        <input type="radio" name="whereHeard" id="whereHeard-Facebook" value="Facebook">
                        <label for="whereHeard-Facebook">Facebook</label>
                        </div>
                        <div>
                        <input type="radio" name="whereHeard" id="whereHeard-Word" value="Word of Mouth">
                        <label for="whereHeard-Word">Word of mouth</label>
                        </div>
                        <div>
                        <input type="radio" name="whereHeard" id="whereHeard-Flyer" value="Flyer/Leaflet">
                        <label for="whereHeard-Flyer">Flyer/Poster</label>
                        </div>
                        <div>
                        <input type="radio" name="whereHeard" id="whereHeard-Other" value="Other">
                        <label for="whereHeard-Other">Other</label>
                        <div class="reveal-if-active">
                            <input type="text" id="whereHeard-Other" name="whereHeard-Other" class="form-control require-if-active" data-require-pair="#whereHeard-Other" placeholder="Other" value="" title="Please tell us where you head about us"/>
                        </div>
                        </div>
                    </div>
                    <input type="submit" name="submit" class="btn btn-success btn-lg" value="Book & Pay" />
                </form>

            </div>

        </div>
        <!-- /.row -->

        <hr>


        <!-- /.row -->

        <hr>

        <!-- Footer -->
        <footer>
            <div class="row">
                <div class="col-lg-12">
                    <p>Copyright &copy; MooWoos 2015. Website by <a href="http://www.Freshify.co.uk">Freshify</a></p>
                </div>
            </div>
            <!-- /.row -->
        </footer>

    </div>
    <!-- /.container -->

    <!-- Tooltip JS -->
    <script src="js/tooltip.js"></script>


</body>

</html>

我有代码检查用户是否已登录if($login->isLoggedIn());

但是我需要它然后从数据库中提取正确的用户详细信息并相应地更改值,或者如果用户没有登录,它将正常提交,因为并非每次预订都会重复业务

Submission.php:

<?php
    session_start();
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['address'] = $_POST['address'];
    $_SESSION['phone'] = $_POST['phone'];
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['c-rail'] = $_POST['c-rail'];
    $_SESSION['date'] = $_POST['date'];
    $_SESSION['stallType'] = $_POST['stallType'];
    $_SESSION['insurance'] = $_POST['insurance'];
    $_SESSION['goodyBag'] = $_POST['goodyBag'];
    $_SESSION['whereHeard'] = $_POST['whereHeard'];
    $_SESSION['whereHeard-Other'] = $_POST['whereHeard-Other'];

    include("bin/connection.php");
    require("bin/sendemail.php");
    require("lib/class.phpmailer.php");
    // Check connection

    if($link === false){

        die("ERROR: Could not connect. " . mysqli_connect_error());

    }



    // Escape user inputs for security

    $name = mysqli_real_escape_string($link, $_POST['name']);
    $address = mysqli_real_escape_string($link, $_POST['address']);
    $telephone = mysqli_real_escape_string($link, $_POST['phone']);
    $email = mysqli_real_escape_string($link, $_POST['email']);
    $clothesRail = mysqli_real_escape_string($link, $_POST['c-rail']);
    $bookingDate = mysqli_real_escape_string($link, $_POST['date']);
    $stallType = mysqli_real_escape_string($link, $_POST['stallType']);
    $insurance = mysqli_real_escape_string($link, $_POST['insurance']);
    $goodyBag = mysqli_real_escape_string($link, $_POST['goodyBag']);
    $wdyhau = mysqli_real_escape_string($link, $_POST['whereHeard']);
    $wdyhauOther = mysqli_real_escape_string($link, $_POST['whereHeard-Other']);


    var_dump($_POST);

    print "<pre>". $sql ."</pre>";


    // attempt insert query execution

    $sql = "INSERT INTO bookings (name, address, telephone, email, clothesRail, bookingDate, stallType, insurance, goodyBags, wdyhau, wdyhauOther) VALUES ('$name', '$address', '$telephone',  '$email', '$clothesRail', '$bookingDate', '$stallType', '$insurance', '$goodyBag', '$wdyhau', '$wdyhauOther')";

    if(mysqli_query($link, $sql)){

        sendCustomer();
        sendMooWoos();

        if(isset($_POST['stallType']) && ($_POST['stallType']) == "Preloved"){

            header("refresh:5;url=https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FGXPCRVAXJC8J"); 

 }

        elseif(isset($_POST['stallType']) && ($_POST['stallType']) == "Craft"){

            header("refresh:5;url=https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6SLZSSYLDRM2A"); 

 }
        elseif(isset($_POST['stallType']) && ($_POST['stallType']) == "Business"){

            header("refresh:5;url=https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WH8HB3M9S3GU2"); 
}

        else{

            echo "ERROR: Could not redirect";
}
        } else{

            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}



    // close connection

    mysqli_close($link);

    ?>

0 个答案:

没有答案