0

Well I've managed to put the following together from various examples, however I don't believe the javascript function is even being called, below is all my code:

Javascript:

<script type="text/javascript" src="coding/jquery.js"></script>
<script type="text/javascript">

    function showHint(quantity, price){
     document.getElementById("txtcost").innerHTML="being called values" + quantity + price;
        $.getJSON('http://website.com/ecommerce/coding/validation/calprice.php', {q: quantity, p: price}, function(data){
            $('#txtcost').html('Total: ' + data.total);
            $('#txtvat').html('VAT: ' + data.vat);
        });

    }

</script>

Display

<span id="txtcost" style="color:#060"></span>
<span id="txtvat" style="color:#060"></span>

PHP

<?php
echo "being called";
function data() {
$q = $_GET["q"];
$p =&$_GET["p"];

$total = $p * $q;

if($q >= 10)
{
    $total = ($total / 10) * 9;
    //echo '<strong>£ ' . $total . '</strong><span style="font-size:10px; font-weight:bold;"> Discount applied.</span>';
    echo json_encode(array('total' => $total, 'vat' => '10.00'));
}
else{ echo '<strong>£ ' . $total . '<strong>'; }}
?>

Hope that helps

Note: the code isn't perfect, I want to get the initial functionality working.

EDIT:

SO far I've managed to establish:

The function is being called The correct values are being passed into the function

1
  • looks like you are mixing your medicine - PHP and javascript. Commented Mar 31, 2011 at 0:00

2 Answers 2

2

For one thing, you need to use JavaScript's dot notation to reference object properties, and not PHP's arrow operator:

data.total

instead of

data->total
Sign up to request clarification or add additional context in comments.

1 Comment

Can you post more of your code? At the risk of asking a dumb question, you have included jQuery on the page, right? And you have put that function within script tags? More code makes it easier diagnose the root cause of the problem.
0

i think a problem with your quotes on line 1 you seem to be breaking into php, maybe you need to use something more like:

<input name="quantity" type="text" id="quantity" onkeyup="showHint(this.value, ' . $events[0]['price'] . ' )" size="6" />

but hard to say without full code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.