php - Issue with retrieving a parameter from a $.ajax call -
hey guys have next $.ajax call:
$.ajax({ type: "post", datatype: "json", url: '/pcg/popups/getnotes.php', data: { 'namenotes': notes_name.text(), }, success: function(response) { $('#notes_body').text(response.the_notes); alert(response.the_notes); } )};
now focus on data:
lets sent 'billcosby'. sent on file specified , have within file:
$username_notes = $_post['namenotes'];
now lets have $username_notes homecoming in json this...
$returnarray = array( 'the_notes' => $username_notes ); echo json_encode($returnarray);
this work , response billcosby. out of way, when seek value billcosby mysql database using pdo homecoming null....before show code whole file want create clear pdo works perfect, if give variable $username_notes direct value of 'billcosby' run through database perfect, returns null if have $_post['namenotes']; in front.
getnotes.php:
$username_notes = $_post['namenotes']; grabnotes($username_notes); function grabnotes($xusername) { ..... $newuser = $xusername; seek { # mysql pdo_mysql $dbh = new pdo("mysql:host=$hostname;dbname=$database", $username, $password); $dbh->setattribute( pdo::attr_errmode, pdo::errmode_exception ); } catch(pdoexception $e) { echo "i'm sorry, i'm afraid can't that."; file_put_contents('pdoerrors.txt', $e->getmessage(), file_append); } $sql = "select notes csvdata username = :username"; $getmeminfo = $dbh->prepare($sql); $getmeminfo->execute(array(':username' => $newuser)); $row = $getmeminfo->fetch(pdo::fetch_assoc); $notes = $row['notes']; $returnarray = array( 'the_notes' => $row['notes'],); echo json_encode($returnarray); $dbh = null; }
so question is, why can not homecoming value pdo statement? work perfect if told pdo statement name - 'billcosby' within file $.ajax file calls to. not work , returns null if value through $_post $.ajax. funny thing can homecoming same value sent in.
thanks time guys!
try trimming :
$username_notes = trim( $_post['namenotes'] );
sometimes spaces in string cause sort of error, , can hard spot.
php jquery pdo
No comments:
Post a Comment