Webサイトに2つのラジオボタンを含む単純なフォームがあり、どちらのラジオボタンもクリックされなかった場合のJavascriptフォーム検証を追加しました。(SOに感謝)ただし、ユーザーが[OK]ボタンをクリックすると、ページはフォームの「プロセス」ページにリダイレクトされます。ユーザーがアラートボックスの[OK]ボタンをクリックしたときに、ページをリロードするか、アラートボックスを閉じるという動作が望ましいです。
私が話していることをさらに確認するには、chrisrjones.comにアクセスして、chrisrjones.com/splash.phpにリダイレクトしてからEnterをクリックします。(ラジオボタンを選択せずに)アラートボックスが表示され、[ OK ]ボタンが押されると、ページはフォームプロセスページにリダイレクトされます(私が望むものではありません)。
<!DOCTYPE html>
<html lang="en">
<head>
<title>chrisRjones.com - Splash</title>
<?php
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
$num = array_rand($ar);
return $ar[$num];
}
/////////////////////////////////////////////////////////////////////
// This is the only portion of the code you may need to change.
// Indicate the location of your images
$root = '';
// use if specifying path from root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'pics/';
// End of user modified section
/////////////////////////////////////////////////////////////////////
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
<!-- begin google analytics PHP include -->
<?php include_once("analyticstracking.php") ?>
<!-- end google analytics PHP include -->
<!-- Javascript radiobutton form validation -->
<script type="text/javascript">
function valForm(form) {
var btn1 = parseInt(form.splash.value, 10) || 0;
btn = (btn1 )
if (btn < 3) {
alert('You either like splash pages or hate em, choose one ...please');
return false; }
else {
alert('Button value ' + btn + ' selected');
}
}
</script>
<!-- Javascript radiobutton form validation END -->
</head>
<body>
<img src="<?php echo $path . $img ?>" alt="" width="640" height="auto" />
<h1>Hello Javascript redirection.</h1><br />
<p>
<form name="tosplashornottosplash" action="splash-process.php" method="post">
Splash pages are stupid
<input type="radio" name="splash" value="false" /> No
<input type="radio" name="splash" value="true" /> Yes
<input type="submit" name="formSubmit" onClick="valForm(tosplashornottosplash)" value="Enter" />
</form>
</body>
</html>