0

I have a single page of HTML that includes PHP code.

I am using the PHP to send data from a form to the same page using $_SERVER['PHP_SELF'] as the form action.

There is an isset($_POST... condition in the PHP that detects if the page has loaded with data via POST. If this is true, the PHP compares the value sent to a set maximum value.

  • If the data is less than the maximum value, a table is displayed.

  • If the data exceeds the maximum value, I would like to display an error message using jQuery: $('.error_box').append("Error");

The error message is not displaying. I think this is because the PHP is trying to make changes to .error_box before it has loaded.

How can I make sure that the error display function is available, given that I am validating the form data via PHP as soon as the page loads?

Arve
  • 8,058
  • 2
  • 22
  • 25
Andy C
  • 119
  • 1
  • 9

2 Answers2

3

You don't have to use javascript or jQuery to add content to '.error_box'

You should add it with PHP directly.

EDIT:

You must know that you can't directly execute javascript via PHP.

Here is how your page is created then rendered (this is simpler than reality) :

  1. Your server receive the request to display a page
  2. The PHP code is executed and output an HTML (+CSS +JS) page
  3. The output is send back to the client who ask for page
  4. The client browser parse the HTML and render it using css rules you specified
  5. The javascript is executed
AMDG
  • 955
  • 1
  • 9
  • 25
1

Wrap it in a document ready function, like so:

$(document).ready(function() {
    $('.error_box').append("Error");
});

Edit: AMDG is probably right also...

Jonathan Lerner
  • 450
  • 1
  • 4
  • 11