New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home Blog Page 8

Read and display the text file using javascript

Welcome back to shortlerner.com, in our previous post we was learn How to Send Attachment With Email with the help of PHP. today in this post we will see how to read and display a text file data into html text area tag with the help of JavaScript.

Read and display the text file using javascript

so here we are creating a file tag where user upload the text file and there is another html form element text area.

so whenever user upload the file we use some JavaScript functions which will read the file and display all the data of the file inside the text area.

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<input type="file" onchange="onFileSelected(event)">
<br>
<textarea id="result" rows="10" cols="50"></textarea>
<script type="text/javascript">
function onFileSelected(event) {
var selectedFile = event.target.files[0];
var reader = new FileReader();
var result = document.getElementById("result");
reader.onload = function(event) {
    result.innerHTML = event.target.result;
  };
reader.readAsText(selectedFile);
}
</script>

simply just copy the above code and put it on your root directory.
hope this post will helps you, if you still facing some bugs or error to than feel free to contact.

thanks for reading the post and please share this code with your developer buddy.

keep coding , keep learning

Send Attachment With Email using php

0

Welcome back to shortlearner.com, in our previous post we learn how to add pagination using PHP and MySQL. so today in this post we will see how to Send Attachment With Email using php.

so first of all we should take an overview of our working scenario.
in the below code we are created a form with having email, subject,message and attachment fields.

once the form is submitted our script is check that the file’s format is valid or not.
if the file format is valid than the attachment is concatenate with our message field and with the help of MIME (Multipurpose Internet Mail Extensions) version and its content type and boundary.

we will send the attachment. once the mail is send successfully our script will display the success message and if the mail isn’t sent than it will display failure message as well.

send email with attachment in php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<html>
    <body>
        <h2>Send Mail</h2>
        <form method="post" action="" enctype="multipart/form-data">
            <input type="text" name="email" placeholder="email"><br>
            <input type="text" name="sub" placeholder="Subject"><br>
            <textarea name="msg" placeholder="Write email message"></textarea><br>

            Attach file:<br>
            <input type="file" name="attach1"/><br><br>
            <input type="submit" value="Send Mail"/>
        </form>
    </body>
</html>
<?php
if (isset($_FILES) && (bool) $_FILES) {

    $allowedExtensions = array("pdf", "doc", "docx", "gif", "jpeg", "jpg", "png");

    $files = array();
    foreach ($_FILES as $name => $file) {
        $file_name = $file['name'];
        $temp_name = $file['tmp_name'];
        $file_type = $file['type'];
        $path_parts = pathinfo($file_name);
        $ext = $path_parts['extension'];
        if (!in_array($ext, $allowedExtensions)) {
            die("File $file_name has the extensions $ext which is not allowed");
        }
        array_push($files, $file);
    }

    // email fields: to, from, subject, and so on
    $to = $_POST['email'];
    $from = "admin@localhost";
    $subject = $_POST['sub'];
    $message = $_POST['msg'];
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    // headers -for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

    // multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for ($x = 0; $x < count($files); $x++) {
        $file = fopen($files[$x]['tmp_name'], "rb");
        $data = fread($file, filesize($files[$x]['tmp_name']));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $name = $files[$x]['name'];
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }
    // send

    $ok = mail($to, $subject, $message, $headers);
    if ($ok) {
        echo "<p>mail sent to $to!</p>";
    } else {
        echo "<p>mail could not be sent!</p>";
    }
}
?>

guys this code is not works on local server like xampp,wamp and appserv. so try it on live server.

delete multiple records using checkbox php

0

Welcome back to shortlearner.com, in our previous post we learn How to Create pagination using php and mysql . now in this post we will learn how to delete multiple records using checkbox with the help of PHP and MySQL.

delete multiple records in PHP

before start this tutorial first of all we should understand the overall scenario of this post.
now first of all we need to create a database connection , so in this example my database name is testdb .

after creating a database connection we fetch all the records from our user table and designed it in a tabular format with checkbox field, and also create a java script function that will help us to select all check boxes at a time.
so just follow the below code.

Also Read :
Get Domain name from URL
Unable to create a directory a wordpress error
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
$conn = mysqli_connect("localhost","root","rootroot","testdb");
if(isset($_POST['save'])){
	$checkbox = $_POST['check'];
	for($i=0;$i<count($checkbox);$i++){
	$del_id = $checkbox[$i]; 
	mysqli_query($conn,"DELETE FROM user WHERE id='".$del_id."'");
	$message = "Data deleted successfully !";
}
}
$result = mysqli_query($conn,"SELECT * FROM user");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Delete userandroid data</title>
</head>
<body>
<div><?php if(isset($message)) { echo $message; } ?>
</div>
<form method="post" action="">
<table class="table table-bordered">
<thead>
<tr>
    <th><input type="checkbox" id="checkAl"> Select All</th>
	<th>userandroid Id</th>
	<th>First Name</th>
	
	<th>Email id</th>
</tr>
</thead>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
    <td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["id"]; ?>"></td>
	<td><?php echo $row["id"]; ?></td>
	<td><?php echo $row["user_name"]; ?></td>
	<td><?php echo $row["email"]; ?></td>
	
</tr>
<?php
$i++;
}
?>
</table>
<p align="center"><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
</form>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>

copy the above code and save it on htdocs directory of your xampp server.
there is a www folder if you are using WAMP or Appserv local server.

share this code with your developer buddies and if you have another way to make it simple ,please feel free to contact we will update your method also in this post.

keep coding, keep learning

How to Create pagination using php and mysql

0

welcome back to shortlearner.com, in our previous post we learn how to create a signup or registration page with the help of AJAX, PHP and MySQL.

Now in this post today we will learn how to create a pagination using PHP and MySQL.

so most of the time we see when we are developing any kind of software and when we fetch records from our MySQL database it probably returns us thousand or millions records. so it is not a good idea to show all records on a single page, so we split all those records into multiple pages.

pagination in php and mysql

so before start this tutorial we just take an overview how we split records into multiple pages.
first of all we are establish a database connection , in this tutorial my database name is library. so i am going to fetch all the books name and their title which are stored in the table name called bookstore.

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
$hostname = 'localhost'; // your mysql hostname
$username = 'root';   // Your mysql db username
$password = 'rootroot';   // You mysql db password
$database = 'library';       // mysql db name
$con = mysqli_connect($hostname, $username, $password, $database);
 
    if (isset($_GET["page"])) { 
      $page  = $_GET["page"]; 
    } else { 
      $page=1; 
    };
$recordsPerPage=20; 
$start = ($page-1) * $recordsPerPage; 
$query = "SELECT * FROM bookstore LIMIT $start, $recordsPerPage"; 
$result = mysqli_query($con, $query);
 
echo "<table><tr><th>Title</th><th>Description</th></tr>";
while ($row = mysqli_fetch_assoc($result)) { 
            echo "<tr>
            <td>".$row['Title']."</td><td>".$row['Bookname']."</td></tr>";            
}
echo "</table>";
 
$query = "SELECT * FROM bookstore"; 
$result = mysqli_query($con, $query); //run the query
$totalRecords = mysqli_num_rows($result);  //count number of records
$totalPages = ceil($totalRecords / $recordsPerPage); 
 
echo "<a href='abc.php?page=1'>".'|<'."</a> "; // Go to 1st page  
 
for ($num=1; $num<=$totalPages; $num++) { 
            echo "<a href='abc.php?page=".$num."'>".$num."</a> "; 
}; 
echo "<a href='abc.php?page=$totalPages'>".'>|'."</a> "; // Go to last page
?>
</body>
</html>

How to extract all links of web page using PHP

0

Welcome back to shorltearner.com, in our previous post we learn how to Convert words to numbers with the help of PHP.

extract all url of website

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

so in this post today we will learn how to extract all links of any web page with the help of PHP and will store all the links into MySQL database.
also make a URL extractor platform which will help us to analysis the website.

if some one is using WordPress website we can extract the URLs and check which kind of themes and plugins that website developers are used.
so just follow the below code an develop your own URL extractor.

<?php 
function getAllLinks($url) {
$urlData = file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($urlData);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
 for($i = 0; $i < $hrefs->length; $i++){
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    $url = filter_var($url, FILTER_SANITIZE_URL);
    if(!filter_var($url, FILTER_VALIDATE_URL) === false){
        $urlList[] = $url;  
    }
 }
return array_unique($urlList);
}
?>

so in the above code we are just creating a PHP function that takes website URL as a parameter and fetch /extract all the links.
so in the below code we are just passing website URL as a parameter to our function.

<?php
$url = 'http://localhost/wordpress';
var_dump(getAllLinks($url));
?>