如何使用JS使用更新值自动更新第二个文本框

时间:2012-01-05 19:43:34

标签: php javascript javascript-events joomla1.5

我有一个PHP脚本和两个文本框。它的货币转换。

如果用户想要使用第一个文本框购买1个5美元的信用额度并点击标签,则下一个文本框将通过乘以php变量自动更新。

可以使用简单的内联javascript吗?

我的PHP变量是“$ currentValue”

第一个测试盒是名称& ID = “信用” 第二个文本框是名称& ID = “finalRupee”

请在function.php文件中查看以下代码

<form action="index.php" method="post" name="userFormJob" onSubmit="return   validateForm()" enctype="multipart/form-data">
<table width= "100%">
<tr>
<td colspan="2"><strong><?php echo JText::_( 'PRICE FOR 1 CREDIT' ); ?> = <?php echo  $currencytype . ' ' . $currency.' '; ?> &nbsp;<?php echo $price_credit; ?> (Please Note:   For One credit value you can post 1 Job for 1 Day)</strong></td>
</tr>
<tr>
<td><p> <b>
<?php 
$content = file_get_contents("http://www.xe.com/ucc/convert.cgi?From=USD&To=INR&    Amount=1.&template=pca-xetrade"); 
$content = explode('<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>',$content);
$content = str_replace('<TD VALIGN=MIDDLE ALIGN=RIGHT><FONT     FACE="Arial,Helvetica"><B>', "", "$content[1]");
$content = explode('<TD COLSPAN=3 ALIGN=CENTER><FONT FACE="Arial,Helvetica"     SIZE=-2>',$content);
$toremove = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ">", "<", "\"", "\'", "=", ",", "/", "-1");

$content = str_replace($toremove, "", "$content[0]");
$final = explode('+1',$content);

$from_code  = "USD";
$to_code    = "INR";
$currency   = $config-> get('currency');

echo '<b>' .$final[0]. ' ' .$from_code. ' =' .$final[1]. ' ' .$to_code. '</b>';
?>
</b></p></td>
</tr>
<tr>
<td><?php echo JText::_( 'BUY' ); ?></td>
<td><input type="text" name="credit" id="credit" class="inputbox"     onKeyPress="return     isNumberKey(event)" />
<?php 
$currentValue = $final[1];
?>
<?php echo JText::_('CREDIT'); ?>
<input name="finalRupee" type="text" class="inputbox" id="finalRupee" onKeyPress="return isNumberKey(event)" value="<?php ?>" readonly /></td>
</tr>
<tr>
<td colspan="2" >Please Use Currency Convert for Dollar to Rupees <a     href="http://www.xe.com/ucc/convert.cgi?Amount=1&From=USD&To=INR&template=8n"     target="_blank">Click Here</a></td>
</tr>
<tr>
<td colspan="2" ><b><?php echo JText::_('BILLING ADDRESS'); ?></b></td>
</tr>
<tr>
<td><?php echo JText::_('ADDRESS'); ?></td>
<td><input class="inputbox" type="text" name="address" id ="address" size="40"     maxlength="255" value="<?php echo $row->bill_addr; ?>"/></td>
</tr>
<tr>
<td><?php echo JText::_('ADDRESS CONT'); ?>:</td>
<td><input class="inputbox" type="text" size="40" maxlength="255" name="address_cont" id ="address_cont" value="<?php echo $row->bill_addr_cont; ?>"/></td>
</tr>
<tr>
<td><?php echo JText::_('CITY'); ?>: </td>
<td><input class="inputbox" type="text" name="city" id ="city" value="<?php echo     $row->bill_city; ?>"/></td>
</tr>
<tr>
<td><?php echo JText::_('STATE'); ?>:</td>
<td><input class="inputbox" type="text" name="state" id ="state" value="<?php echo     $row->bill_state; ?>"/></td>
</tr>
<tr>
<td><?php echo JText::_('COUNTRY'); ?>:</td>
<td><?php 
$list_country = getSelectCountry('id_country',$row->bill_id_country,'');
echo $list_country;
?></td>
</tr>
<tr>
<td><?php echo JText::_('ZIP POSTAL'); ?>:</td>
<td><input class="inputbox" type="text" name="zip" id ="zip" value="<?php echo         $row->bill_zip; ?>"/></td>
</tr>
<tr>
<td><?php echo JText::_('PHONE'); ?>:</td>
<td><input class="inputbox" type="text" name="phone" id ="phone" value="<?php echo     $row->bill_phone; ?>"/></td>
</tr>
<tr>
<td><?php echo JText::_('PAYMENT'); ?>:</td>
<td><select name="mode_pay">
<option value="m"><?php echo JText::_('MANUAL'); ?> / <?php echo JText::_('TRANSFER');     ?></option>
<option value="p">PayPall Secure Pay</option>
</select></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2"><center>
<img src="http://www.jobodisha.com/images/stories/powered_by.png" alt="Pay Pal Secure Payments" width="497" height="149" align="absmiddle">
</center></td>
</tr>
</table>
<input type="submit" value="<?php echo JText::_( 'BUY' ); ?>" class="button" />
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="task" value="savebuycredit" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHTML::_('form.token'); ?>
</form>

3 个答案:

答案 0 :(得分:2)

您不需要php来进行简单的乘法计算。这个简单的内联jquery代码是:

var individualCost= 5;
var amount = $("#credit").val();
$("#finalRupee").val(amount * individualCost);

答案 1 :(得分:1)

我会使用

从PHP获取JavaScript变量
<script>var conversionRate = <?php echo $currentValue; ?>;</script>

然后在第一个文本框失去焦点时在JavaScript中处理转换:

<script>
    document.getElementById("credit").onblur = function() {
        document.getElementById("finalRupee").value = this.value * conversionRate;
    };
</script>

代码无需任何附加库即可运行。如果您已经在使用jQuery或其他库,那么您可能更喜欢更短的代码,但这样做没有任何依赖性。

请告诉我是否有任何我可以详细说明的内容,或者是否有我没有回答的内容。

<强>更新
刚刚意识到我已将<?php echo $currentValue; ?>放在引号中,但不应该这样。代码已修复。

答案 2 :(得分:0)

我尝试了以下方法及其对我的工作。

<?php 

    $content = file_get_contents("http://www.xe.com/ucc/convert.cgi?From=USD&To=INR&Amount=1.&template=pca-xetrade"); 

    $content = explode('<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>',$content);

    $content = str_replace('<TD VALIGN=MIDDLE ALIGN=RIGHT><FONT FACE="Arial,Helvetica"><B>', "", "$content[1]");

    $content = explode('<TD COLSPAN=3 ALIGN=CENTER><FONT FACE="Arial,Helvetica" SIZE=-2>',$content);

    $toremove = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ">", "<", "\"", "\'", "=", ",", "/", "-1");

    $content = str_replace($toremove, "", "$content[0]");

    $final = explode('+1',$content);

    $from_code  = "USD";

    $to_code    = "INR";

    $currency   = $config-> get('currency');

    echo '<h3> Today Cost of ' .$final[0]. ' ' .$from_code. ' =' .$final[1]. ' ' .$to_code. '</h3>';
?>