dongshuobei1037 2014-12-19 19:08
浏览 37

Javascript循环来比较文本框中的值

for starters, I'm NOT working with arrays

with that being said, I need your help...I created a webform in PHP that retrieves values from a mysql table and displays them with its own mysqli_fetch_array command, in that loop it generates a textbox for each record...so far so good.

The created textbox (input element) is so that the user can type in the sequential number of how to reorder the records...example

Original Order of Records

1 Alpha 2 Bravo 3 Charlie 4 Delta 5 Echo

and user needs it to be in this order

User input sequential

2 Alpha 5 Bravo 1 Charlie 4 Delta 3 Echo

the new order of the records will be saved on a temporary table in the database before insertion on the main table, kinnda like a preview for the new order, something like this:

New Order of Records

1 Charlie 2 Aplha 3 Echo 4 Delta 5 Bravo

Now what I need is a function that helps me display a message if the user duplicates a sequential unique, if they type in number 1 in 2 or more records, when I hit the button for the preview I need it to loop through all the input boxes and check their values, compare it with the other inputs and determine if there are duplicates or not....if there're no duplicates, continue with PHP code.....if there are duplicates, display an error so the user seeks for the duplicate and change it (inputs left in blank will not be considered for the insertion in the preview table)

I already have this:

<script>
function check(){
    const t='texto';
    //var conta=1;
    var contar=<?php echo $contar; ?>;
    //while (conta<=contar)
    for(var conta=1;conta<contar;conta++)
    {
        alert(conta);
        var contas=conta+1;
            if (document.forms['OrderPreview'][t+conta].value!=document.forms['OrderPreview'][t+contas].value)
            {
                alert('All Good');
                return false;
            } 
            else 
            {
                alert('Something bad');
                return true;
            }
        //conta++;
        contas=conta+1;
        if (contas>contar){alert('End');return;}
    }
};
</script>

Which only works with first and second inputs....currently I have 62, but this number is random for every data upload...I was reading some posts here stating that using autoincrements in javascript is bad but as you can see I've tried both ways of doing the autoincrement and none seems to work properly....

I'm open to suggestions in Ajax as well, but I must confess I'm a novice in that language...

Any help or orientation will be gladly accepted, thanks in advance :)

  • 写回答

1条回答 默认 最新

  • duanquanzhi5560 2014-12-19 19:18
    关注

    you have to get $contar; value through ajax otherwise once the page is loaded the php value will not change since it will be retrieved before page load.

    try using like this:

    $.ajax({
        type:'post',
            url:'url to php file',
            data:{/*post variables*/},
            success:function(data){
                // use data in for loop.put entire for loop containt here           
            }
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败