doutuo7126 2014-09-29 21:22
浏览 65
已采纳

使用$ .post从javascript页面验证目录以调用webservice PHP函数

I am trying without success to use the $.post function to test (via a webservice that calls a PHP function "is_dir") if a folder already exists on a server and then I want it to return a string or boolean value back to my javascript page before I proceed to dynamically write the new files that will be placed there. The file path of the folder to be tested is "built" using jQuery which captures form data. I need to define (in a variable) if the directory exists and then be able to access that variable from outside of the $.post function (not from within, using success callback). This is so I can proceed in javascript as follows:

if {directory exists} then capture more form data (via jQuery) and $.post to webservice that calls PHP to update database

Outside of the $.post function, the value of my return variable is undefined.

I think I may be over-complicating this. Any suggestions? Thank you, in advance.

  • 写回答

1条回答 默认 最新

  • dongnuan7956 2014-09-30 15:34
    关注

    Please see my comment to @Steve above:

       <script type='text/javascript'>
    
        //function gathers form data, validates constructed file path and then writes to DB
        function post_FormData() {
    
            var week_number          = $("#form_week_number").val();
            var program              = $("#form_program").val();
            var course               = $.trim($("#form_course_number").val());
            var form_content_type    = $("input:radio[name=content_type]:checked").val();
            var content_type         = "";      
            var activity_title_Val   = $.trim($("#form_activity_name").val());
            var activity_title_Split = activity_title_Val.split(" ");
            var activity_title_Clean = new Array();
    
            //this for-loop constructs a valid directory folder name from form data
            for(var i=0, l=activity_title_Split.length; i<l; i++) {
    
                activity_title_Split[i] = activity_title_Split[i].replace(/[^a-z0-9\s]/gi,"");
                activity_title_Clean[i] = activity_title_Split[i];
                activity_title_Split[i] = activity_title_Split[i].replace(/\b[a-z]/g, function(letter){return letter.toUpperCase();});
            }
    
            var activity_title = activity_title_Split.join("");
    
            var file_path  = "";
    
            file_path     += "/CourseFiles/" + program + "/" + program + course + "/" + content_type + "/Week" + week_number + "/activity-" + activity_title;
    
            var message    = "<div id=\"confirmation_container_contents\"><p><b>Confirm Content Repository file path: </b><br></p>";
    
            //begin web service call to PHP function
            $.post('webservices/create_PA_webservices.php', {web_service: "go_check_if_exists", data_file_path: file_path}, function(data){
    
                var exists = data.does_exist; //json_encoded RESPONSE FROM ASYNC REQUEST
    
                if(exists == "Y") {
    
                    message   += file_path;
                    message   += "<br><br><br><center><b>An activity folder with this name already exists.</b></center>";
                    message   += "<br><br><center>Please edit the activity title and resubmit.</center>";
                    message   += "<br><br><br><center><input type=\"image\" src=\"pa_images/editButton.jpg\" id=\"editButton\" value=\"edit\"></center></div>";
    
                    $("#confirmation_container").empty();
                    $("#confirmation_container").append(message);
    
                }
                else if(exists == "N") {
    
                    message   += file_path;
                    message   += "<br><br><center><input type=\"image\" src=\"pa_images/editButton.jpg\" id=\"editButton\" value=\"edit\">";
                    message   += "&nbsp\;&nbsp\;&nbsp\;<input type=\"image\" src=\"pa_images/confirmButton.jpg\"  id=\"confirmButton\" value=\"confirm\"></center></div>";
    
                    $("#confirmation_container").empty();
                    $("#confirmation_container").append(message);
    
                }
    
                $(function(){//edit proposed file path
    
                    $("#editButton").click(function() {
    
                        $("#confirmation_container").empty();
    
                    });//end function edit path button
    
                });//end anonymous function
    
    
                $(function(){//confirm proposed file path and write to DB
    
                    $("#confirmButton").click(function() {
    
                        go_post_FormData(activity_title_Val, file_path, week_number,  program, course, content_type);
                        $("#create_practice_activity").hide();
                        $("#build_practice_activity").show();
                        $("#activity_is_new").val("N");
    
                    });//end function confirm path button
    
                });//end anonymous function
    
    
            }, "json").fail(function() {alert("The go_check_if_exists webservice call has failed");}); //end web service call  
    
        }//end function post_FormData declaration
    
        </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器