Thursday, May 25, 2017

get all input type and there value in a form

<script>function validateRequiredFields()
{
$('#registerForm input').each(
function(index){
var input = $(this);
//alert(input.attr('required'));
if(input.attr('required')=='required'){
var id=input.attr('id');
if(input.val()==''){
$("#"+id).next(".validation").remove();
$("#"+id).css("border", "1px solid red");
$("#"+id).after("<span class='validation' style='color:red;margin-bottom: 20px;'>Please Enter value!</span>");
}else{
$("#"+id).next(".validation").remove();
$("#"+id).css("border", "1px solid green");

}
}
//alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
}
);


} </script>

call ajax on form submit

<script>
$("#contactForm").submit(function(e) {
//---------------^---------------
e.preventDefault();

var name =$("#name").val();
var surname =$("#surname").val();
var email =$("#email").val();
var dataString = "name="+name+"&surname="+surname+"&email="+email;
$.ajax({
  type: "POST",
  url: "send.php",
  data: dataString,
  cache: false,
  success: function(result){
if(result==1){
$("#mess_sucess").html('');
$('#mess_sucess').show();
var mass='<span style="color:red;margin-bottom: 20px;">Thanks. Your Request Submitted Successfully!</span>';
$('#mess_sucess').html(mass);
document.getElementById('download').click();

}else{
$("#mess_sucess").html('');
$('#mess_sucess').show();
var mass='<span style="color:red;margin-bottom: 20px;">Thanks. Your Request Not Submitted Successfully!</span>';
$('#mess_sucess').html(mass);

}

  }
});

});
</script>

Monday, May 8, 2017

Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago

Example
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);

Output

4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago

Function 
function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}