Jump to content

Help with Ajax and checkboxes


Recommended Posts

Hello guys!

 

I am having difficulty regarding ajax and checkboxes. I’m making this function wherein a user can pay their supplier in lump sum. Whenever the user selects/deselects a purchase, the sum of the selected purchases is shown in a readonly textbox. My problem is that the value in the textbox in incorrect. To be specific, it only gives the “first” row of the query I made, even if it is not selected. I am using the ajaxrequest.js given by alpine found here: http://www.phpfreaks.com/forums/index.php/topic,115581.0.html.

I have shortened my php file to be straight to the point.

 

// lump_sum.php

$sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance

      FROM purchases

      WHERE supplier_id = '$supplier_id' AND balance > 0

      ORDER BY delivery_date DESC, or_num";

$res = mysql_query($sql); $num = mysql_num_rows($res);

 

while ($row = mysql_fetch_array($res)) {

?>

<td><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="CheckBoxRequest('tprice', 'get_lump_sum.php?pid[]=', 'pid[]')"></td>

  <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td>

  <td><?PHP echo $row[2] ?></td>

  <td><?PHP echo $row[3] ?></td>

  <td><?PHP echo $row[4] ?></td>

  <?PHP

}

?>

<tr class="total">

  <td colspan="4"><b>Total Amount</b></td>

  <td><input type="text" size="20" id="tprice" name="tprice" readonly></td>

</tr>

 

 

// get_lump_sum.php

$payable = 0;

$pid_array = $_GET[pid];

foreach ($pid_array as $purchase_id) {

  $sql = "SELECT balance FROM purchases WHERE purchase_id = '$purchase_id'";

  $res = mysql_query($sql); $row = mysql_fetch_array($res);

  $payable = $payable + $row[0];

}

$payable = number_format($payable, 2);

echo $payable;   

 

My suspect is that the error is somewhere in this part:

<input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="CheckBoxRequest('tprice', 'get_lump_sum.php?pid[]=', 'pid[]')">

 

Or in this part of the get_lump_sum.php:

$pid_array = $_GET[pid];

foreach ($pid_array as $purchase_id) {

  $sql = "SELECT balance FROM purchases WHERE purchase_id = '$purchase_id'";

  $res = mysql_query($sql); $row = mysql_fetch_array($res);

  $payable = $payable + $row[0];

}

 

 

I have been working on this for more than a week now and I cannot get it right. Any help will be highly appreciated.

Thanks in advanced.

 

Link to comment
Share on other sites

You want to list purchases to the user with a checkbox to make a lump sum payment. When a checkbox is ticked it updates the total price to pay in the read only textbox.

 

If I understand this correctly then you don't need AJAX to produce this at all.

 

Don't overcomplicate things by implementing AJAX for this. All you need to do is set a javascript price variable for each product, then have a js function that will just total up the purchases that are ticked.

Link to comment
Share on other sites

u have 2  write a function 2 pass the checkbox value 2 php via java script

 

here is a function for this

 

function CheckStaste() {

 

if(document.myform.srcctc.checked != true)

{

document.myform.srcctc.value=0;

}

 

 

 

                }

 

 

call this function on click even of the checkbox ....i face the same trouble like when i doing some application....hope this will help ....if not pm with ur code 

Link to comment
Share on other sites

To send a PHP variable to a javascript variable you can use something like:

 

 

<html>

<head>

<script type="text/javascript">

var myData = Array(); //create the blank array variable

//now add values to the array from PHP
<?php

echo " myData[0] = '" . $variable1 . "';";
echo " myData[1] = '" . $variable2 . "';";
echo " myData[2] = '" . $variable3 . "';";

?>

</script>

</head>


<body>
..document body...
</body>

</html>

 

Do you see how it is done, all it does is echoes the PHP variables into the javascript array. You can then access the data from javascript to do whatever you want, such as add them up to create a total.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.