2017-03-04 64 views
-1

显示变量的内容是不工作有我的PHP代码

这里的问题是我的PHP代码的$schanName

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

变量名我已经试过这呼应了在以往任何时候我想

字符串
<div id="err" style="color:#fff;"><?php echo $schanName ?></div> 

但它没有显示任何内容。

,如果我尝试这样它会工作

if($_POST['thisfolder'] == 'default') { 
echo 'Please select a Name'; 
return; 
} 

,但我不知道我做错了那里,它不工作。

编辑#2这里我的整个页面

<!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> 
+1

当用户什么都不输入时,您不处理这种情况。您只能在用户输入“默认”时处理 – Psi

+0

您为什么使用“返回”? – user3526204

+0

这是在一个函数或东西? 'return'怎么回事? –

回答

1

ü需要给这个$ schanName值和HTML表单需要PHP后添加,因为如果u HTML后使用PHP不能得到的值或变量名称。但是,如果你使用PHP然后HTML然后你可以抓取值和变量从PHP到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> 
+1

如果您在试图回应它之后定义它,您会如何期待它被打印? – Qirel

+0

代码编辑。在这种情况下,U必须为变量添加一个值,他检查的是$ schanName。如果不显示$ schanName的值,你必须检查它是否为空。 – Mario

+0

再次检查我的问题的底部bro –

0

声明变量$ schanName在if语句之外,那么你可以在if语句中使用它。它也会显示在html中。

这是一个范围界定问题。检查变量作用域以获取更多细节