PHP で関数を使用して、フォーム要素の「値」属性の値をエコーしようとしています。問題は、複数の引数を渡すと、エラーは返されませんが、ページ上のこのフォーム要素と残りの HTML が表示されないことです。引数が 1 つの場合、関数は期待どおりに機能します。この関数は、値とフォーマット定数を受け取り、フォーマットされた値をフォーム要素の「value」属性にエコーする必要があります。
追加メモ: 一重引用符は結果を変更しません。2 番目の引数を削除すると、正常に処理されます。
function myFunction($input, $format)
{
if(isset($format))
{
switch($format)
{
case "num":
if(is_num($input))
{
echo number_format($input);
break; //only breaks if the input & data type match, else it will return an error with catch
}
case "percent":
if(is_num($input))
{
echo number_format($input, 2);
break; //only breaks if the input & data type match, else it will return an error with catch
}
default:
echo "Error: either format doesn't match the variable type or format type is undefined. Input: $input; Format: $format";
}
}
echo number_format($input);
}
...
<input type="text" name="M7" id="M7" value="<?php myFunction($value, "num"); ?>" onkeypress="return validateNumber(event)" readonly="true" />