Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    145
    Thanks
    0
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    or
    write the checks in javascript before submitting.

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Posts
    145
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    something like this, but am sure we have some better form validation peps around lol

    <script>
    function validate(aobj)
    {alert();
    var valid=false;
    //---checks---//

    if(valid)
    {
    aobj.form.submit();
    }
    }
    </script>
    <form name="test" action="test.php">

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

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Posts
    145
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Attached Files

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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,
    Sadiq.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •