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 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码