Here is my code for product.php:
<script src="../Scripts/product.js"></script>
<form id="frmAddEditProduct" name="frmAddEditProduct" method="post" action="addproduct.php">
<select class="form-control" id="companyname" name="companyname">
<option value"xyz">XYZ</option>
</select>
<input type="onlytext" class="form-control" name="productname" id="productname" placeholder="Product Name" required />
<input type="number" class="form-control" name="productlength" id="productlength" placeholder="Product Length" required />
<input type="number" class="form-control" name="productwidth" id="productwidth" placeholder="Product Width" required />
<input type="number" class="form-control" name="productheight" id="productheight" placeholder="Product Height" required />
<input type="number" class="form-control" name="productdeckle" id="productdeckle" placeholder="Deckle" required />
<input type="number" class="form-control" name="productcutting" id="productcutting" placeholder="Cutting" required />
<input type="number" class="form-control" name="productprice" id="productprice" placeholder="Price" required />
<input type="submit" name="submit" id="submit" value="Submit" class="btn btn-lg btn-success btn-block" />
</form>
<a onclick='EditProduct(id is passed);' href='#'>Edit</a>
Here is my code for editproduct.php
<?php
$connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to DB: ".mysql_error());
$db = mysql_select_db("proshell", $connection) or die("Could Not Connect to DB: ".mysql_error());
$json = array();
$ID = $_GET['Id'];
$result = mysql_query("SELECT * FROM product where Id=$ID");
while($row = mysql_fetch_array($result))
{
$row_array['Id'] = $row[0];
$row_array['Company'] = $row[1];
$row_array['Name'] = $row[2];
$row_array['Length'] = $row[3];
$row_array['Width'] = $row[4];
$row_array['Height'] = $row[5];
$row_array['Deckle'] = $row[6];
$row_array['Cutting'] = $row[7];
$row_array['Price'] = $row[8];
array_push($json, $row_array);
}
echo json_encode($json);
?>
Here is my code for product.js
function EditProduct(id) {
//RemoveValidation();
$('#status').html('');
var userid = $('#id').val();
$.ajax({
type: "GET",
url: 'editproduct.php',
data: { "Id": id},
success: function (data) {
if (data != "Error") {
$('#companyname').val(data.Company);
$('#productname').val(data.Name);
$('#productlength').val(data.Length);
$('#productwidth').val(data.Width);
$('#productheight').val(data.Height);
$('#productdeckle').val(data.Deckle);
$('#productcutting').val(data.Cutting);
$('#productprice').val(data.Price);
$('#productname').focus();
}
else {
$('#status').attr("style", "color:Red;");
$('#status').html("There was some error while getting data please refresh page and try again.");
}
}
});
}
here after clicking on Edit button in product.php It comes to javascript function and it goes in to if condition when i watch my code in console of firefox it also give me string as response like this:
[{"Id":"1","Company":"Chintan Co.","Name":"Chintan's Box","Length":"7","Width":"8","Height":"9","Deckle":"17","Cutting":"18","Price":"99"}]
In if condition of EditProduct.php method if I do like this: $('#productlength').val(data.Length); then textbox shows nothing but if I do like this $('#productlength').val(data.length); then it shows me the length of the json array but I can't set any field.
So help me out here please. Thank you