新手在这里。 我正在编写具有这些功能的代码 1.添加新产品的表格 2.将此新产品的名称作为其选项之一添加到选择元素中 3.选择新添加的产品名称,将其显示在单独的表格中。
我已经有了这个工作,但有一个问题。 当我尝试添加新产品并从列表中选择这个新添加的产品时,它不会在单独表单内的输入框中显示数据。
但是当我刷新整个页面时。它完美地运作。除了刷新整个页面之外还有其他任何方法可以使它工作吗?
我在想是否可以只刷新select标签。
下面是我的代码:
添加新产品表单:
string removeAfterFrom = removeSelect.Substring(0, removeSelect.IndexOf(" FROM ", StringComparison.CurrentCultureIgnoreCase));
用于添加产品的AJAX脚本:
<div id='bgw' style='background:#fff; height:80%; top:7%;left:2%; width:90%;position:absolute;margin;text-align:center;padding:20px; border-radius:10px;'>
<label style='font-size:15px;font-weight:bold;color:#ff6f01;'>Add New Product to List</label><br><br><br>
<table style='margin-left:30px;'>
<tr>
<td><label>Product Name</label></td>
<td><label>Product Category</label></td>
</tr>
<tr>
<td><input type="text" class="input" id="prod_name" style='width:250px;' /></td>
<td>
<select id = "prod_category" style="font-family:verdana; width:250px; height:34px; border: 1px solid #000000;">
<option value="">---</option>
<optgroup label="Apparel">
<option value="Footwear">Footwear</option>
<option value="Underwear">Underwear</option>
<option value="Outerwear">Outerwear</option>
<option value="Cloth">Cloth</option>
<option value="Jewelry">Jewelry</option>
<option value="Headwear">Headwear</option>
<option value="Eyewear">Eyewear</option>
<option value="Legwear">Legwear</option>
<option value="Blanket">Blanket</option>
<option value="Carpet">Carpet</option>
<option value="Sport Series">Sport Series</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<td><label>Unit</label></td>
<td><label>PO Number</label></td>
</tr>
<tr>
<td>
<select id="prod_unit" style="width:250px;height:34px;" class="input">
<option value="">---</option>
<option value="Piece/s">Piece/s</option>
<option value="Roll/s">Roll/s</option>
<option value="Set/s">Set/s</option>
<option value="Pair/s">Pair/s</option>
<option value="Box/es">Box/es</option>
</select>
<td><input type="text" class="input" id="prod_po" style="width:250px;"/></td>
</td>
</tr>
<tr>
<td><label>Brand</label></td>
</tr>
<tr>
<td><input type="text" class="input" id="prod_brand" style="width:250px;"/></td>
</tr>
<tr>
<td><label>Color/Material/Finish</label></td>
<td><label>Technical Specifications/Rating</label></td>
</tr>
<tr>
<td><textarea rows="4" cols="40" id='prod_color' style="border:1px solid #000000;" ></textarea></td>
<td><textarea rows="4" cols="40" id='prod_spec' style="border:1px solid #000000;" ></textarea></td>
</tr>
<tr>
<td><label>Shipping Mark</label></td>
</tr>
<tr>
<td><textarea rows="4" cols="40" id='prod_ship' style="border:1px solid #000000;"></textarea></td>
<td>
<input type='button' id='cancel_prod' value='Cancel' style='width:100px;height:30px;border:none;border-radius:5px;background:#ff6f01;color:#fff;font-weight:bold;font-size:14px;cursor:hand;cursor:pointer'>
<input type='button' id='save_prod' value='Save Product' style='width:150px;height:30px;border:none;border-radius:5px;background:#ff6f01;color:#fff;font-weight:bold;font-size:14px;cursor:hand;cursor:pointer'>
</td>
</tr>
</table>
</div>
ajaxrequests.php
$.ajax({
url : 'ajaxrequests.php',
type : 'POST',
async : false,
data : {
prodsave : 1,
compid : compid,
pname : prodname,
pcat : prodcat,
punit : produnit,
ppo : prodpo,
pbrand : prodbrand,
pcmf : prodcmf,
ptech : prodtech,
pship : prodship
},
success: function(result){
if (result == 0) {
$('.name_of_product').each(function(){
var new_prod = $('#prod_name').val();
$(this).append('<option value="'+new_prod+ '">'+new_prod+'</option>');
})
alert('New Product details has been saved!');
$('#prod_name').val("");
$('#prod_category').val("");
$('#prod_po').val("");
$('#prod_brand').val("");
$('#prod_unit').val("")
$('#compid').val("");
$('#prod_color').val("");
$('#prod_spec').val("");
$('#prod_ship').val("");
$('#bgw').hide(500);
$('#bgb').hide(500);
}else if(result == 1){
alert('There was a problem saving the product details. Pelase try again later.');
}else if (result == 2) {
alert("Please fill in all fields");
}else if(result == 3){
alert("Product Name already exists! Please press cancel and select from the list or if you are adding a new product, give the product a different name.");
}
}
});
** JQUERY用于在单独的表单上显示数据*
include('config.php');
if (isset($_POST['prodsave'])) {
if (!empty($_POST['pname']) && !empty($_POST['pcat']) && !empty($_POST['ppo']) && !empty($_POST['pbrand']) && !empty($_POST['pcmf'])&& !empty($_POST['ptech']) && !empty($_POST['pship'])){
$showid = $_POST['compid'];
$pname = $_POST['pname'];
$pcat = $_POST['pcat'];
$ppo = $_POST['ppo'];
$pbrand = $_POST['pbrand'];
$pcmf = $_POST['pcmf'];
$ptech = $_POST['ptech'];
$pship = $_POST['pship'];
$punit = $_POST['punit'];
$qry = $handler->prepare("SELECT * From product_list WHERE plist_name = ? AND plist_compid = ? ");
$qry->execute(array($pname,$showid));
$row = $qry->rowCount();
if ($row >=1) {
echo 3;
}else{
$qry = $handler->prepare("INSERT INTO product_list (plist_compid, plist_name, plist_category, plist_unit, plist_po, plist_brand, plist_cmf, plist_spec, plist_ship) VALUES (?,?,?,?,?,?,?,?,?)");
$qry->execute(array($showid, $pname, $pcat, $punit, $ppo, $pbrand, $pcmf, $ptech, $pship));
if ($qry) {
echo 0;
}else{
echo 1;
}
}
}else{
echo 2;
}
exit();
}
if (isset($_POST['psearch'])) {
if (!empty($_POST['product']) && !empty($_POST['compid'])) {
$showprod = $_POST['product'];
$showid = $_POST['compid'];
$qry = $handler->prepare("SELECT * FROM product_list WHERE plist_name = ? AND plist_compid = ?");
$qry->execute(array($showprod,$showid));
$rows = $qry->fetch(PDO::FETCH_ASSOC);
header("Content-type: text/x-json");
echo json_encode($rows);
exit();
}
}
任何帮助将不胜感激..谢谢