views:

142

answers:

3

I have this form submit code:

Event.observe(window, 'load', init, false);
function init() {
  Event.observe('addressForm', 'submit', storeAddress);
}
function storeAddress(e) {
  $('response').innerHTML = 'Adding email address...';
  var pars = 'address=' + escape($F('address'));
  var myAjax = new Ajax.Updater('response', 'ajaxServer.php', {method: 'get', parameters: pars});
  Event.stop(e);
}

How can I change it to work with jQuery?

Here is the html form:

<form id="addressForm" action="index.php" method="get">
  <b>Email:</b> <input type="text"  name="address" id="address" size="25"><br>
  <input name="Submit" value="Submit" type="submit" />
  <p id="response"><?php echo(storeAddress()); ?></p>
</form>

and this is php code at start of document:

<?php
require_once("inc/storeAddress.php");
?>
+3  A: 

Something like this:

$(function() {
    $('#addressForm').submit(function() {
        $('#response')
            .html('Adding email address...')
            .load('ajaxServer.php', { address: $('#address').val() }, function() {
                $('#address').val('');
            });
        return false;
    });
});

Note: when using .load(), the POST method is used if data is provided as an object; otherwise, GET is assumed. So, if you want to pass the data as GET, use:

.load('ajaxServer.php', 'address='+$('#address').val());

Or just get the submitted data from $_POST['address'] which would be wiser and easier in my opinion.

Tatu Ulmanen
hi this is not submitting data
steve
@steve, I've updated my answer with a solution.
Tatu Ulmanen
works awesome :)
steve
can you do one more help, how can i clear the email field once the request is submitted.thanks
steve
@steve - Change the `.load(...` line to this: `.load('ajaxServer.php', { address: $('#address').val() }, function() { $('#address').val(''); });`
Nick Craver
@steve, just as Nick Craver said. I added it in my example for clarity.
Tatu Ulmanen
thanks, one last question...when i move my ajaxServer.php to a child directory (/email) and i change path .load('ajaxServer.php..... to .load('email/ajaxServer.php...i get error... do i need to change more?
steve
just to clearify..... if i remove the script from page, the data will submit but with page refresh ofcourse...i.e i have set all other paths correct once i move into child directory, i think i need some change in this java script only. thanks
steve
oh sorry my mistake...working all fine nowgreat support thanks a lot Tatu Ulmanen, Nick Craver and John Himmelman for your precious time
steve
A: 

Give this a whirl..

function init()
{
    $('#addressForm').submit(storeAddress);
}

function storeAddress(e)
{
    $('#response').html('Adding address...');

    var params = 'address=' + $('#address').val();

    $('#response').load('ajaxServer.php', params);

    $.ajax({
        url : 'index.php',
        data : $('#addressForm').serialize(),
        success : function(data)
        {
            alert('yay, form data sent!');
        }
    });

    // Fix - stops page reload
    return false;
}

$(document).ready(init);
John Himmelman
i was trying that page wont reload, although data is submitted but page is reloading aswell
steve
Try now, it sends the form data ajaxically and stops the form from submitting (by returning false).
John Himmelman
oh yes it works sweethow will i remove the alert window that pops up. i dont need that, also is this submitting data twice, emails are appearing in pairs with single submit.thanks
steve
Why two AJAX requests?
Tatu Ulmanen
Tatu: The first request fetches the status, the second submits the form data (form action is index.php).
John Himmelman
+2  A: 

You can convert the script like this:

$(function() {
  $('#addressForm').submit(function(e) {
    $('#response').html('Adding email address...');
    $.ajax({
        url: 'ajaxServer.php',
        type: 'GET',
        data: {'address': $('#address').val() },
        success: function(response) {
          $('#response').html(response);
        }
    });
    e.preventDefault();
    return false;
  });​
});

Here's a rundown of the equivalents:

  • .html() for the innerHTML call
  • $('#id') selector for finding by an ID
  • $.ajax for the ajax call
    • data is the passed parameters, set the pair up (address=...)
    • success callback runs when the request completes
      • put the response into the id="response" div here
Nick Craver
although data is submitted but page is reloading aswell
steve
@steve - Updated, this should prevent the default submit from happening now.
Nick Craver
You would be better off observing the submit event on the form rather then the click event on the submit button. Looks like that's what the prototype code was doing too.
PetersenDidIt
@ Nick Craver thanks for you time looking into this, actually i am trying to submit form with out page refresh...the updated version isnt doing that still..is there i am missing? somr thing you need to know from my side? do let me know
steve
@petersendidit can you kindly let me know what to chnage...i am a mess with codes :( and i need this one working soon,,,thanks
steve
@steve, @petersendidit - Updated one more time, I'm basing this off you using jQuery 1.4 and the submit handler normalized (even in IE). Try the updated answer again, if there are still problems, it's most likely a javascript error being thrown, and a link to a page I can see would be helpful.
Nick Craver
still problem :( i actually dont have a page set for web preview, i am testing this on my local server, i will however provide you one link in few hoursthanks for your timei am grateful
steve
@steve - Something I take for granted most times, were you wrapping this in a `$(function() { });` like I added above? If not please do.
Nick Craver
i am actually copying your whole code.pasting it in a java file that i call in page.
steve
@steve - Then do that once more, should work, sorry I forgot the obvious.
Nick Craver
hi, sorry but still no luck, data submits but still with page refresh.
steve