So Ive this code which accepts input from user into a form and updates the SQL database when submit is clicked (without refreshing the page). The problem is that it isnt updating/sending the values to sql table.
I've tried to follow examples and tutorials I've found on several sites, but I can't get any of them to work.
I have the form(index.html) and the php file(upd.php) for db connection and update . The code for it is below.
I'm trying to save these values to the database
table named messages
:-
- timestamp(tstamp) : Is a dynamic value, which is the timestamp of the video file being played in the browser( for example 1.935771),
- message(any comments)
- checkbox values
index.html
<body>
<h1>VIDO LABELLING TOOL</h1>
<video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="640" height="268"
data-setup='{ "playbackRates": [0.5, 1, 1.5, 2, 4] }'>
<source src="project.m4v" type='video/mp4'>
<track src='br.srt' kind="subtitles" srclang="en" label="English" default>
</video>
<script>
// Get the audio element with id="my_video_1"
var aud = document.getElementById("my_video_1");
// Assign an ontimeupdate event to the audio element, and execute a function if the current playback position has changed
aud.ontimeupdate = function() {myFunction()};
</script>
<div class="container" style="max-width:800px;margin:0 auto;margin-top:50px;">
<form name="contact-form" action="" method="post" id="contact-form">
<label for="email">Comments about the frame</label>
<textarea name="message" class="form-control" id="message"></textarea>
<div class="error" id="error_message"></div>
<label>Vehicle Type:</label>
<input name="veh_type_1" id="veh_type_1" type="checkbox" value="lmv">lmv
<input name="veh_type_2" id="veh_type_2" type="checkbox" value="2w">2w
<p>TimeStamp: <span id="tstamp"></span></p>
</div>
<p class="submit">
<button type="submit" class="btn btn-primary" name="submit" value="Submit" id="submit_form">Submit</button>
</p>
<div class="display-content">
<div class="message-wrap dn"> </div>
</div>
</form>
</div>
<script>
function myFunction() {
document.getElementById("tstamp").innerHTML = aud.currentTime;
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#contact-form").on("submit",function(e) {
e.preventDefault();
$.ajax({
type: 'post',
url: "insert.php",
data: $( this ).serialize(),
success: function() {
alert("form was submitted");
}
});
return false;
});
});
</script>
</body>
The php file as follows:-
<?php
$servername = "localhost";
$database = "test";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
if(isset($_POST['Submit'])) {
$tstamp=addslashes($_POST['tstamp']);
$message=addslashes($_POST['message']);
$veh_type_1=addslashes($_POST['veh_type_1']);
$veh_type_2=addslashes($_POST['veh_type_2']);
mysqli_query($conn, "insert into messages(message,tstamp,veh_type_1, veh_type_2) values ('$message','$tstamp','$veh_type_1', '$veh_type_2')");
$sql = mysqli_query($conn, "SELECT message,tstamp,veh_type_1,veh_type_2 id FROM messages order by id desc");
$result = mysqli_fetch_array($sql);
echo '<div class="message-wrap">' . $result['message'] . '</div>';
}
?>
The table structure is as follows:-
#database=test, table_name=messages
1 idPrimary int(11) No None AUTO_INCREMENT
2 message text latin1_swedish_ci Yes NULL
3 tstamp float No None
4 veh_type_1 varchar(5) latin1_swedish_ci No None
5 veh_type_2 varchar(5) latin1_swedish_ci No None
EDIT : added the ajax code to index.html, still when I click submit, it says submitted but nothing is updated in table