TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-04-2010, 07:57 AM   #1 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default Ajax + Javascript + DIV problem

Not so much or a problem, just I don't know...

Ok, so ajax select to create a table with a few fields in it is working fine, obviously puts the table in a DIV...

Now the problem I have is I want to do a calculation on a field in the table, the issue I have is I don't know where to do the javascript calc (if that makes sense)...

Code:
<html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<script type="text/javascript">

function calcMargin(margin)
{

do I do this here ??????

}


function showProduct(str)
{

// use correct code for Browser Type
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

// Check for state change and fill the "div" with the sql data and table html bollocks

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getProd.php?q="+str,true);
xmlhttp.send();
}
</script>

</head>


<?




$con = mysql_connect('localhost', 'user', 'pass');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

/*
Grab the ID's and the product names from the db, only going to use the id_prd value for the form value
but going to show the ID and product name for ease of use.
*/


$sql="SELECT id_prd, name_prd FROM products_prdV2 order by id_prd asc";
$result = mysql_query($sql);
?>


<body>
Please select a Product from the List
<form>
<select name="id_prd" onchange="showProduct(this.value)">

<?
while($row = mysql_fetch_array($result))
        {
        echo "<option value=" . $row['id_prd'] .">" . $row['id_prd'] . " " . $row['name_prd'] . "</option>\n";
        }
?>


</select>
</form>

<br />
<div id="txtHint"></div>
</body>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</HEAD>
</HTML>
This is the form processor (yes it is rough, just throwing it together to get it working then will tidy and sort it properly, just want funtionality first)

Code:
<?php

$q=$_GET["q"];

?>

<script type="text/javascript">

function calcMargin(margin)
{

Or do I do this here ????


}
</script>

<?

$con = mysql_connect('localhost', 'user', 'pass');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);
$sql="SELECT id_prd, sku_prd, price_prd, prd_discounts.discount_sum  FROM products_prdV2, prd_discounts WHERE id_prd = $q  and discount_prd_id = $q";

$result = mysql_query($sql);

echo "<table border='1'>";
echo "<tr>";
echo "<th>Product ID</th>";
echo "<th>Product name</th>";
echo "<th>Price</th>";
echo "<th>Discount Percentage</th>";
echo "<th>Price after discount</th>";
echo "<th>Cost Price</th>";
echo "<th>Margin</th>";
echo "</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id_prd'] . "</td>";
  echo "<td>" . $row['sku_prd'] . "</td>";
  echo "<td>&pound;" . $row['price_prd'] . "</td>";
  echo "<td>" . $row['discount_sum'] . "%</td>";
  echo "<td>&pound;" . round(($row['price_prd'] - (($row['price_prd'] / 100) * $row['discount_sum'])), 2) . "</td>";
  echo "<td><input type='text' name='cost_price' onChange='calcMargin(this.value)'></td>";
  echo "<td><input type='text' name='margin' value='margin' readonly='readonly' /></td>";
  echo "</tr>";

  }
echo "</table><br><br>";


mysql_close($con);
?>

Thanks :D
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easily Format JSON using PHP and Interpret using Javascript Wildhoney Advanced PHP Programming 13 02-01-2013 11:05 AM
Building Javascript PHP problem captainmerton Absolute Beginners 2 10-31-2009 10:02 AM
PHP and AJAX 9three Javascript, AJAX, E4X 0 09-27-2009 09:26 PM
Ajax Theme for CMS russellharrower Javascript, AJAX, E4X 5 07-22-2009 08:44 PM
Searching for a motivated web programmer(PHP, OOP, JAVASCRIPT, AJAX) APTB The Lounge 0 05-18-2008 06:00 PM


All times are GMT. The time now is 06:36 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design