見よう見まねで検索フォーム作ってアラート表示も実装してみました。
他のページではアラートが出るのですが検索実行ページではアラートが出ないです。
どうしてなのでしょうか。
【トップのページ】 <html> <script> <?php echo $message; ?> function search() { if (document.forms[0].sample.value == "") { alert("文字を入れてください。"); document.forms[0].sample.focus(); return false; } return true; } </script> ... <form action="検索実行ページ.php" method="POST" onsubmit="return search()"> <input type="text" name="sample"> <input type="submit"> </from> .... 【検索実行ページ.php】 <?php // デバッグを入れよう ini_set('display_errors', true); error_reporting(E_ALL); if (!empty($_POST['word'])) { $db_host=''; $db_name=''; $db_user=''; $db_pass=''; $db_table=''; try { $pdo = new PDO("mysql:dbname=$db_name;host=$db_host;charset=utf8", $db_user, $db_pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]); } catch (PDOException $e) { header('Content-Type: text/plain; charset=UTF-8', true); exit($e->getMessge()); } $query = "SELECT .... FROM sample WHERE ... LIKE '%".$_POST['word']."%' OR .... LIKE '%".$_POST['word']."%'"; $stmt = $pdo->query($query); $rows = $stmt->fetchAll(); } ?> <html> <script> <?php echo $message; ?> function search() { if (document.forms[0].sample.value == "") { alert("文字を入れてください。"); document.forms[0].sample.focus(); return false; } return true; } </script> ... <form action="検索実行ページ.php" method="POST" onsubmit="return search()"> <input type="text" name="sample"> <input type="submit"> </from> .... <?php if (0 < count($rows)): ?> <?php foreach ($rows as $row) : ?> <div class="samplebox"> <ul> <li><?= h($row['id']); ?></li> <li><?= h($row['message']); ?></li> <li><?= h($row['sample']); ?></li> </ul> </div> <?php endforeach; ?> <?php endif; ?>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。