JavaScripts,AJAX,PHP:获取json字符串

时间:2016-02-17 14:16:17

标签: javascript php jquery json ajax

这是我的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>

这是我的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);
?>

这是我的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.");
            }
        }
    });
}
在点击product.php中的编辑按钮后,这里来了javascript函数,当我在firefox的控制台中查看我的代码时,它进入if条件,它也给我字符串作为这样的响应:

[{"Id":"1","Company":"Chintan Co.","Name":"Chintan's Box","Length":"7","Width":"8","Height":"9","Deckle":"17","Cutting":"18","Price":"99"}]

在EditProduct.php方法的条件下 如果我喜欢这样:$('#productlength')。val(data.Length); 然后textbox显示什么,但如果我喜欢这个$('#productlength')。val(data.length);然后它显示了json数组的长度,但我无法设置任何字段。

请帮帮我。 谢谢

1 个答案:

答案 0 :(得分:1)

您正在将数据对象包装在不必要的数组中。

因此,在javascript中,您实际上需要将data.Company更改为data[0].Company,并将其他属性更改为类似。

我建议你摆脱php中的额外数组

而不是array_push($json, $row_array);只发送$row_array

还要注意你的php使用mysql扩展名是非常不安全的,这个扩展名已被弃用,而不是清理查询输入