Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的php代码有问题吗_Php - Fatal编程技术网

我的php代码有问题吗

我的php代码有问题吗,php,Php,显示可变内容不起作用 下面是变量名为$schanName的php代码 我试着在任何我想要的地方呼出这根弦 <div id="err" style="color:#fff;"><?php echo $schanName ?></div> 但我不知道我在那里做错了什么,它不起作用 在这里编辑我的整个页面 <!doctype html> <html> <head> <meta charset="utf-8"> <

显示可变内容不起作用

下面是变量名为$schanName的php代码

我试着在任何我想要的地方呼出这根弦

<div id="err" style="color:#fff;"><?php echo $schanName ?></div>
但我不知道我在那里做错了什么,它不起作用

在这里编辑我的整个页面

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<div id="error"> i must display that error here</div>

<form class="s_submit" method="post"> 
<label class="def_lab">File:</label> 
<input class="t_box" type='text' name='filename' placeholder='File Name'> 
<label class="t_lab def_lab">Select Folder:</label> 
<select id="soflow" name="thisfolder"> 
<option selected="selected" value="default">Default</option> 
<option value="../embed/tv/xbox/">Xbox</option> 
<option value="Folder2">Folder2</option> 
<option value="Folder3">Folder3</option>
</select><br><br> 
<label class="def_lab">Text Area 1:</label><br> 
<textarea class="tarea_box" type='text' name='strin'></textarea><br><br> 
<label class="def_lab">Text Area 2:</label><br> 
<textarea class="tarea_box" type='text' name='strin2'></textarea><br> 
<button type="submit" class="btn btn-primary">Submit</button> 
</form>  

<?php
var_dump($_POST);
$fNum = 'File Name is Required';
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
if(empty($_POST['filename'])) {
echo $fNum;
return; 
}
if($_POST['thisfolder'] == 'default') { 
$schanName = 'Please select a Folder';
return; 
}
$filename=$_POST['filename']; 
$words = array("1", "2", "3", "4", "5"); 
$arrlength = count($words); 
$found = false; 

for($x = 0; $x < $arrlength; $x++) { 
if($filename == $words[$x]) 
{ 
$found = true; 
} 
} 

if($found) 
{ 
echo 'Not a valid File Name'; 
return; 
} 
// the name of the file to create 
$filename=$_POST['filename']; 
// the name of the file to be in page created 
$strin=$_POST['strin']; 
// the name of the file to be in page created 
$strin2=$_POST['strin2']; 
// the name of the folder to put $filename in 
$thisFolder = $_POST['thisfolder']; 
// make sure #thisFolder of actually a folder 
if (!is_dir(__DIR__.'/'.$thisFolder)) { 
// if not, we need to make a new folder 
mkdir(__DIR__.'/'.$thisFolder); 
} 
// . . . /[folder name]/page[file name].php 
$myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php"; 

// This is another way of writing an if statment 
$div = ($strin !== '') ? '<div id="area_code">'.$strin.'</div>' : '<div id="area_code">'.$strin2.'</div>'; 



$fh = fopen($myFile, 'w'); 
$stringData = ""; 

fwrite($fh, $stringData); 
fclose($fh); 
} 
?>

</body>
</html>

您需要给这个$schanName一个值,并且需要在php之后添加html表单,因为如果您在html之后使用php,则无法获取值或变量名。但是如果你使用php,然后是html,那么你就可以从php中获取值和变量到html中

对于错误报告,我在数组中输入了u个错误,所以u将一次获得所有错误

<!doctype html>
<html>
<head>
   <meta charset="utf-8">
   <title>Untitled Document</title>
</head>

<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")
{ 
    if(empty($_POST['filename']))
    {
       $schanName[] = 'File Name is Required';
    }

    if($_POST['thisfolder'] == 'default') 
    { 
       $schanName[] = 'Please select a Folder'; 
    }

    $filename=$_POST['filename']; 
    $words = array("1", "2", "3", "4", "5"); 
    $arrlength = count($words); 
    $found = false; 

    for($x = 0; $x < $arrlength; $x++)
    { 
       if($filename == $words[$x]) 
       { 
          $found = true; 
       } 
    } 

    if($found) 
    { 
       $schanName[] = 'Not a valid File Name';
    } 

    // the name of the file to create 
    $filename=$_POST['filename']; 
    // the name of the file to be in page created 
    $strin=$_POST['strin']; 
    // the name of the file to be in page created 
    $strin2=$_POST['strin2']; 
    // the name of the folder to put $filename in 
    $thisFolder = $_POST['thisfolder']; 
    // make sure #thisFolder of actually a folder 
    if (!is_dir(__DIR__.'/'.$thisFolder))
    { 
        // if not, we need to make a new folder 
        mkdir(__DIR__.'/'.$thisFolder); 
    } 
    // . . . /[folder name]/page[file name].php 
    $myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php"; 

    // This is another way of writing an if statment 
    $div = ($strin !== '') ? '<div id="area_code">'.$strin.'</div>' : '<div   id="area_code">'.$strin2.'</div>'; 

   $fh = fopen($myFile, 'w'); 
   $stringData = ""; 

   fwrite($fh, $stringData); 
   fclose($fh); 
} 

?>


<?php
  // display your errors here
  if(!empty($schanName))
  {
     foreach ($schanName as $sn)
     {
        echo '<div id="error"><ul><li>'.$sn.'</li></ul></div>';
     }
  }
?>  


<form class="s_submit" method="post"> 
<label class="def_lab">File:</label> 
<input class="t_box" type='text' name='filename' placeholder='File Name'> 
<label class="t_lab def_lab">Select Folder:</label> 
<select id="soflow" name="thisfolder"> 
    <option selected="selected" value="default">Default</option> 
    <option value="../embed/tv/xbox/">Xbox</option> 
    <option value="Folder2">Folder2</option> 
    <option value="Folder3">Folder3</option>
</select><br><br> 
<label class="def_lab">Text Area 1:</label><br> 
<textarea class="tarea_box" type='text' name='strin'></textarea><br><br> 
<label class="def_lab">Text Area 2:</label><br> 
<textarea class="tarea_box" type='text' name='strin2'></textarea><br> 
<button type="submit" class="btn btn-primary">Submit</button> 
</form> 

</body>
</html>

首先在if语句外部声明变量$schanName,然后可以在if语句中使用它。它也将显示在html中


这是一个范围问题。查看变量范围以了解更多详细信息。

当用户不输入任何内容时,您不会处理该情况。您仅在用户输入default时处理为什么要使用“return”?这是在函数中还是在其他地方?退货是怎么回事?是的,但我说的是$schanName它根本不像我在上面那样显示它必须显示请选择一个名称代码的其余部分是什么?若语句以任何一种方式结束,那个么您都是错的。我认为你需要向我们展示所有相关的代码。在它的当前状态下,有太多的问题,比如:函数中是否有if语句?我还没有回答这个问题。您是否删除了更多的代码?它和HTML在同一个文件中吗?相同的作用域?如果在尝试回显后定义它,您希望如何打印它?代码已编辑。在这种情况下,必须向变量添加一个值,检查值为$schanName。如果没有显示$schanName的值,你必须检查它是否为空。再次检查我问题的底部bro@Mario兄弟,你能告诉我什么是密码保护页面最安全的方法吗?你可以这样做,只允许注册/登录的用户使用会话。如果用户已注册并登录,则为其提供访问权限,如果未登录,则向其显示登录表单。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<div id="error"> i must display that error here</div>

<form class="s_submit" method="post"> 
<label class="def_lab">File:</label> 
<input class="t_box" type='text' name='filename' placeholder='File Name'> 
<label class="t_lab def_lab">Select Folder:</label> 
<select id="soflow" name="thisfolder"> 
<option selected="selected" value="default">Default</option> 
<option value="../embed/tv/xbox/">Xbox</option> 
<option value="Folder2">Folder2</option> 
<option value="Folder3">Folder3</option>
</select><br><br> 
<label class="def_lab">Text Area 1:</label><br> 
<textarea class="tarea_box" type='text' name='strin'></textarea><br><br> 
<label class="def_lab">Text Area 2:</label><br> 
<textarea class="tarea_box" type='text' name='strin2'></textarea><br> 
<button type="submit" class="btn btn-primary">Submit</button> 
</form>  

<?php
var_dump($_POST);
$fNum = 'File Name is Required';
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
if(empty($_POST['filename'])) {
echo $fNum;
return; 
}
if($_POST['thisfolder'] == 'default') { 
$schanName = 'Please select a Folder';
return; 
}
$filename=$_POST['filename']; 
$words = array("1", "2", "3", "4", "5"); 
$arrlength = count($words); 
$found = false; 

for($x = 0; $x < $arrlength; $x++) { 
if($filename == $words[$x]) 
{ 
$found = true; 
} 
} 

if($found) 
{ 
echo 'Not a valid File Name'; 
return; 
} 
// the name of the file to create 
$filename=$_POST['filename']; 
// the name of the file to be in page created 
$strin=$_POST['strin']; 
// the name of the file to be in page created 
$strin2=$_POST['strin2']; 
// the name of the folder to put $filename in 
$thisFolder = $_POST['thisfolder']; 
// make sure #thisFolder of actually a folder 
if (!is_dir(__DIR__.'/'.$thisFolder)) { 
// if not, we need to make a new folder 
mkdir(__DIR__.'/'.$thisFolder); 
} 
// . . . /[folder name]/page[file name].php 
$myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php"; 

// This is another way of writing an if statment 
$div = ($strin !== '') ? '<div id="area_code">'.$strin.'</div>' : '<div id="area_code">'.$strin2.'</div>'; 



$fh = fopen($myFile, 'w'); 
$stringData = ""; 

fwrite($fh, $stringData); 
fclose($fh); 
} 
?>

</body>
</html>
<!doctype html>
<html>
<head>
   <meta charset="utf-8">
   <title>Untitled Document</title>
</head>

<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")
{ 
    if(empty($_POST['filename']))
    {
       $schanName[] = 'File Name is Required';
    }

    if($_POST['thisfolder'] == 'default') 
    { 
       $schanName[] = 'Please select a Folder'; 
    }

    $filename=$_POST['filename']; 
    $words = array("1", "2", "3", "4", "5"); 
    $arrlength = count($words); 
    $found = false; 

    for($x = 0; $x < $arrlength; $x++)
    { 
       if($filename == $words[$x]) 
       { 
          $found = true; 
       } 
    } 

    if($found) 
    { 
       $schanName[] = 'Not a valid File Name';
    } 

    // the name of the file to create 
    $filename=$_POST['filename']; 
    // the name of the file to be in page created 
    $strin=$_POST['strin']; 
    // the name of the file to be in page created 
    $strin2=$_POST['strin2']; 
    // the name of the folder to put $filename in 
    $thisFolder = $_POST['thisfolder']; 
    // make sure #thisFolder of actually a folder 
    if (!is_dir(__DIR__.'/'.$thisFolder))
    { 
        // if not, we need to make a new folder 
        mkdir(__DIR__.'/'.$thisFolder); 
    } 
    // . . . /[folder name]/page[file name].php 
    $myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php"; 

    // This is another way of writing an if statment 
    $div = ($strin !== '') ? '<div id="area_code">'.$strin.'</div>' : '<div   id="area_code">'.$strin2.'</div>'; 

   $fh = fopen($myFile, 'w'); 
   $stringData = ""; 

   fwrite($fh, $stringData); 
   fclose($fh); 
} 

?>


<?php
  // display your errors here
  if(!empty($schanName))
  {
     foreach ($schanName as $sn)
     {
        echo '<div id="error"><ul><li>'.$sn.'</li></ul></div>';
     }
  }
?>  


<form class="s_submit" method="post"> 
<label class="def_lab">File:</label> 
<input class="t_box" type='text' name='filename' placeholder='File Name'> 
<label class="t_lab def_lab">Select Folder:</label> 
<select id="soflow" name="thisfolder"> 
    <option selected="selected" value="default">Default</option> 
    <option value="../embed/tv/xbox/">Xbox</option> 
    <option value="Folder2">Folder2</option> 
    <option value="Folder3">Folder3</option>
</select><br><br> 
<label class="def_lab">Text Area 1:</label><br> 
<textarea class="tarea_box" type='text' name='strin'></textarea><br><br> 
<label class="def_lab">Text Area 2:</label><br> 
<textarea class="tarea_box" type='text' name='strin2'></textarea><br> 
<button type="submit" class="btn btn-primary">Submit</button> 
</form> 

</body>
</html>