weixin_33738555 2012-10-18 19:42 采纳率: 0%
浏览 77

从PHP列表中输入.val()

I'm trying to get information from a list(listDIV) to show up in my form (formDIV), I'm getting the information from a database using JSON. I'm only getting the error "Database erro, please select something else." in my textarea field, which leads me to believe there is something wrong with my PHP.

HTML & JS:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#productList").change(function() {

    var action = $("#productForm").attr('action');
    var form_data = {
        id: $('#productList').val(),
        name: $("#name").val(),
        price: $("#price").val(),
        img: $("#img").val(),
        description: $("#description").val()
    };

var id="something";
    $.getJSON("getProduct.php",form_data,function(data){
        switch(data.retval){
            case 0: $("#name").value(data.data.prodName);
            break;
            default: $("#description").html("Database error, please select something else.");
            break;
        }
        });
    });
});
</script>  
</head><body>

<div id="listDIV">
<select id="productList" name="productList" size="8">
    <option value="123" class="prodID">Terminator Series</option>
    <option value="124" class="prodID">2001 A Space Odyssey</option>
    <option value="125" class="prodID">Serenity</option>
    <option value="126" class="prodID">Alien Quadrilogy</option>
    <option value="127" class="prodID">12 Monkeys</option>
    <option value="128" class="prodID">Final Fantasy</option>
</select>
</div><div id="formDIV">
<form id="productForm" action="getProduct.php" method="post">
    <input type="text" id="name" placeholder="Name" /><br />
    <input type="text" id="price" placeholder="Price" /><br />
    <input type="text" id="img" placeholder="Image" /><br />
    <textarea id="description" placeholder="Description"></textarea><br />
<input type="submit" id="save" value="Save" />
</form>
</div>
</body></html>

PHP (getProduct.php):

<?php
ob_start();
session_start();
mysql_connect('localhost', 'root', 'root') or 
    die('Could not connect: ' . mysql_error());
mysql_select_db('productDB') or
    die ('Can\'t use database: ' . mysql_error());

// retval: 0 - login ok, 1 - login failed, 2 - internal error
$json = array("retval" => 2, "data" => NULL, "debug" => "");

 $id=json_decode($_REQUEST['id']);
 $prodID=$id->id;

$sql="SELECT * FROM productTB WHERE prodID=" . $prodID;


$json['debug'] .= "SQL query was: ".$sql."
";
$result=mysql_query($sql);
if (!$result) {
    $json['debug'] .= "SQL query failed
";
    $json['debug'] .= "Other output: ". ob_get_contents();
    ob_end_clean();
die(json_encode($json));
}
$count=mysql_num_rows($result);

if($count==1){
    $json['retval'] = 0;
    $json['data'] = mysql_fetch_assoc($result);
} else {
    $json['retval'] = 1;
}
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
echo json_encode($json);

Table Structure for my DB:

--
-- Table structure for table `productTB`
--

CREATE TABLE `productTB` (
`prodID` int(11) NOT NULL,
`prodName` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`prodPrice` decimal(10,2) NOT NULL,
`prodDesc` longtext COLLATE utf8_unicode_ci NOT NULL,
`prodImg` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`prodID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  • 写回答

1条回答 默认 最新

  • weixin_33735077 2012-10-18 19:54
    关注

    You need to pass in the variable.. not the string

    $sql="SELECT * FROM productTB WHERE prodID=" . $prodID;
    

    Though I would recommend using preparedstatements

    EDIT:

    You are not passing ID to your php page.. Here's what you can access - These are what you are passing to your PHP -

    Change

    var form_data = {
        name: $("#name").val(),
        price: $("#price").val(),
        img: $("#img").val(),
        description: $("#description").val()
    };
    

    to

    var form_data = {
        id: $('#productList').val(), // <-- pass the id
        name: $("#name").val(),
        price: $("#price").val(),
        img: $("#img").val(),
        description: $("#description").val()
    };
    

    You either need to Pass ID to the page or query to get the ID first from the DB. Then you can use the ID in your query to get the results

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集