2

I need to submit the form below (components/com_users/views/login/tmpl/default_login.php. ) using Ajax.

<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post">

    <fieldset>
        <?php foreach ($this->form->getFieldset('credentials') as $field): ?>
            <?php if (!$field->hidden): ?>
                <div class="login-fields"><?php echo $field->label; ?>
                <?php echo $field->input; ?></div>
            <?php endif; ?>
        <?php endforeach; ?>
                     ......
</form>

My Ajax request will come from external mobile App (on click, I run javaScript with Ajax to submit the form).

Is that possible? If yes, what parameters should I pass to the Ajax request (based on the example here : "Form submit with AJAX passing form data to PHP without page refresh" I need to pass url, data and success).

If that is possible, I'm also able to pass username and password (which the form fetch them from fieldset), I can provide them in plain text (no security concerns about that).

Community
  • 1
  • 1
Hawk
  • 5,060
  • 12
  • 49
  • 74

1 Answers1

0

Use serialize: documentation

In your case add an ID or name to your form or if its the only form in the page you can just:

$('selectorOfButtonThatTriggers').click(function(){
    $.ajax({
            type: 'POST',
            url: $('form').attr('action'),
            data: $('form').serialize(),
            success: function(data){
                   alert(data);
            }
    });
});

All your form field will be fetch and on the server side the POST keys will be the fields input names.

To add some more data programatically you can use:

data: $('form').serialize() + "&more1=" + more1value
Shlomi Hassid
  • 6,500
  • 3
  • 27
  • 48
  • The website is on different server. Don't I need some URL to the file where the form locates? – Hawk Mar 18 '15 at 05:04
  • I managed to submit the form. But the form itself is fetching the credentials from username and password fields website's login page (they are empty when I submit the form). I need to pass these values with the request itself such that the form read them from my request. Do I need to amend the form? – Hawk Mar 18 '15 at 05:55