1
form.php

<?php
// define variables and set to empty values
$nameErr = $ageErr = $qualErr = $mobileErr = "";
$name = $age = $qual = $mobile = "";
$error="";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["age"]))
     {$ageErr = "Age is required";}
   else
     {$age = test_input($_POST["age"]);}

   if (empty($_POST["qual"]))
     {$qualErr = "Qualification is required";}
   else
     {$qual = test_input($_POST["qual"]);}

   if (empty($_POST["mobile"]))
     {$mobileErr = "mobile Number is required";}
   else
     {$mobile = test_input($_POST["mobile"]);}
     if (empty($_POST["name"]))
     {$nameErr = "Name is required";}
   else
     {$name = test_input($_POST["name"]);
     if(file_exists($name)!=$name)
     {
    $ab=mkdir($name,0777);//creating folder in server side, entered name as folder name
    $c=realpath($name);//created folder path
    $d=$name.".txt";//creating text file which should come under folder with the same name 
    $sam="$c/".$d;
    $a=fopen($sam,'w+');
    $space="\r\n";
    $details=$name.$space.$age.$space.$qual.$space.$mobile;
    fwrite($a,$details);//inside text file information is stored

    }

     else
     {
         echo $name."\talready exists";

     }
     }

}
function test_input($data)
{

     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);

     return $data;
}
?>
-------------------------------------------------------------------------------------------
form.html


<html>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
 <script>
 $(document).ready(function()
 {
         $('#sub').click(function()
         {
             $("#formajax").submit(function(e)
         {
        e.preventDefault();
        dataString=$("#formajax").serialize();


$.ajax({
type: "POST",
url:"form.php",
data:dataString,

success:function(data){
    alert("success");

},
error:function(data){
    alert("name already exists as a folder");
    }

})
return false;

});
});
});

</script>    
<style>
.error {color: #FF0000;}
</style>
</head>

<body>
<form id="formajax"  action="" method="post" enctype="multipart/form-data">
<pre>
NAME         :<input type="text" name="name" id="name"><span class="error">*<?php echo $nameErr;?></span><br/>
AGE          :<input type="text" name="age" id="age"><span class="error">*<?php echo $ageErr;?></span><br/>
QUALIFICATION:<input type="text" name="qual" id="qual"><span class="error">*<?php echo $qualErr;?></span><br/>
MOBILE       :<input type="text" name="mobile" id="mob"><span class="error">*<?php echo $mobileErr;?></span>
<br/>

              <input id="sub" type="submit" value="submit" name="submit">
<pre>
</form>
</body>

当我执行此代码时,在输入详细信息文件夹后,在文件夹中创建文本文件,在文本文件中存储信息...文件夹和文本文件名都是使用用户输入的名称创建的。如果它已经存在,那么它不会用那个名字创建..现在我收到像成功一样的警报消息,即使名字已经退出..我无法通过警报获得名字已经存在的消息..请找出错误并回复谢谢

4

1 回答 1

3

文件存在函数被错误调用(它返回布尔值)

http://php.net/manual/en/function.file-exists.php

if(file_exists($name)!=$name)

应该替换为

if(!file_exists($name))
于 2013-10-25T12:37:33.867 回答