Addis16
Addis16

Reputation: 31

Trouble with AJAX JQuery

can someone take a look at what I have here? Either my JS variable is not sending to my php file or the php file isnt writing to my log. I'm not sure how to do a php alert either to find out. I've looked this up quite a bit but I'm still at a loss. I tried doing the alert with:

echo "<script type='text/javascript'>alert('$alert');</script>";

But It nothing happens. Here is the JS:

var english = $('#english').val();
$.post("grammar.php", {text: english});

Here is the php:

$english = $_POST['english'];

$open = fopen("log.html", 'a');
fwrite($open, "<div>".stripslashes(htmlspecialchars($english))."</div>\n");
fclose($open);

And I load log.html into a div:

function load() {
   $.ajax({
      url: "log.html",
      cache: false,
      success: function(html) {
          $("#log-container").html(html);
      };
   });
};
setInterval(load, 1000);

The text-field is saving into the JS variable, I tested that with browser alerts. The div does post to log.html but when it posts its empty and just appears in the log as:

<div></div>

Here is the whole project, it's not done yet but if anyone wants to run it on their server. thanks. https://jsfiddle.net/6cwcjxhj/

Upvotes: 1

Views: 47

Answers (1)

Andrei
Andrei

Reputation: 56688

Your request param name seems wrong. Sure, the value that is sent is stored in the variable called english, but that is not what the parameter is called. You are sending this as text, so this is what you should be looking for:

$english = $_POST['text'];

Upvotes: 2

Related Questions