This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Add custom javascript to form


  1. moonlightss
    Member

    I would like to add some custom javascript functions to a couple of the fields. How and where can i do this?

    Thanks

    Posted 14 years ago #
  2. redityo

    You should edit "view-functions.php" file to add custom java script. May i know what java script code do you want to add ?


    MachForm Support

    Posted 14 years ago #
  3. moonlightss
    Member

    Yes, i would like to add an onblur to the zip code field to calculate the distance from my location to the address entered with a google API. i would also be interested in adding a button to calculate and display a price for based on some options that were selected on the form.

    Posted 14 years ago #
  4. moonlightss
    Member

    Can you give me some direction on where exactly to put the onblur code?

    Thanks.

    Posted 14 years ago #
  5. moonlightss
    Member

    Here is kind of what i want to do:
    <input type="text" name="address1" value="Address 1" onblur="showLocation(); return false;"/>

    I would also like to have a hidden field that the mileage is written too.

    Posted 14 years ago #
  6. redityo

    Basically you can put the java script in "view-functions.php" file around line 1839, put the script bellow this code :

    {$calendar_js}

    and for example you want to call the java script code on onblur event for text field with element id 2 on form 5. Then, go to 40 for this code :

    $element_markup = <<<EOT

    and add this code above that line

    if ($_GET['id'] == 5 && $element->id ==  2) {
    	$javascript_code = "onblur=\"showLocation(); return false;\"";
    }

    after that, go to line around line 46 for this code :

    <input id="element_{$element->id}" name="element_{$element->id}" class="element text {$element->size}" type="text" value="{$element->default_value}" />

    change it to

    <input id="element_{$element->id}" name="element_{$element->id}" class="element text {$element->size}" type="text" value="{$element->default_value}" {$javascript_code} />

    MachForm Support

    Posted 14 years ago #
  7. moonlightss
    Member

    I will give that a try.

    Thanks!!

    Posted 14 years ago #
  8. moonlightss
    Member

    That worked. However, i'm getting an error. my code works and it does exactly what i want it to. here is the error i'm getting:

    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
    Timestamp: Sat, 22 May 2010 02:03:09 UTC

    Message: Object doesn't support this property or method
    Line: 1
    Char: 1
    Code: 0
    URI: http://url/machform/js/view.js

    here is the javascript code i'm using:

    <script type="text/javascript">
    var geocoder;
    var location2;
    var gDir;

    function initialize() {
    geocoder = new GClientGeocoder();
    gDir = new GDirections();
    GEvent.addListener(gDir, "load", function() {
    var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
    var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
    document.form_1.element_6.value = drivingDistanceMiles.toFixed(1);
    });
    }

    function showLocation() {
    geocoder.getLocations("3120 Alford Bend Rd, Gadsden, AL, 35903", function (response) {
    if (!response || response.Status.code != 200)
    {
    alert("Sorry, we were unable to geocode the first address");
    }
    else
    {
    location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    geocoder.getLocations(document.form_1.element_3_1.value + ', ' + document.form_1.element_3_5.value, function (response) {
    if (!response || response.Status.code != 200)
    {
    alert("Sorry, we were unable to geocode the second address");
    }
    else
    {
    location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    gDir.load('from: ' + location1.address + ' to: ' + location2.address);
    }
    });
    }
    });
    }
    </script>

    I also added an onload="initialize();" to the body tag.

    so far i haven't seen any functionality lost, but i'm sure something is missing.

    Thanks.

    Posted 14 years ago #
  9. moonlightss
    Member

    ok. another thing. everything seems to work fine calling the form from a url, but if i embed the form in my php template, it doesn't work.

    Posted 14 years ago #
  10. moonlightss
    Member

    OK. i seem to have everything working fine if i call the form via url. but if i embed the form with the advanced form code, my javascripts stop working. do i need to have the code somewhere else for advanced code placement?

    Posted 14 years ago #
  11. moonlightss
    Member

    ok, all i need to know now is how to get my if statement to filter on form id. i can get element->id to work, but with advanced embed form code, form->id doesn't seem to work. can you tell me the variable that is used for form id on advanced embeded form?

    Thanks.

    Posted 14 years ago #
  12. redityo

    If you're using advance code, you should add the java scipt code around line 2172. You can put exactly above this code :

    {$calendar_js}

    It's inside "display_integrated_form" function block. And to get the form id with advance embed code, you should use a SESSION variable. You can add like this in advance embed code :

    <?php
            @session_start();
    	require("/domain/machform.php");
    	$mf_param['form_id'] = 1;
            $_SESSION['form_id'] = 1;
    	$mf_param['base_path'] = 'http://domain.com/machform/';
    	display_machform($mf_param);
    ?>

    then you can use the session like this

    if ($_SESSION['form_id'] == 5 && $element->id ==  2) {
    	$javascript_code = "onblur=\"showLocation(); return false;\"";
    }

    MachForm Support

    Posted 14 years ago #
  13. moonlightss
    Member

    no workie... i did make one little change to see if that might be it, but no dice..

    i changed $_SESSION['form_id'] == 5;
    to $_SESSION['form_id'] = 5;

    still not working. any ideas??

    Thanks so much for the GREAT support!

    Posted 14 years ago #
  14. yuniar

    Can you send us your file please? and let us know the URL to the form.
    We'll check it.

    Mail to: customer.service [at] appnitro.com


    MachForm Founder

    Posted 14 years ago #
  15. moonlightss
    Member

    just sent file today. Thanks.

    Posted 14 years ago #

RSS feed for this topic

Reply