Wern Ancheta
Wern Ancheta

Reputation: 23297

How to connect php and jquery

Is it possible to use php to compute something and use jquery to fetch the computed value and display it back to the webpage without the need to submit the form. I'm really having trouble using client-side to compute something that is using arrays. So I'm thinking of letting php do the computation.

                    <input type="hidden" name=ids[] value="<?php $id; ?>">
                    <input type="hidden" name=qoh[] value="<?php $qtyhand; ?>">

                    <input type="hidden" name=dprice[] value="<?php $dsprice; ?>">
                    <input type="hidden" name=sprice[] value="<?php $ssprice; ?>">

                    <td>$qtyhand</td>";
                    <td><input type="text" name=qbuys[]  value="<?php echo $ssprice; ?>"></td>
                    <td><?php $ssprice; ?></td>";
                    <td><input type="text" name=disc[]  value=""></td>


                    <td><input type="text" name=subtot[]  value=""></td>

And here is the php file that is supposed to be used for computing.

<?php
    $ctbill=0;
    foreach ($_GET['ids'] as $k => $v) {
        $id = (int)$v;
        $qtyhnd = (int)$_GET['qoh'][$k];
        $qtytbuy = (int)$_GET['qbuys'][$k];
        $left = $qtyhnd - $qtytbuy;



        $sellprice=(double)$_GET['sprice'][$k];
        $dealerprice=(double)$_GET['dprice'][$k];

        $finalvalue=.01;
        $dis=(double)$_GET['disc'][$k];




        $stotal=(double)$qtytbuy * $sellprice;
        $cdizval=(double)$stotal * $dis * $finalvalue;
        $cdstotal=$stotal-$cdizval;

        $ctbill=(double)$ctbill + $cdstotal; 






        $dizval=$dis * $finalvalue;
        $preprof=(($sellprice * $qtytbuy)-($dealerprice*$qtytbuy)) * $dizval;
        $profit=(($sellprice * $qtytbuy)-($dealerprice*$qtytbuy)) - $preprof;

    ?>

Can you give me an idea on how to do it.

Upvotes: 0

Views: 2820

Answers (3)

mario
mario

Reputation: 145482

The loading itself is often as simple as

$("#output").load("compute.php?ids[]=1111&ids[]=2222");

The complexity in your case is assembling the various URL parameters from the form fields. Usually you can pass a simple Javascript array/hash as data parameter. But JS arrays and the default jQuery .load handler don't work well with repetetive PHP-style var[] field names that you have.

Your best bet is to build a loop and do that manually I think. (But there might be plugins for that, so try Google and jQuery plugin repository.)

Upvotes: 1

jon_darkstar
jon_darkstar

Reputation: 16768

Use ajax and set those values in the callback.

jQuery's ajax functions include $.get, $.post, $.load, $.ajax . You should probably pass your parameters with POST bc it seems you want to pass some arrays and GET is not well suited for that. That PHP file should echo out the stuff you need, and depending on its complexity you may want it to be JSON used JSONencode instead of just a plain string.

$.post('/url.php', {data1: 'literal', data2: variable, ....}, function($x){--display==});

url.php is the PHP file that handles it
second argument is encoming data
third argument is a callback function, with $x being whatever url.php echos

http://api.jquery.com/category/ajax/
http://www.visualjquery.com

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

Yes. Look at the jquery manual for examples of how to make ajax requests and show their results:

http://api.jquery.com/jQuery.ajax/

Upvotes: 1

Related Questions