Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.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
如果stament正确,如何使用PHP if语句并执行fwrite if,if_Php_Html_Fwrite - Fatal编程技术网

如果stament正确,如何使用PHP if语句并执行fwrite if,if

如果stament正确,如何使用PHP if语句并执行fwrite if,if,php,html,fwrite,Php,Html,Fwrite,我正在尝试一个带有复选框的html表单。然后我使用php验证表单并获取复选框的值。如果复选框对应于某个文本,则会有一个fwrite语句 以下是html复选框的代码: <input class="choose" type="checkbox" name="check_list[]" value="Python">&nbsp; Python</label> <input class="choose" type="checkbox" name="check_lis

我正在尝试一个带有复选框的html表单。然后我使用php验证表单并获取复选框的值。如果复选框对应于某个文本,则会有一个fwrite语句

以下是html复选框的代码:

<input class="choose" type="checkbox" name="check_list[]" value="Python">&nbsp; Python</label>
<input class="choose" type="checkbox" name="check_list[]" value="Java"> &nbsp; Java And Html(opitnal)</label>
<input class="choose" type="checkbox" name="check_list[]" value="PHP"> &nbsp; PHP And Html(optinal)</label>
以下是if声明:

$fh = fopen("myfile.php", "w") or die("Error! :(");
if ($lan == "Python") { 
    $page1 = "Some Code For The Fwrite";
    fwrite($fh, $page1);
}
fopen可以工作,但fwrite实际上没有写入文件,它应该将if语句中的代码写入文件


此外,表单验证的工作方式似乎就像我回显复选框一样,它返回正确的内容

check_list[]
是一个数组,这意味着如果用户选中所有的
$lan
将不等于
python
java
,而是等于数组的最后一个元素,如果选中了多个复选框,则fwrite将不起作用

if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $selected) {
    $lan = $selected; //$lan will equal to the last value of the array, if more than one checkbox is ticked
}
}

您可以在
if
语句中打开您的文件,此代码只有在选中python时才有效

if($lan == 'Python'){
$file = fopen("myfile.php","w") or die("Error! :(");
$page1 = "Some Code For The Fwrite again";
fwrite($file,$page1);
fclose($file);
}

您尝试写入文件后正在打开该文件。。。。。怪不得我的剧本写得不好,对不起。我现在已经改了,你的笔迹写得很好。您在哪里
echo
?我希望您在完成对文件的写入后也能
fclose()
该文件。我无法判断不关闭是否会导致数据无法保存,但关闭它肯定会有帮助。您确定
if
块中的代码运行吗?可能
if
条件的计算结果为
FALSE
并且
fwrite()
没有任何机会运行。
if($lan == 'Python'){
$file = fopen("myfile.php","w") or die("Error! :(");
$page1 = "Some Code For The Fwrite again";
fwrite($file,$page1);
fclose($file);
}