0

I want to send some data to php to change something in my MySQL database, but I keep getting this 405 error 'POST method not allowed'. I googled a lot, but can't find any useful solution. Can someone explain to me what I'm doing wrong?

I'm using Razor inside HTML to render the data on the webpage:

@foreach(var row in db.Query(getKamers))
     {            
        <form id="@row.id" action='_DataConn.php' method='post' class='ajaxform'>                    
            <input type="text" value="@row.id" name="id" id="td-id" />
            <input type="text" value="@row.oppervlakte" name="oppervlakte" id="td-opp" />
            <input type="text" value="@row.locatie" name="locatie" id="td-loc" />
            <input type="text" value="@row.type" name="type" id="td-type" />
            <input type="text" value="@row.kamernr" name="nummer" id="td-kamernr" />
            <input type="text" value="@row.vrij" name="vrij" id="td-vrij" />
            <input type="submit" value="opslaan" name="opslaan" id="@row.id" />                
        </form>              
     }

then I have this JavaScript file:

$(document).ready(function () {
$('.ajaxform').submit(function () {
    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method'),
        dataType: 'json',
        data: $(this).serialize(),
        success: function (data) {
            console.log(data);
        }
    });

    return false;
});

});

and my PHP looks like this:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "Studentenkamers";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String


$id = $_POST['id'];
$oppervlakte = $_POST['oppervlakte'];
$locatie = $_POST['locatie'];
$kamernr = $_POST['nummer'];
$type = $_POST['type'];
$vrij = $_POST['vrij'];
echo 'ok'
// Escape User Input to help prevent SQL Injection

$id = mysql_real_escape_string($id);
$oppervlakte = mysql_real_escape_string($oppervlakte);
$locatie = mysql_real_escape_string($locatie);
$kamernr = mysql_real_escape_string($kamernr);
$type = mysql_real_escape_string($type);
$vrij = mysql_real_escape_string($vrij);
//build query
//"UPDATE Studentkamer SET oppervlakte='" + room[1] + "', locatie='" + room[2] + "', type='" + room[3] + "', vrij='" + room[4] + "' WHERE id='" + room[0] + "'";
$query = "UPDATE Studentkamer SET oppervlakte = '$id', locatie = '$locatie', type='$type', kamernr   = '$kamernr', vrij = '$vrij' WHERE id='$id'";

//Execute query
 $qry_result = mysql_query($query) or die(mysql_error());
 ?>

EDIT:

Below is the link to a screenshot of the error info, does it has something to do with the static context? http://img41.imageshack.us/img41/9139/errorde.jpg

I am working in Webmatrix using localhost, do I need to put this website online for it to work?

CharlesV
  • 31
  • 1
  • 7

2 Answers2

1

Look at the submit button and form id. Does the razor translate that into a valid string? It looks to me as if the ids contain @. The @ in the id values is not permitted according to http://www.w3.org/TR/html4/types.html. This might be passed as values containing those characters

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number
 of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Try

@(row.id)

Reference - MVC3 Razor syntax. How do I convert this ASP.NET style to Razor. (also issues with @ within quotes)

Community
  • 1
  • 1
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
  • ok, thanks! I changed it but it still gives me the same error. I guess it has something to do with the POST method that isn't allowed on my webserver, but I'm waiting for an answer how to do that. I'm running localhost and using Webmatrix. – CharlesV Sep 14 '12 at 13:41
  • But at the same time, I highly doubt this has anything to do with the error they're getting. Good point though! – Ian Sep 14 '12 at 13:41
0

You have to allow POST method in config for your web-server

Nick Pavluk
  • 379
  • 2
  • 7
  • thanks for the answer! I am using Webmatrix and this website is running on localhost, how do I allow the POST method? I Already put " " in my web.config – CharlesV Sep 14 '12 at 13:37
  • Maybe these links help you: http://support.microsoft.com/kb/819267 or http://stackoverflow.com/questions/3472429/how-do-i-configure-iis-to-accept-post-requests – Nick Pavluk Sep 14 '12 at 13:45
  • I already allowed it in the web.config, but no change. This is the info the Response Header contains in chrome inspector: HTTP/1.1 405 Method Not Allowed Cache-Control: private Allow: GET, HEAD, OPTIONS, TRACE Content-Type: text/html; charset=utf-8 Server: Microsoft-IIS/7.5 X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQ2hhcmxlc1xEb2N1bWVudHNcTXkgV2ViIFNpdGVzXFN0dWRlbnRlbmthbWVyc1xfRGF0YUNvbm4ucGhw?= X-Powered-By: ASP.NET Date: Fri, 14 Sep 2012 13:56:14 GMT Content-Length: 5444 – CharlesV Sep 14 '12 at 13:57
  • Maybe you have different configs for whole server and for your current project? – Nick Pavluk Sep 17 '12 at 08:13