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 = "stephan@mail.co.za";

$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);

?>
Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
Stephan Botes
  • 87
  • 1
  • 2
  • 10

2 Answers2

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.

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
  • 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 .. – alwaysLearn Mar 29 '13 at 08:41
  • @StephanBotes its ok mate .. it happens sometimes .. good to help you :) – alwaysLearn Mar 29 '13 at 08:57
0

you can use

$('selector').load() 

OR

$.ajax();

$.ajax(); is most preferable.

Albzi
  • 15,431
  • 6
  • 46
  • 63
Nimesh Vagadiya
  • 602
  • 3
  • 8
  • 18