将Computed PHP变量传递给jQuery / Ajax表单

时间:2012-09-05 01:57:07

标签: php javascript jquery ajax

我的初始表单计算变量$ finallowamount& $ finalhighamount,但我似乎无法将这些变量的值传递给我的jQuery联系人javascript文件(下面的代码)。任何帮助将不胜感激。感谢。

    $(function() {
$('a.email').click(function(event) {
    event.preventDefault();

    $('body').append('<div id="popupshadow"></div><div id="popup"></div>');
    $('#popupshadow').css({opacity:0}).animate({opacity:0.7},1000);
    $('#popup').css({opacity:0}).delay(1200).animate({opacity:1},800);
    $('#popup').html('<form class="contact" action="#" method="post"><table class="contact"><tr><td class="input-name">Name:</td><td><input type="text" name="name" size="25" /></td></tr><tr><td class="input-email">Email:</td><td><input type="email" name="email" size="25" /></td></tr><tr><td class="input-phone">Phone:</td><td><input type="phone" name="phone" size="25" /></td></tr><tr><td></td><td><input type="submit" value="Send" /></td></tr></table><input type="hidden" name="to" value="noahldavis@gmail.com"></form>');
    $('form.contact').submit(function(event) {
        $('form.contact td').css('color','#000000');

        var name = $('form.contact input[name="name"]').val();
        var email = $('form.contact input[name="email"]').val();
        var phone = $('form.contact input[name="phone"]').val();
        var to = $('form.contact input[name="to"]').val();

        var valid = true;
        if(name == "") 

        {
            valid = false;
            $('td.input-name').css('color','#FF0000');
        }
        if(email == "" && phone == "")
            valid = false;
        if(email == "")
            $('td.input-email').css('color','#FF0000');
        if(phone == "")
            $('td.input-phone').css('color','#FF0000');

        var message = 'message';
        var paymentstreamtype = $("#ssq_rc_general input[type=radio]:checked").val();
        var finallowamount = $("#finallowamount").html();
        var finalhighamount = $("#finalhighamount").html();


        if(valid) {
            $.ajax({url : 'contact.php',
                    data : {name : name,
                            email : email,
                            phone : phone,
                            message : message,
                            paymentstreamtype : paymentstreamtype,
                            finallowamount : finallowamount,
                            finalhighamount : finalhighamount,
                            to : to},
                    type : 'post',
                    success : function(data) {
                        $('#popup').html('<p style="text-align:center;">Your message has been sent.</p>');
                        $('#popup, #popupshadow').delay(1000).animate({opacity:0},500).delay(500).queue(function() { $(this).remove(); });
                    }
            });
        }
        event.preventDefault();
        return false;
    });
    $('#popupshadow, a.cancel').click(function(event) {
        $('#popup, #popupshadow').animate({opacity:0},500).delay(500).queue(function() { $(this).remove(); });
    });
});

});

1 个答案:

答案 0 :(得分:1)

首先,当您的服务器上运行php时,javascript正在Web客户端中运行。

如果你不使用php var,你必须让source将这些var替换为实际值。

你可以使用php包含这个文件,如果这是一个js文件:

//loadjs.php
public function loadJs()
{
    if(extension_loaded('zlib')) {
        //check gzib
        ob_start('ob_gzhandler');
    }

    header ("content-type: application/javascript; charset: UTF-8");
    header ("cache-control: must-revalidate");
    $offset = 60 * 60 * 24;
    $expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
    header ($expire);
    ob_start("compress");
    $js = array(
        '0'=>array('/js/swf/swfupload.js'),
    );

    foreach($js as $key=>$value){
        foreach($value as $v){
            include $v;
        }
    }

    if(extension_loaded('zlib')){
        ob_end_flush();//flush all buffer content
    }
}
loadJs()

//then,load this js file in your html file:
<script src="loadjs.php" />