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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用$ .post函数进行测试(通过调用PHP函数的web服务) is_dir“)如果一个文件夹已经存在于服务器上,然后我希望它返回一个字符串或布尔值返回到我的javascript页面,然后我继续动态写入将放置在那里的新文件。 要测试的文件夹的文件路径是使用jQuery“构建”的,它捕获表单数据。 我需要定义(在变量中)目录是否存在,然后能够从$ .post函数外部访问该变量(不是从内部,使用成功回调)。 这样我可以按如下方式继续javascript:

如果{directory exists}然后 捕获更多表单数据(通过jQuery)和 $ .post到调用PHP的webservice 更新数据库

在$ .post函数之外,我的返回变量的值是未定义的。

我想我可能过于复杂了 。 有什么建议? 提前谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题