开发者

Having problems passing values and outputting them using jQuery

开发者 https://www.devze.com 2023-03-12 08:52 出处:网络
Here\'s the page: bit.ly/m5cyjx What I\'m trying to do is to pass the value of the select element (i.e. EUR, USD, etc.), as well as the value of the left text input (i.e. 100) to the form on the bott

Here's the page: bit.ly/m5cyjx

What I'm trying to do is to pass the value of the select element (i.e. EUR, USD, etc.), as well as the value of the left text input (i.e. 100) to the form on the bottom of the page.

When I submit the form, I can see that it is only passing the default values of the text input and the select element. Namely, it passes the values that each element contains upon page load rather than the ones I input.

The form is submitted using jQuery $.post.

Furthermore, I'm trying to dynamically show the resultant value in the right text area. I believe the PHP is working properly, but here it is anyway:

<?php
//convert to selected currency from BTC
$currencySelectValue = $_POST['currencySelectValue'];
$currencyValue = $_POST['currencyValue'];
$lastPrice = $_POST['lastPrice'];

if($currencySelectValue == "USD")
{
    $convertedResult = ($currencyValue / $lastPrice);
}
else
{
    //convert currency to USD
    $convertJSON = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=" . $currencyValue . $currencySelectValue . "%3D%3FUSD", true);
    $convertArr = json_decode($tickerJSON, true);
    $currencyValue = $convertArr["rhs"];
    $currencyValue = preg_replace('/[^0-9]/', '', $currencyValue);
    $convertedResult = ($currencyValue / $lastPrice);
}

echo $convertedResult;
?>

jQuery code:

    $(document).ready(function(){
        $('#fromCurrencyValue').inlineFieldLabel({
            label: 'Currency Value'
        });
        $('#btcValue').inlineFieldLabel({
            label: 'BTC Value'
        });
        $('#currencySelect').selectmenu({
            style: 'popup',
            width: 100
        });
        $('#convertToButton').button();
        $('#convertFromButton').button();
        $('#convertToButton').click(function() {
            $.post("php/convertToForm.php", $("#convertToForm").serialize(), function(data){ 
                $('#toCurrencyValue').val(data);
            });
        });
        $('#convertFromButton').click(function() {
            $.post("php/convertFromForm.php", $(开发者_开发知识库"#convertFromForm").serialize(), function(data){ 
                $('#fromCurrencyValue').val(data);
            });
        });


        $("#currencySelectValue").val($("#currencySelect").val());
        $("#currencyValue").val($("#fromCurrencyValue").val());
        $("#btcValueForm").val($("#btcValue").val());
    });

Any help?


I think I know what your problem is. Looking at the data that's being passed to the PHP script, the value of "currencyValue" is passed as "Currency Value". In your script, you have $currencyValue = $_POST['currencyValue']; and then $currencyValue / $lastPrice.

The problem is that $currencyValue is text and not a number. The division of "Currency Value" / 19.8 is invalid.

EDIT: The value of "currencyValue" is set in your jQuery code when it does this: $("#currencyValue").val($("#fromCurrencyValue").val());. That calls happens as soon as the page loads. This is probably not what you want. Instead, you should move that to your onclick event handlers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号