1

I have a form that needs to run a php script once the submit button is clicked, it needs to be ajax.

<form method="post" action="index.php" id="entryform" name="entryform">
<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform')" />
</form>

In this situation, using if(form posted) { get results and run script } is not an option, I need to run the script externally, that is why I need it to execute the email-notification.php at onclick

When I point my web browser to domain.com/include/email-notification.php - it runs perfectly.

Any help is appreciated.

This executes the script before you submit the form, now I need to wait to execute the script once the submit button is clicked, possible to do this?

$.ajax({
    type: "POST",
    url: "/include/email-notification.php",
    dataType: "script"
});
8
  • I guess you should point it to /include/email-notification.php in the form as well then? Commented Jun 3, 2010 at 15:06
  • Yeah, I was wondering the same thing as well. Brad, is the '/web/ee_web' part supposed to be there? Commented Jun 3, 2010 at 15:09
  • /web/ee_web/ is the absolute path Commented Jun 3, 2010 at 15:12
  • Sorry if I don't get it, but why can't you just include email-notification.php in the target of the form? Commented Jun 3, 2010 at 15:46
  • using this w/ ExpressionEngine and am using a native EE form, in this situation I can not achieve it that way. Commented Jun 3, 2010 at 15:53

3 Answers 3

3

Check out the $.ajax() function of the jQuery javascript library. It will make you life much much easier and is quite minimal as far as added size to your page (like 12kb or something like that).

Sign up to request clarification or add additional context in comments.

Comments

1

I suggest using jquery. because of its flexibility and conveinience

If you want to send email notification and then only post the data then do as following:

create a separate page to handle your email notification like email.php

// fill in headers and prepare content
$result = mail(............................);
if($result) { return 1; } else { return 0; }

Then on your form

$('#mysubmitbutton').click(function() {
      $.post(
           'email.php',
           { emailadd: '[email protected]', content: 'mycontent' }, //send parameters to email.php
           function(data) {
                //now check whether mail sending was successfull
                if(data==1) {
                     //message sending was successful so carry out further operation
                     //.................................
                }
                else { alert("Sorry, email notification was unsuccessfull"); }
           });
});

Comments

0

Try changing your "onclick" value:

<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform'); return false" />

That's assuming that the "xmlhttpPost()" is something that actually works on its own.

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.