1

I am trying to do an HTML Contact form that will send an email via MailGun

I am following the instructions here to do this using Ajax and PHP

I have no experience in PHP and want a simple way of debugging.

Thus I want to add popup messages to my send.php

I can get wwwroot\hello.php to work, so I know PHP is available.

<?php
echo("hello php");
?>

I have tried the following in send.php but I do not see any messages

<?php
 echo "Hello Send2";
if( isset($_POST) ){
  echo "In post.";
  phpAlert("in post2");
    $postData = $_POST;
    $mailgun = sendMailgun($postData);

    if($mailgun) {
    echo "Great success.";
  } else {
    echo "Mailgun did not connect properly.";
  }
}
 // etc

The Ajax call is

var dataString = '<p><strong>Name: </strong> '+ name + '</p><p><strong>Email: </strong> ' + email + '</p><p><strong>Message: </strong> ' + message + '</p>';

$.ajax({
    type: "POST",
    url: "script/send.php",
    data: { data: dataString, senderAddress: email },
    success: function() {
      alert("Got success");

I do get the javascript "Got success" message but I am trying to troubleshoot why send.php does not work.

I am thinking the first step would be to be able to add calls to echo but nothing shows.

send.php does contain calls to curl so I am guessing my next question may be about how to set that up in Azure, but first I would like to know why echo does not work from inside send.php

Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

1

You said "I don't see any messages". Since you report that the ajax call completes successfully, then this is simply because your ajax call doesn't print them out. Unlike calling a script directly, when you request via ajax it doesn't automatically echo the response to your browser window - after all, your code doesn't know where within your existing page to put that output, until you tell it. (It can't just place it arbitrarily somewhere, that could potentially wreck your page layout or just look stupid.)

The "success" callback provides a parameter which will contain the response from the server. You need to take that and show it somehow. Here's a very simple example, just showing it an alert box:

success: function(response) {
  alert(response);
}

response will contain anything which you echo in the send.php script.

See http://api.jquery.com/jquery.ajax/ to understand what parameters are provided by the various callbacks within $.ajax, and what they contain.

P.S. You can also always see the response to an ajax call, even if you don't use the response within your code, by simply opening your browser's developer tools, going to the Network tab, and looking for the ajax call and viewing the "Response" tab within that call.

ADyson
  • 57,178
  • 14
  • 51
  • 63