0

Been a while since I asked a qustion about uncharted waters again (Ajax with JQuery). I've been doing a little website for a local charity group. On the website I have a small form were people can ask for help. But I am stuck. Probably staring at the error but can't see it. ;)

ajax.js

function submitApplication()
{
    $.ajax({
        url: 'db/submitApplication.inc.php',
        type:'POST',
        dataType: 'json',
        data: { name: $("#name").val() },
        success: function(output_string){  },
        error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }
    });
}

submitApplication.inc.php

<?php
include('../inc/conn.inc.php');

$query = "INSERT INTO ljusimorker_applications (ljusimorker_applications_name) VALUES ('" . $_POST['name'] . "')"; 
$writeData = mysql_query($query) or die(mysql_error());

mysql_close();
?>

I know that the mysql statements are of the deprected variant but it's only for testing the script. I will add the security aspect later before putting the website live. The HTML I'm fetching is below.

HTML

<div class="input-text">Namn (visas ej):</div></td><td><input type="text" id="name" class="input-box">

The error I get is:

Chrome: SyntaxError: Unexpected end of input

Firefox: SyntaxError: JSON.parse: unexpected end of data

Firebug says this under NET -> XHR

Response Headers
Connection  Keep-Alive
Content-Length  0
Content-Type    text/html
Date    Thu, 15 Aug 2013 11:19:14 GMT
Keep-Alive  timeout=3, max=100
Server  Apache/2.2.24 (FreeBSD) PHP/5.4.15 mod_ssl/2.2.24 OpenSSL/0.9.8y
X-Powered-By    PHP/5.4.15

Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language sv-SE,sv;q=0.8,en-US;q=0.5,en;q=0.3
Cache-Control   no-cache
Connection  keep-alive
Content-Length  23
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Host    xxx.xxx.xxx
Pragma  no-cache
Referer http://xxx.xxx.xxx/
User-Agent  Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
X-Requested-With    XMLHttpRequest

Thanks in advance. :)

Nicklas
  • 57
  • 1
  • 6
  • 4
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 15 '13 at 11:48
  • 3
    "I know that the mysql statements are of the deprected variant but it's only for testing the script. I will add the security aspect later before putting the website live." — Why write code that you know you will have to throw away? – Quentin Aug 15 '13 at 11:49
  • 1
    As I said in the question. I know it's absolete but only for testing. I will probably use mysqli or pdo when the client-side script works. – Nicklas Aug 15 '13 at 11:50
  • I wanted to get the Ajax part done as I've always got stuck on that in the past. It's going to be the only script on the site. And for me at the moment the old mysql style api is easier to read. – Nicklas Aug 15 '13 at 11:54

2 Answers2

3

Your PHP makes an SQL query and then never outputs anything back to the browser. If you want to parse some JSON then you need to send some JSON.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks, it's working now. As I said in the question I was probably looking at the error but did not see it. When I read your answer my first thought was "Duh... Why did i not see that...". – Nicklas Aug 15 '13 at 12:06
1

Your data type is 'json' but your dont return anything.

http://api.jquery.com/jQuery.ajax/

John
  • 1,095
  • 3
  • 15
  • 31