撒拉嘿哟木头 2015-05-29 08:49 采纳率: 100%
浏览 29

jQuery AJAX脚本未保存

Newbie here, I am working with these two jquery scripts for the whole day now. When I fire the first script the second script won't work.

The first script checks whether a certain product name is already existing in the database, while the second script is the function to save the data entries on the database.

When I fire the first script it works perfectly fine. When the product name is existing it disables the save product button, When the product name is available it enables the save button.

If there are no conflicts on the data entries, you can save the details on the database, but when I fire the script to save it to database(which is the second script below) it doesn't work.

Below is my code.

First Script:

$('#prod_name').on('focusout',function(){
var prodname = $('#prod_name').val();
var compid = $('#compid').val();
$.ajax({
        url     :   'php/ajax.php',
        type    :   'POST',
        async   :   false,
        data    :   {
                    check   : 1,
                    compid  : compid,
                    pname   : prodname
                    },
        success: function(result){
            if (result==1) {
                alert('Product Name Already Exists! Please rename the product or give the product name a prefix');
                $('#save_prod').prop('disabled',true);
            }else{
                $('#save_prod').prop('disabled',false);
            }   
        }
        });

});

second script

$('#save_prod').on('click', function(event){
event.preventDefault();
var prodname = $('#prod_name').val();
var prodcat = $('#prod_category').val();
var prodpo = $('#prod_po').val();
var prodbrand = $('#prod_brand').val();
var produnit = $('#prod_unit').val();
var compid = $('#compid').val();
var prodcmf = $('#prod_color').val();
var prodtech = $('#prod_spec').val();
var prodship = $('#prod_ship').val();

if(prodname == ""){
    alert('Enter Product Name');
}else if(prodcat == ""){
    alert('Select Product Category');
}else if(produnit == ""){
    alert('Please select Unit');
}else if(prodpo == ""){
    alert('Enter PO Number (if not applicable please input N/A)');
}else if(prodbrand == ""){
    alert('Enter Product Brand (if not applicable please input N/A)');
}else if(prodcmf == ""){
    alert('Define Color/Material/Finish (if not applicable please input N/A)');
}else if(prodtech == ""){
    alert('Define Technical Specifications/Rating (if not applicable please input N/A)');
}else if(prodship == ""){
    alert('Define Shipping Mark (if not applicable please input N/A)');
}else{

    $.ajax({
        url     :   'php/ajax.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) {
                    var new_prod = $('#prod_name').val();
                    $('#name_of_product').append('<option value="'+new_prod+ '">'+new_prod+'</option>');
                    $('#name_of_product').trigger("chosen:updated");
                    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.");
                }   
            }
    });
}
});

Any help would be gladly appreciated.

Thanks guys!!

  • 写回答

1条回答 默认 最新

  • weixin_33696822 2015-05-29 10:54
    关注

    I saw that you have same url i.e. php/ajax.php in both of your ajax scripts, sometimes browsers caches the response of the ajax and blocks the preceding ajax requests to the same url considering the requests as repetitive.

    So for such cases always use - cache: false, so that the browser considers the each request a new request and does not caches or blocks it.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办