我想将一个jquery变量传递给PHP页面,有时我得到的是变量,有时候不是

时间:2013-09-02 11:50:56

标签: php jquery

我有一个名为logo_design_3.php的PHP页面。 我使用Jquery将变量“grand_total”发布到名为logo_design_3_sql.php的另一个页面。但不幸的是,有时我会在下一页上获得该变量,有时候不会。 知道我哪里做错了吗?

以下是logo_design_3.php页面的摘要:

<script type="text/javascript">
function load1() {
    document.getElementById('hide2').style.display = 'none';
    return false;
}

function toggle() {
    document.getElementById('hide1').style.display = 'none';
    document.getElementById('hide2').style.display = '';
    document.getElementById('hidrad').checked = 'true';
    document.getElementById('silver').checked = false;
    document.getElementById('bronze').checked = false;
    document.getElementById('gold').checked = false;
}

function load2() {
    document.getElementById('hide1').style.display = '';
    document.getElementById('hide2').style.display = 'none';
}
</script>
<script type="text/javascript">
$(document).ready(function() {

    var grand_total = 0;

    $("input").live("change keyup", function() {

        $("#Totalcost").val(function() {

            var total = 0;
            $("input:checked").each(function() {

                total += parseInt($(this).val(), 10);
            });
               var textVal = parseInt($("#min").val(), 10) || 0;

               grand_total = total + textVal;

               return grand_total;
        });

     });
         $("#next").live('click', function() {

        $.ajax({
            url: 'logo_design_3_sql.php',
            type: 'POST',
            data: {
                grand_total: grand_total
            },
            success: function(data) {
                // do something;

            }
        });

    });

});
 </script>
 <script type="text/javascript">
function validate() {
    var num = document.getElementById("min").value;
    var num = parseInt(num, 10);

    if (num < 299) {
        document.getElementById("min").value = "299";
        alert("Minimum Amount Should be 299");
    }
}

</head>
<body onLoad="load1()">
<div id="wrap">
<div class="main-container internal">
<div class="main wrapper clearfix section">
<article>
<section>
                     <h1>Step 3. Contest details</h1>

                     <h2>Create a contest that attracts the right designers to your brief.</h2>

                    <p>We've got the largest design community online so it's important to create a contest that attracts the kinds of designers you want to work on your brief.</p>
                    <div id="form">
                        <form method="post" id="logoform3" action="logo_design_3_sql.php">
                            <label>What design package do you want?</label>
                            <br>All packages come with a 100% money-back guarantee and you own full copyright to the final design.
                            <br>
                            <p></p>
                            <p>
                                <input type="radio" class="radio" name="b" onClick="load2()" value="299" id="bronze">
                                <label>Bronze - $299</label>
                                <br>Get lots of great designs at a great price
                                <br>
                                <li style="list-style:circle">Expect around 30 designs</li>
                            </p>
                            <p>
                                <input type="radio" class="radio" name="b" onClick="load2()" value="499" id="silver">
                                <label>Silver - $499</label>
                                <label class="block badge">Most popular</label>
                                <br>Attract great designers and get priority support
                                <br>
                                <li style="list-style:circle">Your project will be shown to designers before bronze projects</li>
                                <li style="list-style:circle">Expect around 60 designs</li>
                            </p>
                            <p>
                                <input type="radio" class="radio" name="b" onClick="load2()" value="799" id="gold" checked="true">
                                <label>Gold - $799</label>
                                <label class="block badge">Work only with our top designers</label>
                                <br>Work only with our most talented designers and receive 1-on-1 support
                                <br>
                                <li style="list-style:circle">Only our best designers can participate to ensure top quality</li>
                                <li style="list-style:circle">A dedicated account manager will guide you to the best result possible</li>
                                <li style="list-style:circle">Expect around 60 designs of the highest quality</li>
                            </p> <a onclick="toggle(); event.preventDefault();" href="#" style="text-decoration:none;border-bottom:1px dotted blue;" id="hide1">I want to create a custom package</a>

                            <br>
                            <div id="hide2">
                                <p>
                                    <input type="radio" class="radio" name="bbb" id="hidrad" checked="true" id="name_ur_price" value="0">Name your price:
                                    <input type="number" width="10px" style="background:#ccc; border:1px solid #ccc !important;" id="min" min="299" name="min" onChange="validate();" id="name_your_price" name="name_your_price">.00($299 minimum)
                                    <br>
                                </p>
                            </div>
                            <label>Contest preferences</label>
                            <br>
                            <input type="checkbox" name="" value="39.00" align="middle" id="private_contest" name="private_contest">
                            <label>Make your contest private (+$39.00)</label>
                            <br>Your contest will be hidden from search engines and the general public. Only members of the Square Designs community can see your contest and they must agree to a Non-Disclosure Agreement (NDA) to see the details. We provide a standard template for the NDA or <a href="" onclick="document.getElementById('upload').click(); return false"> you can upload your own(as a PDF).</a>

                            <br>
                            <input type="file" id="upload" name="upload" style="visibility: hidden; width: 1px; height: 1px" multiple />
                            <label id="design_dura" style="left:!important">How quickly do you need your design?</label>
                            <br>
                            <br>
                            <input type="radio" name="c" value="0">
                            <label>Standard 7 days (FREE)</label>
                            <br>
                            <input type="radio" name="c" value="39" id="days_3">
                            <label>In 3 days(+$39.00)</label>
                            <br>
                            <input type="radio" name="c" value="59" id="days_2">
                            <label>In 2 days(+$59.00)</label>
                            <br>
                            <label>
                                <input type="radio" name="c" value="79" id="days_1">In 24 hours(+$79.00)</label>
                             <h2>TOTAL :</h2>

                            <p>
                                <input type="text" name="Totalcost" id="Totalcost" style="background:#f5f5f5; border:none; outline:none; box-shadow:none; font-size:18px;" disabled/>
                                <br>
                            </p>
                            <input type="submit" class="sumit-styled" value="Next" style="margin-left:-15px;" id="next" name="next">
                            <input type="submit" class="sumit-styled" value="Save Now" style="margin-left:-15px;" id="save" name="save">
                        </form>
                    </div>
                </section>
                <aside>
                    <img src="img/slide2_2.png" webstripperwas="img/slide2_2.png" alt="mockup" />
                    <p><em>Donec sed odio dui. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.</em>

                    </p>
                </aside>
            </article>
            <!-- END MAIN -->
        </div>
    </div>
</div>

这是来自logo_design_3_sql.php页面:

<?php include ( "config.php"); 
$total=$_POST[ 'grand_total'];
 $sql="INSERT INTO form2 (total)VALUES('$total')" ; 
if($total>0)
{ $res = mysql_query($sql); } 
if($res) 
{ echo "<script> window.location.replace('logo_design_4.php')</script>"; }
 else { echo "<script> window.location.replace('logo_design_3.php')</script>"; }
 ?>

我有关于 - &gt;的问题window.location.replace('logo_design_4.php')也是。即使$ total已正确插入表中,插入后导航的页面也是“logo_design_3.php”。为什么这样?

2 个答案:

答案 0 :(得分:0)

请尝试以下实时功能

$("#next").live('click',function () {
    $.ajax({
            url: 'logo_design_3_sql.php',
            type: 'POST',
            data: {grand_total: grand_total},
            success: function(data) {}
        });
    });
});

答案 1 :(得分:-1)

您必须在全局范围内声明grand_total变量。

var grand_total = 0;
$(document).ready(function () {



$("input").live("change keyup", function () {

    $("#Totalcost").val(function () {

        var total = 0;
        $("input:checked").each(function () {

            total += parseInt($(this).val(), 10);
        });
        var textVal = parseInt($("#min").val(), 10) || 0;

        grand_total = total + textVal;

        return grand_total;
    });

});
$("#next").click(function () {

    $.ajax({
                url: 'logo_design_3_sql.php',
                type: 'POST',
                data: {grand_total: grand_total},
                success: function(data) {}
            });
        });
    });

请不要在生产系统上使用您的PHP代码。您必须检查POST变量(http://www.php.net/manual/en/security.database.sql-injection.php