0

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

1 Answer 1

1

You are wrapping the data object in a needless array.

So in the javascript you would actually need to change data.Company to data[0].Company and similar for other properties.

I would suggest you get rid of the extra array in php

Instead of array_push($json, $row_array); just send $row_array

ALso note your php is very insecure using mysql extension which is deprecated and not sanitizing input for query

Sign up to request clarification or add additional context in comments.

6 Comments

also add dataType:'json' to $ajax
how to do that $row_array stuff?
echo json_encode($row_array) . Should also define $row_array in case no data returned in query
Please use browser dev tools console to check for errors. This is your most important first line of troubleshooting javascript
well you should have seen errors and mentioned them in question. They tell you a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.