简体   繁体   中英

can't get value pass in ajax post using slim framwork

I am trying to write simple application using slim but I can't get value in ajax post using slim always it says null value and 500 server error. This is what I tried to do. How can I pass data using ajax and get that data. What is wrong with this code. Please help me

my code(jquery)

$('document').ready(function(){
        $('input').change(function(){
         var nama=$(this).val();
            var supplier={'name':name};
            var rootURL ='http://localhost/manju/Slim/';
         alert($(this).val());
            $.ajax({
                        type: 'POST',
                        url: rootURL +'supplier',
                        dataType: 'json',
                        data:supplier,
                        contentType: "application/json; charset=utf-8",
                        success: function(response){
                            alert(response);

                        },
                        error: function(){
                           alert('error');
                        }
                    });
        });
    });  

server side (slim)
ini_set('display_errors', 1); 
error_reporting(E_ALL);
require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=manju;host=127.0.0.1';
$user = 'root';
$password = '';

try {
    $db = new PDO($dsn, $user, $password);

} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$app->post('/supplier',function () use($app,$db) {
      $supplier=$app->request();
      $paramName=$supplier->params('name');
      $query='INSERT INTO name (`name`) VALUES ("'.$paramName.'")';
      $insert=$db->query($query);
      echo json_encode($paramName);


});


$app->run();

Use below code

$results = json_decode(file_get_contents('php://input'), true);
$paramName = $supplier['name'];

instead of

$supplier=$app->request();
$paramName=$supplier->params('name');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM