1

Im looking to pass one variable from my PHP code to my Javascript code to validate a figure. I'm creating a shopping cart scenario whereby i have a variable 'in_stock' which specifies the amount of stock available for a product. If I enter an amount that is greater than the 'in_stock' available i need an error to be thrown. This is currently what I have:

Java Script

   <script language="javascript">
  function numCheck() 
  {
        var enteredChar = document.getElementById('add_value').value;
        var stockavailable ="<?php echo $stock?>";

    //To check if a non-numerical value is entered
    if (isNaN(enteredChar)) 
    {
        alert("Not a Number!");
        return false;
    }
    //To check if the input is empty
    if (enteredChar=="")
    {
        alert("Empty!");
        return false;
    }
    //To check if the amount entered is not greater than the amount in stock
 if (enteredChar > stockavailable)
    {
        alert("Not enough in stock");
        return false;
    }

 }  
   </script>

This is all occurring whilst my PHP has called the database and returned the value on the screen. As follows:

PHP

$product_id =(int)$_GET['product_id'];

$username = "potiro";
$password = "pcXZb(kL";
$hostname = "rerun";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");


//select a database to work with
$selected = mysql_select_db("poti",$dbhandle)
  or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM products where product_id=$product_id");

echo '<form name="form1">';
echo '<table class="Grocery-table">'; 

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

$stock =$row['in_stock'];
$product_id = $row['product_id'];

  echo "<tr><td><b>Product ID</b></td>"; 
  echo "<td>";  
  echo $product_id;
  echo "</td></tr>";
  echo "<tr><td><b>Product Name</b></td>";
  echo "<td>";
  echo $row['product_name'];
  echo "</td></tr>"; 
   echo "<tr><td><b>Unit Price</b></td>";
  echo "<td>";
  echo $stock;
  echo "</td></tr>"; 
  echo "<tr><td><b>Unit Quantity</b></td>";
  echo "<td>";
  echo $row['unit_quantity'];
  echo "</td></tr>"; 
  echo "<tr><td><b>In Stock</b></td>";
  echo "<td>";
  echo $row['in_stock'];
  echo "</td></tr>";
  echo '<tr><td><b>Add</b></td><td><Input type="text" id="add_value" name="cart"></input></td></tr>';
  echo '<tr><td></td><td><input type="submit" value="Submit" onclick="return numCheck()"></td></tr>';
 }
echo "</table>"; 
echo "</form>";

mysql_close($dbhandle);
?>
3
  • whats the problem you facing??? Commented Apr 3, 2015 at 5:17
  • you have not initialize php variable $stock. Commented Apr 3, 2015 at 5:46
  • variable $stock = $row['in_stock']; - whatever the value is in that row Commented Apr 3, 2015 at 6:01

1 Answer 1

2

add your stock value to a hidden input value stock_value

 echo '<tr><td><b>Add</b></td><td><Input type="text" id="add_value" name="cart"  ></input><Input type="hidden" id="stock_value" name="stock_value"  value="'.trim($stock).'"></input></td></tr>';
 echo '<tr><td></td><td><input type="submit" value="Submit" onclick="return numCheck()"></td></tr>';

in javascript

<script language="javascript">
  function numCheck() 
  {
        var enteredChar = document.getElementById('add_value').value;
        var stockavailable =document.getElementById('stock_value').value;
Sign up to request clarification or add additional context in comments.

5 Comments

it seems when i add the hidden input field the "value" in the form for the Add is "<?php echo $stock; ?>" instead of blank where im supposed to place a value there.
i didn't get you... can u brief more
it is still not validating against the stock_value .. is there something in my PHP that i may need to change?
is the value been sent to javascript through id

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.