如何使用 php 放置自定义错误消息?x-moz-errormessage效果很好,但oninvalid="this.setCustomValidity则不然。
// Formular erstellen
$Formular = $javascript . '
<br><p class="button_center"><a class="btn btn-primary" href="feedback_en.php">to the feedbacks</a></p>
<form name="Form" action="feedback_en.php?eintragen" method="post" accept-charset="UTF-8">
<p><br>
<label class="form-control-label">Your name
<input required="" oninvalid="this.setCustomValidity('Please fill this field.');" onchange="try{setCustomValidity('')}catch(e){};" x-moz-errormessage="Please fill the field!" type="text" class="form-control" name="name" value="' . $name . '" size="35" maxlength="35">
</label>
</p>
'
转义setCustomValidity('Please fill this field.')
中周围的撇号,以便 PHP 可以将它们视为文字字符,而不是 PHP 字符串的结束/打开字符。
读取转义序列。
所以,正确的是:
$Formular = $javascript . '... oninvalid="this.setCustomValidity('Please fill this field.');" ...';
例如,如果您在test.php
页面中编写此简单的 PHP 代码:
<?php
$myName = 'My name is 'Julie'.';
echo $myName;
然后,您将收到以下错误:
解析错误:语法错误,意外的"朱莉"(T_STRING( [路径]/测试.php在第 3 行
而以下带有转义撇号的代码将起作用:
<?php
$myName = 'My name is 'Julie'.';
echo $myName;