Saturday, 10 January 2015

Bootstrap 3 – Get Tooltips Working

The tooltips gave me a little bit of an issues but after nutting it out I thought I might do a brief post to help other that run into the same problem.
First this is for Bootstrap version 3.
The way I am calling the tool tips is via the Markup. This is the part of the code where you want the tool tip.
<a id="example" title="" href="#" data-toggle="tooltip" data-placement="left" data-original-title="Tooltip on left">Hover over me</a>

NOTE: I found that the title=”” needed to go before the data-origial-title for the tooltip to work.
At the bottom of your HTML page just below the Bootstrap Core Javascript add this:
<script type="text/javascript">
        $(document).ready(function() {
            $('#example').tooltip();
        });
 
</script>
You can add an number of customisations to suit your needs here in the code. Your choices are listed here. For anyone just learning it might look like this.

<script type="text/javascript">[
 $(document).ready(function(){
    $("#example").tooltip({
        placement: "left",
        title: "tooltip on left",
    });
 });
</script>
This just makes the tooltip go to the left and places a default title if you have not added it to the code as we previously did with the data-original-title.
Hope that helps.

Submit a form with ajax to database and send results to page without refreshing.

I had a project where I needed to submit a form from an HTML page. The form data would be sent by ajax to a php file which would handle the data. The data could be insterted into a database. A result from the php file is then sent back to the HTML form and inserted into the results div id. The trick with this is to also remove text that was typed into the from in the first place.

 <form id="ajaxform" name="ajaxform" action="assets/ajax/enquiry.php" method="post">                
       <div class="form-group name">
              <label class="sr-only" for="name">Name</label>
              <input name="name" id="name" type="text" class="form-control" placeholder="Name:">
       </div><!--//form-group-->
              <div class="form-group email">
                    <label class="sr-only" for="email">Email</label>
                    <input name="email" id="email" type="email" class="form-control" placeholder="Email:">
              </div><!--//form-group-->
       <div class="form-group message">
                      <label class="sr-only" for="message">Message</label>
                      <textarea name="message" id="message" class="form-control" rows="6" placeholder="Message:"></textarea>
        </div><!--//form-group-->
              <button   class="btn btn-lg btn-theme ">Send Message</button>
   </form><!--//form-->


This is where the results from the form that was submitted will be sent back to after being processed by the php script.

 <!-- Results will be sent back to this div -->
   <div id="result"></div>

This is the javascript / ajax call which is placed at the bottom of the HTML page.
<script type="text/javascript">
    $("#ajaxform").submit(function(e){
 
            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");
            $.ajax({  
                type: "POST",  
                url: formURL,  
                data: postData,
                dataType: "json",
                success: function(dataType){  
                    // Successful result will be sent to the result div id.
                    $("#result").html(dataType); 
                },
                complete:function(){
                    //Clear the form
                    $('#ajaxform')[0].reset();
               }
            });
            e.preventDefault(); //STOP default action
            e.unbind(); //unbind. to stop multiple form submit.
 
    });
    $("#ajaxform").submit(); //Submit  the FORM
    </script>

The form data is sent to this php script as directed in the form action. The php script receives the form information and then anything is possible. This short piece of code just verifies that the information has been received and sends back the return message to the orginal form.

if(!empty($_POST['name'])){
        $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
        //Could insert this into the database.  
 $returnMessage = "The form has been submitted: $name ";
 
 echo json_encode($returnMessage);
}
if(!empty($_POST['name'])){
        $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
        //Could insert this into the database.  
 $returnMessage = "The form has been submitted: $name ";
 
 echo json_encode($returnMessage);
}

Submit a form with PHP using a href link – php MySql

If you are trying to submit information from a database using a link here is a method I used in a project recently.  This may or may not be the answer to your needs but give it a go.  Just so you know it is manipulating the url like using the $_GET global.

<?php
  $query = "SELECT id, name FROM folder WHERE parent=0";
 $result = mysql_query ($query);
 while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
  echo ( "<li><a href="/sportpix/view_gallery.php?type=".($row['id'])."" > ". ($row['name']) ." </a></li>");
 
 }//end while loop
?>

Here is a little explanation of the code.
It goes without saying that you need to connect to your database. You can read about thishere.
We are connecting to the database and selecting the table and columns that we need.

1
2
 $query = "SELECT id, name FROM Table1 WHERE parent=0";
 $result = mysql_query ($query);

echo ( "<li><a href="/sportpix/view_gallery.php?type=".($row['id'])."" > ". ($row['name']) ." </a></li>");

PHP & MySQL find the last entry in the database




1
2
So you are looking for the last entry in a table for your database that has auto increment.  Well I have this to share with you. Use the mysql_insert_id() function.


mysql_query("INSERT INTO table1 (product) values ('$somethin')");
$lastID = mysql_insert_id();


1
echo $lastID;

$query = "SELECT * FROM table1 WHERE id=$lastID"; //select from DB image url and set order
 $result = mysql_query ($query);
 
 while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
 echo ($row['something']. "</br>");
 }

PHP convert date yyyy-mm-dd to dd-mm-yyyy

Dates that come from MySql databases are a bit back to front for people to read.  The year first followed by months and then days.  In Australia, New Zealand, Europe and many other countries it is dd-mm-yyyy so this is a small function to turn this around to be more readable.
On your page where you call the date from the datbase, put the date into a variable then call the function.


3
4
5
//This is the function
function changeDate($backwardsDate){
 $newDate = date("d-m-Y", strtotime($backwardsDate));
 return $newDate;
}

//Put the date into a variable
$backwardsDate = 2012-10-28;
//Call the function
echo changeDate($backwardsDate);
// Once processed by the function it will output 
// 28-10-2012

PHP compare dates to determine if one is expired

//Define the album date in a variable
$albumDate = 2012-11-01;
//Call to function
albumExpired($albumDate);


4
15
16
17
18
//  Show expired albums
function albumExpired($expiry){
  // Find todays date.
  $today = date("Y-m-d");
 
                //Change date into a string
  $todaysDate = strtotime($today);
 
                // Change the album date into a string
  $expiryDate = strtotime($expiry);
 
                // compare the two dates (strings)
  if ($expiryDate<$todaysDate){
   return 'Expired';
  }else{
   return 'Not Expired';
  }
}

Friday, 9 January 2015