0

I've been searching for an answer to this for several days now, but if I missed the answer in another post, let me know.

I'm trying to get into Ajax, so I have a very simple form in my index.php, with separate php and javascript files:

index.php

<div id="ajax-test">
    <form action="ajax/ajax.php" method="POST">
        <textarea name="someText" id="some-text" placeholder="Type something here"></textarea>
            <button type="button" onclick="loadDoc()">Submit</button>
    </form>
    <div id="ajax-text"></div>
</div>

main.js:

function getXMLHttpRequestObject() {
    var temp = null;

    if(window.XMLHttpRequest)
        temp = new XMLHttpRequest();
    else if(window.ActiveXObject) // used for older versions of IE
        temp = new ActiveXObject('MSXML2.XMLHTTP.3.0');

    return temp;
}// end getXMLHttpRequestObject()


function loadDoc() {
    var ajax = getXMLHttpRequestObject();

    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            document.getElementById('ajax-text').innerHTML = ajax.responseText;
            console.log(ajax.responseText);
        }
    };

    ajax.open("POST", "ajax/ajax.php", true);
    ajax.send();
}

ajax.php:

<?php

print_r('\'' . $_POST['someText'] . '\' is what you wrote');

?>

Whenever I try to print, it prints: " '' is what you wrote " - what am I missing/not doing/doing incorrectly that isn't allowing me to access the content of someText? I've changed my naming convention, swapped from single quote to double quote, tried GET instead of POST, but nothing worked.

4
  • 2
    Don't you need to set the data when calling send()? Commented Jul 1, 2016 at 17:02
  • Have you watched the AJAX request / response in the browser's developer tools? Are there any errors reported? Are you running this on a web-server? Commented Jul 1, 2016 at 17:03
  • Along with what @FirstOne said you'll need to capture the data to be sent when you call loadDoc() Commented Jul 1, 2016 at 17:06
  • I'm just about brand new to this, so you may need to use layman's terms - I didn't see an error when I looked in the dev tools @JayBlanchard, and yes I'm running this on a web-server. I"m not sure how to capture the data, and what would I include in the send() @FirstOne? Commented Jul 1, 2016 at 18:37

2 Answers 2

2

You can try to set a header request and also put the data inside the send. Here an example as like as-

ajax.open("POST", "ajax/ajax.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send("someText="+document.getElementById('some-text').value);
Sign up to request clarification or add additional context in comments.

1 Comment

This worked! It's generated a p tag below my ajax-text div with "someText" - can you help me understand why that's being generated?
1

This is probably beacuse of the error

Undefined index: someText in C:\xampp\htdocs\ssms\sandbox\ajax\ajax.php on line 3

You had a couple of issues with your code which i don't have time to list out now. This should work fine, plus i used the onkeyup() function to display the text live without even clicking on the submit button.

The Index File

<div id="ajax-test">
<form method="POST" onsubmit="return false;">
    <textarea onkeyup="loadDoc()" name="someText" id="someText" placeholder="Type something here"></textarea>
        <button type="button" onclick="loadDoc()">Submit</button>
</form>
<div id="ajax-text"></div>
</div>
<script type="text/javascript" src="main.js"></script>

The Main Javascript file

function _(x) {
   return document.getElementById(x);
 }
function ajaxObj ( meth, url ) {
   var x = new XMLHttpRequest();
   x.open( meth, url, true );
   x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   return x;
}
function ajaxReturn(x){
   if(x.readyState == 4 && x.status == 200) {
      return true;
   }
}
function loadDoc() {
    var someText = _("someText").value;
    var ajax = ajaxObj("POST", "ajax/ajax.php");

    ajax.onreadystatechange = function() {
       if(ajaxReturn(ajax) == true) {
          _('ajax-text').innerHTML = ajax.responseText;
          console.log(ajax.responseText);
       }
    }
  ajax.send("someText="+someText);
 }

The PHP AJAX File

if(isset($_POST['someText'])){
  $someText = $_POST['someText'];
  echo "\"$someText\"" . ' is what you wrote';
  exit();
} else {
  echo "An error occured";
  exit();
}

1 Comment

@DrewAdams Your Main.js file wasn't sending a proper request and your Main.js code was a bit messy, plus in your index.php file you set the id of your textarea to 'some-text' while you were looking for 'someText' in ajax.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.