将PHP变量放在javascript文件中 [英] placing PHP Variables inside javascript file

查看:64
本文介绍了将PHP变量放在javascript文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照标题。

我有一段jQuery,如下所示

I have a piece of jQuery that looks like the following

$("<div class='creditCardDetails' id='usercurrentccbox'>
    <div class='creditCard'>
    <div class='cardChoice'>
    <span>Choose Card Type</span>
    <input name='cctype' type='radio' value='V' class='lft-field' id='visa' />
    <label for='visa'></label>
    <input name='cctype' type='radio' value='M' class='lft-field' id='mastercard' />
    <label for='mastercard'></label><input name='cctype' type='radio' value='A' class='lft-field' id='amex' />
    <label for='amex'></label>
    </div>
    <!--cardChoice-->
    <div class='cardNumber'>
    <input name='ccn' id='ccn' type='text' class='long-field' value='<?php echo MaskCreditCard($loggedInfo[ccn]);?>' maxlength='19' readonly />
    </div>
    <div class='cardCVV'>
    <input name='cvv' id='cvv' type='text' maxlength='5' class='small-field' />
    </div>
    <div class='creditCardName'>
    <input name='ccname' id='ccname' type='text' class='long-field' value='<?php echo $loggedInfo[ccname];?>' readonly/>
    </div
    <div class='cardDate'>
    <input name='exp1' id='exp1' type='text' maxlength='2' class='small-field' value='<?php echo $loggedInfo[ccm];?>' readonly /><input name='exp2' id='exp2' type='text' maxlength='4' class='small-field' value='<?php echo $loggedInfo[ccy];?>' readonly />
    </div>
    </div><!--creditCard-->").insertAfter("#paymentCardChoice");

但是你会看到它有PHP变量,如果我将它嵌入到我的PHP文件中,这可以工作,但我想保持PHP文件尽可能短,并将此代码放在一个。 js文件,当然我的变量只显示PHP的文本而不是它自己的变量。

But as you'll see it has PHP Variables, if I have this embedded into my PHP file this works but I want to keep the PHP file as short as possible and placed this code inside a .js file and of course my variables only display the text of the PHP not the variable its self.

我试图调用的变量已在config.php中定义文件。

The variables I am trying to call are defined already in a config.php file.

我是否需要使用这样的东西?如果是这样的话,我会坚持如何在上面的代码中调用变量。

Would I need to use something like this? if so I'm stuck with how I would call the variables in the code above.

$.post('phpfile.php', qString, function (data) {

}, "json"); 


推荐答案

.js

var qString = 'selectedID=' +selectedID;
$.post('/assets/inc/get-logged-info-card.php', qString, function (results) {
$("<div class='creditCardDetails' id='usercurrentccbox'>
    <div class='creditCard'>
    <div class='cardChoice'>
    <span>Choose Card Type</span>
    <input name='cctype' type='radio' value='V' class='lft-field' id='visa' />
    <label for='visa'></label>
    <input name='cctype' type='radio' value='M' class='lft-field' id='mastercard' />
    <label for='mastercard'></label><input name='cctype' type='radio' value='A' class='lft-field' id='amex' />
    <label for='amex'></label>
    </div>
    <!--cardChoice-->
    <div class='cardNumber'>
    <input name='ccn' id='ccn' type='text' class='long-field' value='"+results[0].maskccn+"' maxlength='19' readonly />
    </div>
    <div class='cardCVV'>
    <input name='cvv' id='cvv' type='text' maxlength='5' class='small-field' />
    </div>
    <div class='creditCardName'>
    <input name='ccname' id='ccname' type='text' class='long-field' value='"+results[0].ccname+"' readonly/>
    </div
    <div class='cardDate'>
    <input name='exp1' id='exp1' type='text' maxlength='2' class='small-field' value='"+results[0].ccm+"' readonly /><input name='exp2' id='exp2' type='text' maxlength='4' class='small-field' value='"+results[0].ccy+"' readonly />
    </div>
    </div><!--creditCard-->").insertAfter("#paymentCardChoice");
}, "json");

.php

<?php
$selectedID     =   $_POST['selectedID'];

$q = "SELECT * FROM tbl_user WHERE user_id = '$selectedID'";
$sql = mysql_query($q);

$results = array();
while($row = mysql_fetch_array($sql))
{
   $results[] = array(
      'cct'     => $row['user_cct'],
      'ccn'     => $row['user_ccn'],
      'maskccn'     => MaskCreditCard($row['user_ccn']),
      'ccname'      => $row['user_ccname'],
      'ccm'     => $row['user_date_m'],
      'ccy'     => $row['user_date_y']
   );
}
echo json_encode($results);
?>

这篇关于将PHP变量放在javascript文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆