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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办