javascript - Jquery Ajax Inserting data into a database -
javascript - Jquery Ajax Inserting data into a database -
i have page 3 forms, , on these forms seperate variables. page allows user come in details , inserted mysqldatabase viewing. have script:
edit: know mysql_ deprecated sake of illustration it's working fine.
edit 2: know can inject that's pretty irrelevant @ moment, think it's problem using text area instead of simple input.
edit 3: it's typo (fuck sake). it's been long day
$("#finishbutton").click(function(e) { // store final value , execute script insert db. on success, switch success page var commentsvalid = $('#commentsdetailsform').valid(); if (commentsvalid) { comments = document.getelementbyid('commentsinput').value; e.preventdefault(); $.ajax({ type: "post", url: 'insert.php', data: 'forenameinput=' + forename + '&surnameinput=' + surname + '&emailinput=' + email + '&telephoneinput=' + telephone + '&genderinput=' + gender + '&dobinput=' + dob + '&commentinput=' + comments, success: function (data) { if (data == "error") { $("#error").show(); $("#processing").hide(); } else { window.location.href = "success.php"; } } }); } else { } });
that meant store details database. things stand, stores details in database except comments (final variable). finishing info statement wrong there else fundamentally wrong?
php script:
<?php // connection details $servername = "localhost"; $username = "root"; $password = "user10"; $dbname = "test"; // create connection $conn = mysql_connect($servername, $username, $password); // check connection if (!$conn) { die("connection failed: " . mysql_connect_error()); } // select database mysql_select_db($dbname,$conn); // store posted info in variables $forename = $_post['forenameinput']; $surname = $_post['surnameinput']; $email= $_post['emailinput']; $telephone = $_post['telephoneinput']; $dob = $_post['dobinput']; $gender = $_post['genderinput']; $comments = $_post['commentsinput']; //change date of birth it's storable in mysql database $dobalt = date('y-m-d',strtotime($dob)); // insert form info database $sqlquery = "insert test (firstname, lastname, email, telephone, gender, dob, comments) values ('$forename','$surname','$email','$telephone','$gender','$dobalt', '$comments')"; // check if query worked if (mysql_query($sqlquery, $conn)) { } else { echo "error: " . $sql . "<br>" . mysql_error($conn); } // close db mysql_close(); ?>
html:
<form id = "commentsdetailsform" name = "commentsdetailsform" method = "post"> <label "commentsinput" id = "labels"> comments </label> <br> <textarea id = "commentsinput" rows= "2" name = "commentsinput" class = "input-block-level"></textarea> <br> <div id = "registrationbuttonwrapper"> <button id = "finishbutton" class = "insertdetailsfinal" name = "finish"> finish > </button> </div> </form>
you can see running @ http://chriswaszczuk.me/jobtest/ (you'll have fill in form see database).
you've got typo all.
js
'&commentinput=' + comments
- commentinput - singular
php
$comments = $_post['commentsinput'];
- commentsinput - plural
javascript php jquery mysql ajax
Comments
Post a Comment