gpt4 book ai didi

JavaScript、AJAX、PHP : can't set field values after getting json string

转载 作者:行者123 更新时间:2023-12-03 07:39:41 25 4
gpt4 key购买 nike

这是我的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方法的if条件下如果我这样做: $('#productlength').val(data.Length);那么文本框什么也不显示,但如果我喜欢这个 $('#productlength').val(data.length);然后它显示 json 数组的长度,但我无法设置任何字段。

所以请帮帮我。谢谢

最佳答案

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

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

我建议你去掉 php 中多余的数组

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

还请注意,您的 php 使用 mysql 扩展非常不安全,该扩展已被弃用并且不会清理查询输入

关于JavaScript、AJAX、PHP : can't set field values after getting json string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com