Checking if a PHP array is empty in JavaScript -
hi have form user can enter 1 or more books db. whenever user enters 1 book , forgets enter title, javascript alert comes , alerts him enter title. if has 2 or more books , forgets enter title, alert doesn't show up.
this javascript function.
function validateform() { var a=document.forms["submit_books"]["title"].value; if (a==null || a=="") { alert("please enter title"); return false; } var status = false; var emailregex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i; if (document.submit_books.email.value.search(emailregex) == -1) { alert("please enter valid email address."); return false; } }
and here php code
<form method="post" name="submit_books" onsubmit="return validateform()" action="<?php echo $_server['php_self']; ?>"> <?php ($i=1; $i<$num_of_books + 1; $i++){ echo "<strong>book # $i</strong><br><br>"; ?> <label for="title">*title</label>: <input type="text" id="title" size="60" name="title[]" autocomplete="off"/><br><br> <?php }?> <input type="submit" name="submit" value="submit books"> </form>
i tried putting php array javascript one.
<? $js_array = json_encode($title); echo "var title = ". $js_array . ";\n"; ?> var index = 1; if( index < title.length) { alert("please enter title"); return false; }
there must easier way doing this
try use inside html form
<label> <span>book title: (required)</span> <input name="book" type="text" placeholder="please enter books title" required autofocus> </label>
then use javascript validate
(function() { // create input element testing var inputs = document.createelement('input'); // create supports object var supports = {}; supports.autofocus = 'autofocus' in inputs; supports.required = 'required' in inputs; supports.placeholder = 'placeholder' in inputs; // fallback autofocus attribute if(!supports.autofocus) { } // fallback required attribute if(!supports.required) { } // fallback placeholder attribute if(!supports.placeholder) { } // change text inside send button on submit var send = document.getelementbyid('submit'); if(send) { send.onclick = function () { this.innerhtml = '...processing'; } } })();
Comments
Post a Comment