在php中的mysql数据库中保存数据后,如何在按钮单击中切换选项卡?

时间:2013-09-25 07:38:09

标签: javascript php jquery mysql

我是PHP的新手,我开始使用php开发应用程序,请查看我的代码

的index.php

<?php require_once('./config.php'); ?>
<?php

/*
* Following code will get single product details
* A product is identified by product id (pid)
*/

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name'])) {

$title = $_POST['name'];
$company = $_POST['company'];

require_once('./db_connect.php');

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO user(name,company) VALUES('$title','$company')");
}    

?>
<html>
<head>
<link type="text/css" rel="Stylesheet" href="style.css" />
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
    $(window).load(function () {
        var tabContents = $(".tab_content").hide(),
        tabs = $("ul.tabs li");

        tabs.first().addClass("active").show();
        tabContents.first().show();
        tabs.click(function() {
        var $this = $(this), 
        activeTab = $this.find('a').attr('href');
        if(!$this.hasClass('active')){
        $this.addClass('active').siblings().removeClass('active');
        tabContents.hide().filter(activeTab).fadeIn();
        }
        return false;
        });

        $(".tabbutton").click(function () {
            var nextTab = parseInt($("li.active").attr("id"), 10) + 1;
            if (nextTab === 4) { nextTab = 1; }
            tabs.removeClass("active");
             $("#" + nextTab).addClass("active");
             tabContents.hide();
             $("#tab" + nextTab).fadeIn("slow");                
        });
    });
</script>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript"  src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey("<?php echo $stripe['publishable_key']; ?>");

function stripeResponseHandler(status, response) {
    if (response.error) {
        // re-enable the submit button
        $('.submit-button').removeAttr("disabled");
        // show the errors on the form
        $(".payment-errors").html(response.error.message);
    } else {
        var form$ = $("#payment-form");
        // token contains id, last4, and card type
        var token = response['id'];
        // insert the token into the form so it gets submitted to the server
        form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
                // and submit
        form$.get(0).submit();
    }
}

$(document).ready(function() {
    $("#payment-form").submit(function(event) {
        // disable the submit button to prevent repeated clicks
        $('.submit-button').attr("disabled", "disabled");
        // createToken returns immediately - the supplied callback submits the form if there are  no errors
        Stripe.createToken({
            number: $('.card-number').val(),
            cvc: $('.card-cvc').val(),
            exp_month: $('.card-expiry-month').val(),
            exp_year: $('.card-expiry-year').val()
        }, stripeResponseHandler);
        return false; // submit from callback
    });
});

   身体的一部分    

<ul class="tabs clearfix">
    <li id="1"><a href="#tab1">Tab1</a></li>
    <li id="2"><a href="#tab2">Tab2</a></li>
    <li id="3"><a href="#tab3">Tab3</a></li>
</ul>

<div class="tab_container">
<div id="tab1" class="tab_content">
        <form method="post">

            <p>
                    <label>Name:</label> <input type="text" name="name">

            </p>

            <p>
                    <label>Company:</label> <input type="text" name="company">
            </p>
           <input type="submit" value="SAVE" class="tabbutton"/>
    </form>

    </div>

<div id="tab2" class="tab_content"> 
     <form id="payment-form" action="charge.php" method="post">
<h3>Purchase a quote by Oscar Wilde today! Only $535! Limited supply and going fast, buy now!!</h3>
<!-- to display errors returned by createToken -->
<span class="payment-errors"></span>
<form action="charge.php" method="POST" id="payment-form">
    <div class="form-row">
        <label>Card Number</label>
            <input type="text" size="20" autocomplete="off" class="card-number" />
    </div>
    <div class="form-row">
        <label>CVC</label>
        <input type="text" size="4" autocomplete="off" class="card-cvc" />
    </div>
    <div class="form-row">
        <label>Expiration (MM/YYYY)</label>
        <input type="text" size="2" class="card-expiry-month"/>
        <span> / </span>
        <input type="text" size="4" class="card-expiry-year"/>
    </div>
    <button type="submit" class="submit-button">Buy!</button>
</form>
</form>
</div>


<div id="tab1" class="tab_content">
<p>Tab 3 Content Goes Here...</p>
</div>
</div>
</body>
</html>

在Tab1中我将数据保存到数据库中。每当用户点击保存按钮时,数据都需要保存到数据库中,然后它将转到Tab2。保存数据时我很成功。我的问题是保存数据后不会转到Tab2。当我们点击保存按钮时,tab2只是闪烁,但页面仍显示tab1内容​​。我希望每当用户点击保存按钮时,数据需要保存在数据库中,它将转到Tab2内容。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试这种方法,将解决您的问题:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function() {
    $(".tab_content").hide();
    tabs = $("ul.tabs li");
    tabs.first().addClass("active").show();
    $(".tab_content").first().show();

    tabs.click(function() {
        var $this = $(this), activeTab = $this.find('a').attr('href');
        if (!$this.hasClass('active')) {
            $this.addClass('active').siblings().removeClass('active');
            $(".tab_content").hide().filter(activeTab).fadeIn();
        }
        return false;
    });

    $("#tabbutton").click(function() {
        $("#2").addClass('active').siblings().removeClass('active');
        $(".tab_content").hide();
        $("#tab2").fadeIn();
    });
});
</script>
</head>
<body>
    <ul class="tabs clearfix">
        <li id="1"><a href="#tab1">Tab1</a></li>
        <li id="2"><a href="#tab2">Tab2</a></li>
        <li id="3"><a href="#tab3">Tab3</a></li>
    </ul>
    <div class="tab_container">
        <div id="tab1" class="tab_content">
            <p>Tab 1 Content Goes Here...</p>
            <input type="button" id="tabbutton"/>
        </div>
        <div id="tab2" class="tab_content">
            <p>Tab 2 Content Goes Here...</p>
        </div>
        <div id="tab3" class="tab_content">
            <p>Tab 3 Content Goes Here...</p>
        </div>
    </div>
</body>
</html>

您的代码中也存在几个问题。

首先,您已在tab2 div中添加了两次表单标记。删除一个。

<div id="tab2" class="tab_content"> 
     <form id="payment-form" action="charge.php" method="post">
<h3>Purchase a quote by Oscar Wilde today! Only $535! Limited supply and going fast, buy now!!</h3>
<!-- to display errors returned by createToken -->
<span class="payment-errors"></span>
<form action="charge.php" method="POST" id="payment-form">
    <div class="form-row">
        <label>Card Number</label>
            <input type="text" size="20" autocomplete="off" class="card-number" />
    </div>
    <div class="form-row">
        <label>CVC</label>
        <input type="text" size="4" autocomplete="off" class="card-cvc" />
    </div>
    <div class="form-row">
        <label>Expiration (MM/YYYY)</label>
        <input type="text" size="2" class="card-expiry-month"/>
        <span> / </span>
        <input type="text" size="4" class="card-expiry-year"/>
    </div>
    <button type="submit" class="submit-button">Buy!</button>
</form>
</form>
</div>

其次,您的tab3 div与tab1具有相同的ID。我认为这是导致问题的原因。 将其更改为tab3并尝试。

<div id="tab1" class="tab_content">
<p>Tab 3 Content Goes Here...</p>
</div>