php - INSERT INTO mySQL from client side with AJAX -
i want insert data client side remote mysql database
i calling function , passing variable it.
function uploadmetrics(email){ var email; $.ajax({ type: 'post', url: 'php/insertconvertdata.php', data: { data: {"email" : email }, //data:email, }, success: function(result) { console.log(result); } }); }
on server have php file
$user = $_post['email']; echo $user; echo '<pre>'; print_r($_post); // viewing array var_dump($_post); // viewing info of array echo '</pre>'; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } $sql = "insert cms_conversion_funnel (email) values ('" . $user . "')"; if ($conn->query($sql) === true) { echo "new record created successfully"; } else { echo "error: " . $sql . "<br>" . $conn->error; } $conn->close();
the code runs fine , can connect database , run insert in inserts empty string.
i dumped out value of 'email' console , got this:
( [data] => array ( [email] => teretst@gmail.com ) ) array(1) { ["data"]=> array(1) { ["email"]=> string(17) "teretst@gmail.com" } }
it seems there value in email variable not being picked passed insert statement. can help, new php , ajax.
because it's $_post['data']['email']
, not $_post['email']
.
Comments
Post a Comment