JavaScript Run a JavaScript function based off of PHP validation and form submit javascript function based classes,javascript function based on id,javascript

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!

Answer:1

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(); }

FIDDLE

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

Answer:2

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() ...

  1. angular jasmine tutorial
  2. angular jasmine mock service
  3. angular jasmine spyon
  4. angular jasmine run single test
  5. angular jasmine and karma
  6. angular jasmine createspyobj
  7. angular jasmine stub
  8. angular jasmine vs jest
  9. angular jasmine mock input
  10. angular jasmine mock observable
  11. angular jasmine unit test
  12. angular jasmine inject
  13. angular jasmine vs karma
  14. angular jasmine click button
  15. angular jasmine test event emit
  16. angular jasmine inject service
  17. angular jasmine async
  18. angular jasmine @input
  19. angular jasmine test window.location.href
  20. angular jasmine sleep

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> &...

  1. application cache manifest
  2. application cache manifest file
  3. application cache manifest example
  4. application cache manifest fetch failed
  5. application cache manifest mvc
  6. app cache manifest
  7. app cache manifest deprecated
  8. app cache manifest file
  9. app cache manifest html5
  10. application cache error manifest fetch failed
  11. html5 application cache manifest rewriting
  12. html5 application cache manifest
  13. html application cache manifest
  14. application cache api manifest selection is deprecated
  15. chrome application cache manifest
  16. check application cache manifest
  17. web app cache manifest

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 ...