WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] I can't my form to call a javascript function (6 posts)

  1. FXDaddy
    Member
    Posted 4 years ago #

    Hi,

    I have been pulling my hair out trying to get this to work in WordPress. I am using the Atahualpa theme.

    My form takes reservation information from the user via a form and passes it to another page, which is a booking engine site.

    Everything works fine with the form when I create a standalone php or html page but as soon as I copy the form into WordPress, it doesn't work. This all hinges on a javascript call that concatenates the date fields into a format that the booking engine can read, which is yyyymmdd. The actual input on the form is done through three separate dropdown menus.

    I use onSubmit="combDep(), combRet()" to call the JS functions in the form tag. They are departure and return dates. My JS looks like this:

    <script type="text/javascript">
    <!--
    function combDep()
    {
    var dyear = document.forms[0].d_y.value;
    var dmonth = document.forms[0].d_m.value;
    var dday = document.forms[0].d_d.value;
    document.forms[0].DepartDate.value = dyear + dmonth + dday;
    }
    function combRet()
    {
    var ryear = document.forms[0].d_y2.value;
    var rmonth = document.forms[0].d_m2.value;
    var rday = document.forms[0].d_d2.value;
    document.forms[0].ReturnDate.value = ryear + rmonth + rday;
    }
    //--></script>

    I have tried all different combinations of plugins and calling the JS external file in the header, putting the JS in the form, etc and nothing seems to work. I installed the 'AYB Javascript in Posts' plugin and that is showing some promise because it can display simple JS code, but still can't get the form to work. Any help would be greatly appreciated.

    The site is: http://islandair.server304.com/hawaii/

    Thanks,
    Hugh

  2. esmi
    Forum Moderator
    Posted 4 years ago #

  3. FXDaddy
    Member
    Posted 4 years ago #

    Thanks esmi, but I have been through those pages extensively. I have tried them and they do not work. It looks like my onSubmit is not being called at the time of the form submission. I have turned off the visual editor. I have tried onSubmit at the form level and onClick at the submission button level. Any other ideas?

    Thanks!

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    You're trying to add this via the Visual Editor? Try creating a custom page template and adding it to the template file.

  5. FXDaddy
    Member
    Posted 4 years ago #

    This was a great idea and I will use it to get the formatting on my homepage to work, but it still doesn't pass the information correctly. I tried putting it in the loop, out of the loop, etc. I also tried changing themes and it didn't work. Here is what I pasted into the new template, maybe you can see something I'm missing. I really appreciate you helping me on this one, thanks!

    [Code moderated as per the Forum Rules. Please use the pastebin]

  6. FXDaddy
    Member
    Posted 4 years ago #

    OK, I finally got it! It was because the JS was looking for form[0] as the first form on the page. It works when I do a standalone page but when I put it into the WP template, form[0] is actually the search widget. I changed the JS to reference form[1] and it worked. I appreciate your help!!

Topic Closed

This topic has been closed to new replies.

About this Topic