4

I have a Javascript file that submits a form and loads an alert etc. Now I want to add functionality to call or get a .php file, I tried using $.get("mail.php"); but it doesn't seem to work.

Javascript Code:

$('#myform').on('submit', function(e){
    e.preventDefault();
    $.get($(this).attr('action') + $(this).serialize());
    alert("Thank You! \nTop Up for: <?php echo "$username" ?> Purchased Successfully");
    $.get("mail.php");  //this is the added part to get mail.php//
    location.reload();

PHP - mail.php:

<?php

$to = "[email protected]";

$headers = "From: website mail \r\n";

$email_body = "has just toped up his account.\n".

$email_subject = "User Topup Alert";

mail($to,$email_subject,$email_body,$headers);

?>
5
  • can you please post you php file also Commented Mar 29, 2013 at 7:46
  • What do you expect $.get("mail.php") to do? Do you want it to display something on the screen, or just send an email? (or something else?) Commented Mar 29, 2013 at 7:48
  • just send mail , the mail.php works 100% when loading it in webpage Commented Mar 29, 2013 at 7:48
  • follow this link stackoverflow.com/questions/1960240/jquery-ajax-submit-form Commented Mar 29, 2013 at 8:01
  • forms action is a url that the javascript loads but does not view it Commented Mar 29, 2013 at 8:16

2 Answers 2

9

I think you can try load():

$("#mailDiv").load('mail.php');

According to me $.get and $.post are to send the data. You cannot include files using this.

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

2 Comments

whats the issue .. ? it will load the content of mail.php in html element whose id is mailDiv.. I hope the extension of file where you have added the js code has php extension ..
@StephanBotes its ok mate .. it happens sometimes .. good to help you :)
0

you can use

$('selector').load() 

OR

$.ajax();

$.ajax(); is most preferable.

1 Comment

how to use $.ajax() in php ?

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.