I already have a php validation and form setup with PHP. So if someone forgets a username, the validation will add to the errors array and display it on the page when submitted. Now, instead of displaying some text for the error (Username can't be blank) I just want the input box highlighted in red, which I know needs JavaScript. I am having some trouble running this JavaScript function properly. Below is the code.
JavaScript:
<script type="text/javascript">
function myFunction() {
document.getElementById("usernameformitem").className = "formerror";
}
</script>
PHP:
if (isset($_POST['submit'])) {
$required_fields = array("username");
function validate_presences($required_fields) {
global $errors;
foreach($required_fields as $field) {
//the $value is now the un/pw without whitespaces
$value = trim($_POST[$field]);
//if the value does not exist/is blank
if (!has_presence($value)) {
//remember field is really un or pw
$errors[$field] = fieldname_as_text($field) . " can't be blank";
echo "<script>";
echo "myFunction();";
echo "</script>";
}
}
}
validate_presences($required_fields);
HTML:
<form action="new_user4.php" method="post">
<div id="usernameformitem">
<p>Username:
<input type="text" name="username" value="" />
</p>
</div>
<input type="submit" name="submit" value="Create User" />
</form>
Thanks in advance!
make sure the div exists before calling the function, for example echo the script after the div or use
window.onload=function() { if (myFunction) myFunction(); }
But why even submit the form?
window.onload=function() {
document.getElementById("form1").onsubmit=function() {
if (this.username.value="") {
myFunction();
return false; // stop submission
}
return true;
}
<?PHP if (!has_presence($value)) { .... ?>
if (myFunction) myFunction();
<?PHP } ?>
}
PS: NEVER call a form element name="submit"
- it will block you from ever submitting by script
First I have a Controller like this: login.controller.js: angular.module('app').controller('LoginController', function($scope,UserService,$location) { $scope.submit = function() ...
First I have a Controller like this: login.controller.js: angular.module('app').controller('LoginController', function($scope,UserService,$location) { $scope.submit = function() ...
I have the following manifest CACHE MANIFEST # cache-revision-29541 # cache-creation-date: Fri Dec 5 11:33:29 GMT 2014 CACHE: app.html NETWORK: * And the following app.html <!DOCTYPE html> &...
I have the following manifest CACHE MANIFEST # cache-revision-29541 # cache-creation-date: Fri Dec 5 11:33:29 GMT 2014 CACHE: app.html NETWORK: * And the following app.html <!DOCTYPE html> &...
I have an array of simple objects that looks like the following: var objects =[ { "group": "05", "format": "Legacy", "active": false }, { "group": "05", "...
I have an array of simple objects that looks like the following: var objects =[ { "group": "05", "format": "Legacy", "active": false }, { "group": "05", "...
I am using account-google, I don't know why it doesn't publish emails to the client. From the doc about Meteor.user(): By default the server publishes username, emails, and profile (writable by ...
I am using account-google, I don't know why it doesn't publish emails to the client. From the doc about Meteor.user(): By default the server publishes username, emails, and profile (writable by ...