0

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!

2 Answers 2

1

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);
    }
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

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);
  }
}); 

3 Comments

For cache may be you can use the parameter cache: true, in $.ajax function. (Y)
@Harish POSTs are never cached
@fubar Thank you so much! That worked, it was a silly error but I hope it helps others as well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.