Claudio Martinez
Claudio Martinez

Reputation: 295

PHP not catching data from Ajax Jquery

Im trying to pass data from using Ajax and PHP on server side. the php file is not catching the data sent through Ajax.

the code gets the values with jquery and make a long string called data

the jquery code looks like this:

var data = 'ppemail=' + $('#email').val()
    + '&monto_enviar=' + montoEnviarDisp
    + '&monto_pub=' + montoPubDisp
    + '&tasa=' + tasaDisp
    + '&monto_recibir=' + monto_recibirDisp
    + '&banco=' + $('#banco').val()
    + '&receptor=' + $('#receptor').val()
    + '&cuenta=' + $('#cuenta').val()
    + '&cedula=' + $('#cedula').val();

$.ajax({
    type: "POST",
    url: 'crear_oferta.php',
    data: ({data}),
    success: function (response) {
        alert(response);
    }
}); 

the php file is this:

<?php
session_start();
require_once 'dbconfig3.php';

var_dump($_POST);

try {
    $userID = $_SESSION['userSession'];

    $ppemail = $_POST['ppemail'];
    $monto_e = $_POST['monto_enviar'];
    $monto_p = $_POST['monto_pub'];
    $tasa = $_POST['tasa'];
    $monto_rec = $_POST['monto_recibir'];

    $banco = ($_POST['banco']);
    $receptor = ($_POST['receptor']);
    $cuenta = ($_POST['cuenta']);
    $cedula = ($_POST['cedula']);

    /// luego de confirmar hacer el try e insertar

    //if(isset($_POST['btnferta'])){

    //$password = md5($upass);
    $bid_date = date('Y-m-d H:i:s');

    $stmt = $db_con->prepare("INSERT INTO ofertas(uid,email_pp,nombre_receptor,banco_receptor,cuenta_receptor,cedula_receptor,monto_enviar,monto_publicar,tasa,monto_recibir,fecha) 
                            VALUES(:userid, :emailpp, :nombre, :banco,  :cuenta, :cedula, :monto_e, :monto_p, :tasa, :monto_r, :fecha)");

    $stmt->bindParam(":userid", $userID);
    $stmt->bindParam(":emailpp", $ppemail);
    $stmt->bindParam(":nombre", $receptor);
    $stmt->bindParam(":banco", $banco);
    $stmt->bindParam(":cuenta", $cuenta);
    $stmt->bindParam(":cedula", $cedula);
    $stmt->bindParam(":monto_e", $monto_e);
    $stmt->bindParam(":monto_p", $monto_p);
    $stmt->bindParam(":tasa", $tasa);
    $stmt->bindParam(":monto_r", $monto_rec);
    $stmt->bindParam(":fecha", $bid_date);

    $stmt->execute();

    echo 'ok';


} catch (PDOException $ex) {
    echo $ex->getMessage();
}

?>

why the $_POST is not getting any data? Thanks for the help!

Upvotes: 0

Views: 65

Answers (2)

Barmar
Barmar

Reputation: 782683

You should set data to an object. This ensures that the URL parameters will be properly encoded; otherwise, you need to call encodeURIComponent on any parameter that could contain special characters.

var data = {
    'ppemail': $('#email').val(),
    'monto_enviar': montoEnviarDisp,
    'monto_pub': montoPubDisp,
    'tasa': tasaDisp,
    'monto_recibir': monto_recibirDisp,
    'banco': $('#banco').val(),
    'receptor': $('#receptor').val(),
    'cuenta': $('#cuenta').val(),
    'cedula': $('#cedula').val()
};

Then you shouldn't wrap it in another object when calling $.ajax:

$.ajax({
    type: "POST",
    url: 'crear_oferta.php',
    data: data,
    success: function(response) {
        alert(response);
    }
}); 

Upvotes: 1

fubar
fubar

Reputation: 17398

It looks like you're trying to pass a string as an object property. Change your $.ajax options:

$.ajax({
  type: "POST",
  url: 'crear_oferta.php',
  data: data,
  success: function(response) {
    alert(response);
  }
}); 

Upvotes: 1

Related Questions