title: AJAX to PHP not sending data to database using NODEJS
body: <p>I'm currently developing a chat project.. I'm using a php framework and have managed to run it on node now the problem I'm currently experiencing is that the ajax query is not working it does not send a single data to my database.. the script that I used is perfectly working because I used this script when I was still using a long-polling of ajax for a chat app... It just didnt work now when I used it on the new chat app using node that I was developing... Here is my index.php </p>
  
  <pre><code>&lt;?php startblock('script') ?&gt;
      &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
      &lt;script src="http://code.jquery.com/jquery-1.11.1.js"&gt;&lt;/script&gt;
      &lt;script&gt;
          var socket = io();
          $('form').submit(function(){
              socket.emit('chat message', $('#m').val());
              $('#m').val('');
              return false;
          });
          socket.on('chat message', function(msg){
              var data2 = { text: msg };
  
              $.ajax({
                  url: 'localhost:3000/includes/message/store_chat.php',
                  dataType: 'json',
                  type: 'POST',
                  data: {json:JSON.stringify(data2)},
                  success: function (data2) {  }
              });
          });
      &lt;/script&gt;
  
      &lt;script&gt;
          jQuery(function ($) {
              $(window).on("resize", function () {
                  body      = $("html,body"),
                  menu      = $("#side-menu").width(),
                  gridW     = body.width() - (menu + 30),
                  gridH     = body.height();
  
                  $("#message-app-wrapper").css("height", gridH);
                  $("#views-wrapper").css("width", gridW);
              }).resize();
          });
      &lt;/script&gt;
  &lt;?php endblock(); ?&gt;
  </code></pre>
  
  <p>And this is the database handler</p>
  
  <pre><code>&lt;?php
  
  //Send some headers to keep the user's browser from caching the response.
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
  header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
  header("Cache-Control: no-cache, must-revalidate" ); 
  header("Pragma: no-cache" );
  header("Content-Type: text/plain; charset=utf-8");
  
  $json2 = $_POST['json'];
  $data = json_decode($json2);
  
  $text = $data-&gt;text;
  
  $con = new PDO("mysql:host=localhost:3000;dbname=schat", "root" , "");
  $con-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  
  $sql2 = "INSERT INTO chat_storage(chat) VALUES(:msg)";
  $stmt2 = $con-&gt;prepare($sql2);
  $stmt2-&gt;bindValue( 'msg',$text, PDO::PARAM_STR);
  $stmt2-&gt;execute();
  ?&gt;
  </code></pre>
  
  <p>The index.js is here:</p>
  
  <pre><code>var app = require('express')();
  var http = require('http').Server(app);
  var io = require('socket.io')(http);
  var validator;
  
  function getStdout(command, args, fn) {
      var childProcess = require('child_process').spawn(command, args);
      var output = '';
      childProcess.stdout.setEncoding('utf8');
      childProcess.stdout.on('data', function(data) {
          output += data;
      });
      childProcess.on('close', function() {
          fn(output);
      });
  }
  
  app.use('/assets', require('express').static(__dirname + '/assets'));
  app.use('/temp', require('express').static(__dirname + '/temp'));
  app.use('/includes/message', require('express').static(__dirname + '/includes/message'));
  app.get('/', function(req, res) {
      //res.sendfile(__dirname + '/' +validator);
      res.send(validator);
  });
  
  //you should have only one io.on('connection')
  io.on('connection', function(socket) {
      socket.on('chat message', function(msg){
          console.log('message: ' + msg);
          io.emit('chat message', msg);
      });
  });
  
  getStdout('php', ['index.php'], function(output) {
      validator = output;
      //start your server after you get an output
      http.listen(3000, function() {
          console.log(validator);
      });
  });
  </code></pre>
  
  <p>These are what I have so far. For some reason it wont' store to my database I don't know if I did something wrong here or have missed to add something.</p>