duanhe6718 2018-01-24 14:32
浏览 52
已采纳

使用相同的JSON数据键调用一个php文件的多个Ajax调用

I am trying to validate list of dynamic text fields. Validation needs an AJAX call to interact with server. At the backend I have written just one php file that reads the input request data and performs operation. Below is the example.

abc.js

row_count = 6
for (i = 1; i <=row_count; i++) {
    id = "#val"+i.toString() ;
    $(id).change(function(){
        input_val="random";
        $.ajax({
            url:"url.php",
            type:post,   
            async:true,
            dataType: 'json',
            data : {temp:input_val},
            success:function(result){},
            error: function (request, status, error) {}
        });
    });
}           

url.php

<?php
$random_val = $_POST['temp'];
$cmd = 'systemcommand '.$random_val;
$flag = exec($cmd);
if ($flag == 0){
    echo json_encode(array("status"=>'Fail'));
}
else{
    echo json_encode(array("status"=>'Success'));
}
?>

It works fine when the row_count = 1 (Just one text field) but fails when the input is more than 1. When the count is more than 1, the php script is not able to read the request data(The key in JSON data "temp"). it is blank in that case. Any lead or help should be appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • douhandie6615 2018-01-24 14:45
    关注

    Your javascript bit needs some adjusting, because you do not need to define an ajax for every single element. Use events based on a class. Also, since input behave differently than select, you should setup two different event class handlers.

    function validateAjax ( element ) {
        var input_val = element.val();// get the value of the element firing this off
        $.ajax({
            url: "url.php",
            type: 'post',   
            async: true,
            dataType: 'json',
            data : { temp: input_val },
            success: function(result) {
                // check your result.status here
            },
            error: function (request, status, error) { }
        });
    }
    $(".validate_change").on("change",function() { // for selects
        validateAjax( $(this) );
    });
    $(".validate_input").on("input",function() { // for text inputs
        validateAjax( $(this) );
    });
    

    And for your select or input you add that appropriate class.

    <select class="validate_change" name="whatever"><options/></select>
    <input class="validate_input" name="blah">
    

    PS

    I really worry about this code you have:

    $cmd = 'systemcommand '.$random_val;
    $flag = exec($cmd);
    

    So, you are just executing anything that is coming in from a webpage POST var??? Please say this website will be under trusted high security access, and only people using it are trusted authenticated users :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵