javascript - How to make sure responseText does not return Oracle errors when something cannot be inserted into databse -
so i've got website, bunch of stuff sent through javascript php, if goes well, php returns response says "user created succesfully" if doesn't should return, "user not created" or "username exists", doesn't , instead throws bunch of oracle errors. there anyway make sure php doesn't send that, , sends 1 of 2 strings?
this javascript code:
window.onload = function findsubmitbutton() { var button = document.queryselector(".send_info").addeventlistener("click", serverinteraction); /*unobtrusive javascript listener, added create account button in html*/ } function serverinteraction() { var xmlhttp; var inputarray; var finalarray = []; var jsonarray; var userid; if (window.xmlhttprequest){ xmlhttp = new xmlhttprequest(); /* used ie7+,firefox, opera, chrome, safari */ } else if (window.activexobject) { xmlhttp = new activexobject("microsoft.xmlhttp"); /* compatibility ie6 browsers */ } else { throw new error("your browser not compatible xmlhttp"); return false; } /* following section validates if required inputs have text in them , if password fields match*/ inputarray = document.queryselectorall("input[type=text]") if (inputarray[1].value != inputarray[2].value){ alert("password doesn't match, please make sure password matches") return false; } for(var = 0; < inputarray.length; i++){ if (inputarray[i].value == ""){ alert("please fill out of fields"); return false; } finalarray[i] = inputarray[i].value; } console.log(finalarray); jsonarray = json.stringify({finalarray: finalarray}); console.log(jsonarray); xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200){ if (confirm(xmlhttp.responsetext)){ window.location = 'index.html' } } } xmlhttp.open("post","php/senduserinfo.php", true); xmlhttp.setrequestheader("content-type","application/json;charset=utf-8"); xmlhttp.send(jsonarray); }
here's php, 1 causing problems:
<?php $db_connection = oci_connect('user', 'password', 'localhost/database'); /*used establish connection between database , server*/ $finalarray = file_get_contents('php://input'); /*data retrieval*/ $finalarray = json_decode($finalarray,true); /*json decoding*/ $error; $username = $finalarray['finalarray'][0]; /* these variables data sent database */ $password = $finalarray['finalarray'][1]; $email = $finalarray['finalarray'][3]; $name = $finalarray['finalarray'][4]; $firstlastname = $finalarray['finalarray'][5]; $secondlastname = $finalarray['finalarray'][6]; $sqlvariableusuariotable = 'call usuario_package.add_usuario(:username, :password)'; /* sql statement send */ $sqlvariablepersontable = 'call person_package.add_person(:name, :first_ln, :second_ln, :username_person)'; $sqlvariableemailtable = 'call email_package.add_email(:email)'; $datatoinsertusuario = oci_parse($db_connection, $sqlvariableusuariotable); /* prepares statements sent database */ $datatoinsertperson = oci_parse($db_connection, $sqlvariablepersontable); $datatoinsertemail= oci_parse($db_connection, $sqlvariableemailtable); if(!$db_connection){ /* checks if connection database works */ exit ("server not connect database"); } oci_bind_by_name($datatoinsertusuario, ':username', $username); /* binds variables string names */ oci_bind_by_name($datatoinsertusuario, ':password', $password); oci_bind_by_name($datatoinsertemail, ':email', $email); oci_bind_by_name($datatoinsertperson, ':name', $name); oci_bind_by_name($datatoinsertperson, ':first_ln', $firstlastname); oci_bind_by_name($datatoinsertperson, ':second_ln', $secondlastname); oci_bind_by_name($datatoinsertperson, ':username_person', $username); $arrayofdatatosend[0] = $datatoinsertusuario; $arrayofdatatosend[1] = $datatoinsertemail; $arrayofdatatosend[2] = $datatoinsertperson; foreach ($arrayofdatatosend $value){ oci_execute($value); if(oci_error($value)['code'] == 00001){ oci_rollback($db_connection); exit ("the username or email have entered exists"); } else if (oci_error() != false){ oci_rollback($db_connection); exit ("an error has occured database"); } } exit ("account succesfully created"); oci_close($db_connection); ?>
i know possible filter, or check user doesn't exist before inserting data, either way i'm sure there's simpler solution. i'd appreciate if me out that. i'm new @ this.
you need use try/catch block around code in php
foreach ($arrayofdatatosend $value){ try { oci_execute($value); exit ("account created"); } catch { exit ("an error has occured database"); } }
Comments
Post a Comment