Welcome to WebmasterWorld Guest from 54.158.214.111

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Javascript into PHP.

     
5:16 pm on May 4, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Apr 21, 2007
posts:264
votes: 0


This is the code which handles the AJAX response :

<?
if (!isset($_COOKIE['user'])) {
header ("location:index.php");
} else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="css.css">
<script language="javascript">
function showdiv(id) {
if (document.getElementById){
obj2 = document.getElementById(id);
if (obj2.style.display == "none"){
obj2.style.display = "";
}
}
}
function hidediv(id1) {
if (document.getElementById){
obj = document.getElementById(id1);
if (obj.style.display == ""){
obj.style.display = "none";
}
}
}


function callAjax (id, company, squantity, tquantity, action)

{
//////THIS IS ALL ABOUT CALLING THE DATA BASED ON THE SELECT FIELD////////////////////////////////
if (document.getElementById){
obj1 = document.getElementById(id);
if (obj1.style.display == "none"){
obj1.style.display = "";
}
}
string = "<img src='ajax.gif'/>"
string+= " Please wait while the data is loaded..."
document.getElementById(id).innerHTML = string;
showHint(id, company, squantity, tquantity, action);

}

function showHint(id, company, squantity, tquantity, action){
var AdminRequest = window.ActiveXObject? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
AdminRequest.onreadystatechange = function()
{
if (AdminRequest.readyState == 4)
{
if (AdminRequest.status == 200)
{
document.getElementById(id).innerHTML = AdminRequest.responseText;
}
else {
alert('Error get.php File '+ AdminRequest.statusText);
}
}
}
var infoStr = "?id="+id+"&company="+company+"&squantity="+squantity+"&tquantity="+tquantity+"&action="+action;
AdminRequest.open("GET", "sell.php"+infoStr, true);
AdminRequest.send(null);
}
</script>
<title>Untitled Document</title>
</head>
<body>
<div class = "normal" align ="center">
<?
include ('mysql.php');
$sql = "Select * from shares where user = '" . $_COOKIE['user'] ."';";
$result = mysql_query($sql);
if (!mysql_query($sql)) {
echo mysql_error(); exit ();
}
$num = mysql_numrows($result);
if ($num == 0) {
echo "Sorry, you have not bought any shares. Please go to the <a href =\"main.php\">portfolio</a> page, to buy a share."; exit ();
}
$asql = "SELECT * from users where username = '". $_COOKIE['user'] . "';";
$aresult = mysql_query($asql);
$rows = mysql_fetch_assoc($aresult);
echo "Welcome " . $_COOKIE['user'] . "<br> You have bought $num shares uptil now. You current account balance is ". $rows['initial_amount']."";
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
$new_count = $count++;
?>
<p> <div id="txtHint" align="center">
<div align="center">

<?

echo "<table><tr><td>"?><div align="left" /><? echo" Company Name : </td><td>"?><div align="left"/><? echo $row['company'] . "</td><tr><td>"?><div align="left"/><? echo" Date : </td><td>"?><div align="left"/><? echo $row['date'] ."</td><tr><td>"?><div align="left"/><? echo" Quantity : </td><td>"?><div align="left"/><? echo $row['quantity']. "</td><tr><td>"?><div align="left"/><? echo" Total Transaction Value : </td><td>"?><div align="left"/><? echo $row['transaction']."</td><tr><td>"?><div align="left"/><? echo" Bookerage : </td><td>"?><div align="left"/><? echo $row['bookerage']."</td><tr><td>"?><div align="left"/><? echo" Pmt Amt : </td><td>"?><div align="left"/><? echo $row['pmt']."</td></tr></table>" ;
?>
</div></div>
<div id ="buy">

<a href="javascript: showdiv('buyform<?php echo $new_count;?>')">SELL</a>

</div>
<div id="buyform<?php echo $new_count;?>" style="display:none; background-color:#6A6E84; font-size: 100%;font-weight: 900;border: dotted 1px Black;margin-left: 15%;margin-right: 15%;">
<div align = "right">
<a href="javascript: hidediv('buyform<?php echo $new_count;?>')">
<img src="close.jpg" style="border:0">
</a>
<div id="txtHint<?php echo $new_count;?>" align="center" style="color:Black;"></div>
</div>
<form name="form1" method="post" action="">
<table><tr><td><div align="right" />Company : </td><td><div align="left" /><? echo $row['company']?></td></tr><tr><td><div align="right">Quantity :</td><td><div align="left"><input type="text" name="qty" onkeyup="callAjax('txtHint<?php echo $new_count;?>', '<?=$row['company']?>', this.value, '<?=$row['quantity']?>', 'show');"></td></tr><tr>
<td><div id="sell" align="right"><input type="button" name="Button" value="Sell" onClick="callAjax('txtHint<?php echo $new_count;?>', '<?=$row['company']?>', qty.value, '<?=$row['quantity']?>', 'sell');"></div></td></tr></table></form></div></p>
<?
}
?>

</div>
</body>
</html>
<? }?>

And This Is The Sell.php

<?
if (!isset($_COOKIE['user'])) {
header ("location: main.php");
}
else
{
include ('mysql.php');

$sqty = $_GET['squantity'];
$tqty = $_GET['tquantity'];
$company = $_GET['company'];
$username = $_COOKIE['user'];
$password = $_COOKIE['id'];
$action = $_GET['action'];

if ($action == "show") {

$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result) =='0'){
echo "Please Login Again"; exit ();
} else {

if (!preg_match("/^([0-9]+)$/", $sqty)) {
echo "<li> Please enter a valid numeric value for Quantity. No decimal places are allowed, please enter round-off figures.</li>"; exit ();
}
else if ($sqty > $tqty) {
echo "<li> You do not enough shares to sell. </li>" ; exit ();
} else {

$query= "select * from listedcomp where company='" . $_GET['company'] . "'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$shares = $row['shares'];
$closing = $row['closing'];

$value = $closing * $sqty;
$bookerage = $value * 0.01;
$pmt_amt = $value + $bookerage;
$sql = "SELECT * FROM users where username ='" . $_COOKIE['user'] . "';";
$amount = mysql_query($sql);
$rows = mysql_fetch_assoc($amount);
$currentamount = $rows['initial_amount'];
$updatedamt = $currentamount + $pmt_amt;

echo "<table><tr><td><b>Calculations : </td></tr><tr><td><b> Quantity : </td><td></b>$sqty</td></tr><tr><td><b> Total Transaction Value :</td><td></b>$value</td></tr><tr><td><b> Bookerage :</td><td></b>$bookerage</td></tr><tr><td><b> Pmt Amt :</td><td></b>$pmt_amt</td></tr><tr><td><b> Updated Cash Balance :</td><td></b>$updatedamt</table>";

}}
} else if ($action == "sell") {

$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result) =='0'){
echo "Please Login Again"; exit ();
} else {

if (!preg_match("/^([0-9]+)$/", $sqty)) {
echo "<li> Please enter a valid numeric value for Quantity. No decimal places are allowed, please enter round-off figures.</li>"; exit ();
}
else if ($sqty > $tqty) {
echo "<li> You do not enough shares to sell. </li>" ; exit ();
} else {
?>
<script language="javascript">

if (document.getElementById){
obj = document.getElementById('sell');
if (obj.style.display == ""){
obj.style.display = "none";
}
}

</script>
<?
$query= "select * from listedcomp where company='" . $_GET['company'] . "'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$shares = $row['shares'];
$closing = $row['closing'];

$value = $closing * $sqty;
$book = $value * 0.01;
$pmt_amt = $value + $bookerage;
$sql = "SELECT * FROM users where username ='" . $_COOKIE['user'] . "';";
$amount = mysql_query($sql);
$rows = mysql_fetch_assoc($amount);
$currentamount = $rows['initial_amount'];
$updatedamt = $currentamount + $pmt_amt;

if ($sqty == $tqty) {

$sql = "DELETE FROM shares where user = '". $_COOKIE['user'] ."' AND company = '". $_GET['company']."'";
$result = mysql_query($sql);
if (!mysql_query($sql)) {
echo mysql_error(); exit ();
}
echo "<table><tr><td><b>You have sold the following shares : </td></tr><tr><td><b> Quantity : </td><td></b>$sqty</td></tr><tr><td><b> Total Transaction Value :</td><td></b>$value</td></tr><tr><td><b> Bookerage :</td><td></b>$book</td></tr><tr><td><b> Pmt Amt :</td><td></b>$pmt_amt</td></tr><tr><td><b> Updated Cash Balance :</td><td></b>$updatedamt</table>";
}
else
{
$query= "select * from shares where company='" . $_GET['company'] . "' and user = '". $_COOKIE['user'] ."'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
extract($row, EXTR_PREFIX_SAME, "wddx");
$newquantity = $quantity - $sqty;
$newvalue = $transaction - $value;
$newbookerage = $bookerage - $book;
$newpmt_amt = $pmt - $pmt_amt;

$sql = "UPDATE shares SET quantity = '$newquantity', transaction = '$newvalue', bookerage = '$newbookerage', pmt = '$newpmt_amt' where user = '" . $_COOKIE['user'] . "' and company = '" .$_GET['company']."'";
$asql = "UPDATE users SET initial_amount = '$updatedamt'";
if (!mysql_query($sql)) {
echo mysql_error();
}
if (!mysql_query($asql)) {
echo mysql_error();
}

echo "<table><tr><td><b>You have sold the following shares : </td></tr><tr><td><b> Quantity : </td><td></b>$sqty</td></tr><tr><td><b> Total Transaction Value :</td><td></b>$value</td></tr><tr><td><b> Bookerage :</td><td></b>$bookerage</td></tr><tr><td><b> Pmt Amt :</td><td></b>$pmt_amt</td></tr><tr><td><b> Updated Cash Balance :</td><td></b>$updatedamt</table>";

}}}
}}?>

As u must have noticed a small patch of Javascript into the sell.php and as it is that script is not working...i m sure i have done something silly!

What i want is that if the check on the quantity field is passed and the user clicks on Sell....The sell button should get hidden...else it shud b displayed..

Please Help :(

5:41 pm on May 4, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 24, 2007
posts:138
votes: 0


I' ll admit I don't know a lot about Javascript but I am trying to learn. What I was wondering is why you chose to hide and display your button with javascript. I know that you can use a php conditional statment to echo the image tag or script to the browser else hide it.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members