Insert Automatic Date Into Table Via Button

JavaScript related questions should go here.
Post Reply
azmal101
Posts: 5
Joined: Wed Mar 27, 2013 3:32 pm

Insert Automatic Date Into Table Via Button

Post by azmal101 »

Adding Assessment Form PHP

<?php
 $con=mysqli_connect("localhost","root","","User_db");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($con,"SELECT * FROM Assessments");

 echo "<table border='0' cellpadding='40'
cellspacing='0'>
 <tr>
 <th>Title</th>
 <th>Lecturer</th>
 <th>Start</th>
 <th>Due</th>
 <th>Completed</th>

 </tr>";

 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['Title'] . "</td>";
   echo "<td>" . $row['Lecturer'] . "</td>";
   echo "<td>" . $row['Start'] . "</td>";
   echo "<td>" . $row['Due'] . "</td>";
   echo "<td>" . $row['Completed'] . "<form action='' align='center'>
<input type='checkbox' name='Completed' value='Completed'/><br/>
<input type='submit' name='Completed' value='Completed'/>
</form></td>";
   echo "</tr>";
   }
 echo "</table>";

 mysqli_close($con);
 ?> 

Inserting Assessments to table

<?php
 $con=mysqli_connect("localhost","root","","User_db");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

$strCompleted = $_POST['Completed'];
if ($strCompleted == "Completed") {
	

$strCompleted = "NOW()";
}
else {
	

$strCompleted = "";
}

 $sql="INSERT INTO Assessments (Title, Lecturer, Start, Due, Completed)
 VALUES
 ('$_POST[Title]','$_POST[Lecturer]','$_POST[Start]','$_POST[Due]','$strCompleted')";

 if (!mysqli_query($con,$sql))
   {
   die('Error: ' . mysqli_error());
   }
    header( "Location: Assessment_Calendar.php" ) ;


 mysqli_close($con);
 ?>

Hi guys I was wondering if, using JQuery or anything possible, I could could click on the "Completed" button and it would check the date on which it was created and it would enter that detail in the "Completed" column
In my Assessment table in the sql database
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Insert Automatic Date Into Table Via Button

Post by ExtremeGaming »

Here is an example to get you started. Follow the comments and you should be fine.
<html>
<head>
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
	<script type="text/javascript">
	$(document).ready(function() {

		// When the "completed" button is clicked
		$("#mybutton").click(function(event) {
		
			// Prevent the normal page refresh.
			event.preventDefault();
			
			// If the checkbox is checked, get values
			if($("#mycheckbox").is(":checked")) {
				var checkbox = $("#mycheckbox").val(); // An example of how to get a value in your form
			// If the checkbox is not checked, alert and end the script
			} else {
				alert("Please check the box");
				return false;
			}
	
			// "POST" the values
			$.ajax({
				type: "POST",
				url: "processpage.php",
				data: {  
					// List the data you want sent, if it is a variable, no quotes are needed.  If it is a word, use quotes.
					'checkbox' : checkbox
				},
		
				// If it was successful
				success: function(data){
					alert("Form submitted successfully!");
				}
				
			});
			
		});
	});
	</script>
</head>
<body>

<form method="post">
<input type="checkbox" id="mycheckbox" name="Completed" value="Completed" />
<input type="submit" id="mybutton" name="Completed" value="Completed" />
</form>

</body>
</html>
Note that if a user disables JavaScript, you will need to create a PHP method to complete this. So create an action on your form and make sure to double validate. Also I noticed you aren't securing the submitted data, you can never trust a user.

For the date, you might want to use PHP for it, since the date() function is so much simpler. JavaScript is client-side, so the date you get might not be the one you are on. PHP will always go by the Server Time.
<?php while(!$succeed = try()); ?>
azmal101
Posts: 5
Joined: Wed Mar 27, 2013 3:32 pm

Re: Insert Automatic Date Into Table Via Button

Post by azmal101 »

ExtremeGaming wrote:Here is an example to get you started. Follow the comments and you should be fine.
<html>
<head>
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
	<script type="text/javascript">
	$(document).ready(function() {

		// When the "completed" button is clicked
		$("#mybutton").click(function(event) {
		
			// Prevent the normal page refresh.
			event.preventDefault();
			
			// If the checkbox is checked, get values
			if($("#mycheckbox").is(":checked")) {
				var checkbox = $("#mycheckbox").val(); // An example of how to get a value in your form
			// If the checkbox is not checked, alert and end the script
			} else {
				alert("Please check the box");
				return false;
			}
	
			// "POST" the values
			$.ajax({
				type: "POST",
				url: "processpage.php",
				data: {  
					// List the data you want sent, if it is a variable, no quotes are needed.  If it is a word, use quotes.
					'checkbox' : checkbox
				},
		
				// If it was successful
				success: function(data){
					alert("Form submitted successfully!");
				}
				
			});
			
		});
	});
	</script>
</head>
<body>

<form method="post">
<input type="checkbox" id="mycheckbox" name="Completed" value="Completed" />
<input type="submit" id="mybutton" name="Completed" value="Completed" />
</form>

</body>
</html>
Note that if a user disables JavaScript, you will need to create a PHP method to complete this. So create an action on your form and make sure to double validate. Also I noticed you aren't securing the submitted data, you can never trust a user.

For the date, you might want to use PHP for it, since the date() function is so much simpler. JavaScript is client-side, so the date you get might not be the one you are on. PHP will always go by the Server Time.
Ok thanx..I have figured that part the makes the checkbox return an event..how do make it enter the current date, even if its servers date into the table..do I use php insert scripts or sumthn else?
Post Reply