duanbei2914 2013-07-17 10:14
浏览 56
已采纳

不要使用JavaScript在MYSQL数据库中提交现有值

I have a HTML text field where I'm adding unique values to my database. At the moment I'm using PHP to validate that the value entered in the text field does not already exist in my database. I wonder if this is possible to achieve with JavaScript so that I can prevent the form from submitting if there is an error.

EDIT: This is how I've come so far

The JavaScript is executed in the form-tag by onsubmit="return validateForm()". I get the "System Error!" message and the form is submitted even though I say it should not.

function validateForm(){

    // Get all elements with the required-class 
    var x = document.getElementsByClassName('required');

    var submitForm = true;

    for(var i = 0; i < x.length; i++){

        // Fetch input
        var input = x[i];

        // Fetch input value
        var inputValue = input.value;

        if(input.disabled == false){

            // Check if the value exist             
            if(inputValue == null || inputValue == ''){

                // Red border
                input.style.borderColor = "#ff0000";

                // Don't submit the form
                submitForm = false;

            } else if(input.name == 'company'){

                $.ajax({

                    type: 'GET',
                    url: '../includes/ajax_mysql.php?company='+input.value,
                    success: function(data){

                        if(data == '1'){ submitForm = false; } else{ submitForm = true; }

                        alert(data);

                    },
                    error: function(data){

                        submitForm = false;

                        alert("System Error!");

                    }

                });

            }

        }

    }

    if(!submitForm){ return false; } else{ return true; }

}
  • 写回答

4条回答 默认 最新

  • dsh7623 2013-07-17 10:16
    关注

    You cannot access the DB directly through Javascript.

    It's not possible to access the database directly through Javascript. The solution is to use AJAX to call a server-side PHP page which then queries the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条