Web Analytics Made Easy -
StatCounter calling a javascript function from php - CodingForum


No announcement yet.

calling a javascript function from php

  • Filter
  • Time
  • Show
Clear All
new posts

  • calling a javascript function from php

    Hi, I have created a php form and want to validate each fields length using a simple javascript function which taked the variable name and its maximum length. The page self submits, then checks that all fields have values in php. It is once this has been met that I want to call this new function.

    Secondly, Im not sure where in the page to declare the javascript functions - the top of my page is <? session_start(); which means i cant declare the html and head tags at the very top of the page. These tags are output via php someway down the page, so how do i get round this?

    Thanks, Andy.

  • #2
    java script is client based, meaning that after u submit a form it don't exist really.

    so there are 2 solutions

    write the checks in php
    write the checks in javascript before submitting.


    • #3
      So can javascript functions only ever be called with an event? I would rather use javascript coz im probably gonna have to do the same sort of checks on other pages and it would save me writing the functions out again.

      Where would I call the javascript function then, on clicking submit?


      • #4
        something like this, but am sure we have some better form validation peps around lol

        function validate(aobj)
        var valid=false;

        <form name="test" action="test.php">

        <input type="Submit" value="test" onclick="validate(this);return false;">


        • #5
          I think I'm getting confused with all this because the page (edit_details.php) with the form is self - submitting i.e. if all the details entered are ok then the edit_details.php prints a summary, if not it outputs the form again. Its probably easier to view the code to see what i mean!
          Attached Files


          • #6
            Yes you can create the javascript functions (like the Garadon's) in the head tags or something.

            The the submit button just needs to include the onClick like Garadon has shown.

            Alternatively, you can have an attribute in the form tag called onSubmit calling your functions. Your function will have to return true to submit (valid) or false to not submit.

            In the case that it submitted, your php page will print off the summary page in which case it will not be calling your javascript functions (which may or may not be displayed on that page depending on how you code it...).

            Hope that helps,