-
Alert in un form
Salve ragazzi, ho creato un motore di ricerca.
Vorrei che cliccando sul submit senza aver scritto niente nel input type text appaia un alert o messaggio di errore, è possibile con javascript o php?
Il sito è http://chatmemesbig.altervista.org/cmdsearch/index.html
-
Prova con php:
Codice PHP:
<html>
<head>
</head>
<body>
<?php
if(isset($_POST[q])) {
if($_POST[q] == "") {
$messaggio = "ALERT! Il campo ricerca e' vuoto!";
} else {
header("Location: http://www.google.com/search?q=$_POST[q]");
}}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="q" placeholder=" Cerca..."> <br/>
<button type="submit">Cerca</button>
<button type="reset">Cancella</button>
<h2><?php echo $messaggio; unset($messaggio); ?></h2>
</body>
</html>
Dovrebbe fare ciò che chiedi.
P.S.1: ovviamente devi rinominare la pagina da index.html a index.php
P.S.2: hai scritto alcune fesserie nel codice... una fra tutte, il tag <br> che non va chiuso... o, meglio, va auto-chiuso scrivendo <br/> ...e <tr> e <td> non è che servano a molto se non sei dentro una <table>...
-
Grazie mille, ma ho già fatto con un codice javascript! :
<form method="get" action"" onsubmit="if (this.nome.value=='') {alert('Inserisci qualcosa da cercare no?'); return false;}"> :)