1

继续我之前的帖子..在 PHP 中从列表中输入 .val()这部分代码现在可以工作了,现在我需要能够更新和修改弹出的文本。

查询未收到任何内容。

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() {

// Sends information into form - Works 
$("#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;
        }
        });
    });
// End form information

$("#productForm").submit(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("modify.php",form_data,function(data){
        switch(data.retval){
            case 0: $("#status").html("Update successful!");
            break;
            case 1: $("#status").html("Unable to update!");
            break;
            default: $("#description").html("Database error, please try again.");
            break;
        }
        });
    });
});
</script>  
</head><body>

<div id="status"></div>
<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(修改.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']);
 $prodName=mysql_real_escape_string($_REQUEST['name']);
 $prodPrice=mysql_real_escape_string($_REQUEST['price']);
 $prodImg=mysql_real_escape_string($_REQUEST['img']);
 $prodDesc=mysql_real_escape_string($_REQUEST['description']);

$sql = "UPDATE productTB SET prodName=".$prodName.", prodPrice=".$prodPrice.", prodDesc=".$prodDesc.", prodImg=".$prodImg." WHERE prodID=".$id.";";


$json['debug'] .= "SQL query was: ".$sql."\n";
$result=mysql_query($sql);
if (!$result) {
    $json['debug'] .= "SQL query failed\n";
    $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 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;
4

1 回答 1

1

您需要防止表单提交操作的默认值。因为您的表单元素都没有name属性。它在没有任何参数的情况下发布。这就是您的 SQL 语句没有收到任何内容的原因。

你需要

$("#productForm").submit(function(e) {
      e.preventDefault();
      // your code goes here
}
于 2012-10-19T15:12:00.827 回答