2

I've got a problem figuring out how to pass a variable to parameter of the ajaxForm() function. I am getting templatedir path from hidden form field after clicking on form button in jQuery (this works great) and I can pass it to other function but not to the ajaxForm() parameter:

    var templatedir;

    // Get variable of templatedir
    $('form#contact input[type="submit"]').on('click',function(){
        templatedir = $('input[name=templatedir]').fieldValue();   
    });

    // Validate Form
    function validateform(formData, jqForm, options) {         
        ...
        // IF I CALL TEMPLATEDIR HERE IT WORKS
    }   

    // Send to PHPMailer
    $("form#contact").ajaxForm({
        url: templatedir, // IT DOESNT WORK HERE
        type: 'post', 
        beforeSubmit: validateform,
        target: '#msg',
        clearForm: true
    });

I tried the solution from this answer (How do i pass variables between functions in javascript) as you can see but it doesn't work.

2
  • Mmm...is this right? templatedir = $('input[name=templatedir]').fieldValue(); Commented Jul 6, 2016 at 18:31
  • Yes, this is right, I can pass result to console.log and it works just fine. Commented Jul 6, 2016 at 18:38

2 Answers 2

0

Your variable templatedir is undefined.

You should call ajaxForm() in click event.

$('form#contact input[type="submit"]').on('click',function(event) {
    event.preventDefault();

    // Send to PHPMailer
    $("form#contact").ajaxForm({
        url: $('input[name=templatedir]').fieldValue(),
        type: 'post', 
        beforeSubmit: validateform,
        target: '#msg',
        clearForm: true
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

The points of interest are:

  • you cannot use the templatedir variable before you set it
  • change templatedir = $('input[name=templatedir]').fieldValue(); to

    templatedir = $('input[name=templatedir]').val();

  • remove url: templatedir, // IT DOESNT WORK HERE and set it in validateform: before form submission

My snippet:

var templatedir;

$(function () {
  $('form#contact input[type="submit"]').on('click', function(){
    templatedir = $('input[name=templatedir]').val();
  });
  function validateform(formData, jqForm, options) {
    // because you now are in the beforesubmit event
    // you can change the url.....
    options.url = templatedir;
  }

  $("form#contact").ajaxForm({
    type: 'post',
    beforeSubmit: validateform,
    target: '#msg',
    clearForm: true
  });
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://malsup.github.io/jquery.form.js"></script>

<form id="contact" action="www.google.com" method="post">
    templatedir: <input type="text" name="templatedir"/>
    <input type="submit" value="Submit Comment" />
</form>
<div id="msg"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.