Day: January 22, 2018

Sending data using Ajax to PHP

When you need to send data to server side then you can use Ajax to send data. This is simple code which help you.


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHP, jQuery search demo</title>
    <link rel="stylesheet" type="text/css" href="my.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("input").keyup(function () {
                $('#results').html('');
                var searchString = $("#search_box").val();
                var data = 'search_text=' + searchString;
                if (searchString) {
                    $.ajax({
                        type: "POST",
                        url: 'http://localhost/search.php',
                        data: data,
                        dataType: 'text',
                        async: false,
                        cache: false,
                        success: function (result) {
                            $('#results').html(result);
                            //window.location.reload();

                        }
                    });
                }
            });
        });
    </script>

</head>
<body>
<div id="container">
    <div style="margin:20px auto; text-align: center;">
        <form method="post" action="do_search.php">
            <input type="text" name="search" id="search_box" class='search_box'/>
            <input type="submit" value="Search" class="search_button"/><br/>
        </form>
    </div>
    <div>

        <div id="searchresults">Search results :</div>
        <ul id="results" class="update">
        </ul>

    </div>
</div>

</body>
</html>

then create a search.php file which process ajax request value. you can also select data from database using this ajax data, also set this data to session etc.

This is search.php file

<?php 

$searchquery = $_POST['search_text']; 
echo $searchquery;
?>

if you face any problem then please inform me.

Comment

backup total database using php code.

If you want to backup total database using php code then you can follow this code. In this code which backup total db , if you want to backup some table then you can also use this code. This is simple code.

<?php

// call function which backup database
backup_database('localhost', 'root', '', 'database_name', '*');

// this is main function which process all table data and ready for export
// first host name, db_username, db_password, db_name, when use * then all table
function backup_database($hostname, $user, $pass, $dbname, $tables = '*')
{
    $link = mysqli_connect($hostname, $user, $pass, $dbname);

    if (mysqli_connect_errno()) {
        echo "Failed to connect: " . mysqli_connect_error();
        exit;
    }

    // set utf8 character set
    mysqli_query($link, "SET NAMES 'utf8'");

    // get all table when * and insert all table into tales array
    if ($tables == '*') {
        $tables = array();
        $result = mysqli_query($link, 'SHOW TABLES');
        while ($row = mysqli_fetch_row($result)) {
            $tables[] = $row[0];
        }
    } else {
        // when table name specified then process
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }

    $return = '';
    // process table and process filed using this loop
    foreach ($tables as $table) {
        $result = mysqli_query($link, 'SELECT * FROM ' . $table);

        // get all fields and process them
        $num_fields = mysqli_num_fields($result);
        $num_rows = mysqli_num_rows($result);

        $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
        $row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE ' . $table));
        $return .= "\n\n" . $row2[1] . ";\n\n";
        $counter = 1;

        // process all fields under this table
        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = mysqli_fetch_row($result)) {
                // make table and format table
                if ($counter == 1) {
                    $return .= 'INSERT INTO ' . $table . ' VALUES(';
                } else {
                    $return .= '(';
                }
                // make all fields and format also
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = str_replace("\n", "\\n", $row[$j]);
                    if (isset($row[$j])) {
                        $return .= '"' . $row[$j] . '"';
                    } else {
                        $return .= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return .= ',';
                    }
                }
                // when all row done then return using end line
                if ($num_rows == $counter) {
                    $return .= ");\n";
                } else {
                    $return .= "),\n";
                }
                ++$counter;
            }
        }
        // return using new line
        $return .= "\n\n\n";
    }
    // ready database for export and make sql file
    $fileName = 'backup-' . $dbname . '.sql';
    $handle = fopen($fileName, 'w+');
    fwrite($handle, $return);
    if (fclose($handle)) {
        echo "Backup Done: " . $fileName;
        exit;
    }
}

?>

if you face any problem then please inform me. I will try to help you

Comment