Guru

Embed an External Page Without an Iframe

The best solution to embed an external page without Iframe is ‘object’ element.

1. The HTML tag is used for embedding an object within an HTML document.
2. Use this element to embed multimedia (like audio, video, Java applets, ActiveX, PDF, and Flash) in your web pages.
3. You can also use the tag to embed another webpage into your HTML document.

HTML:

  • The TYPE attribute specifies the media type of the resource referenced by the DATA attribute.
  • The DATA attribute specifies the URI of the embedded object.
  • The WIDTH and HEIGHT attributes define the dimensions of the object.

Refresh Parent Page after Closing Popup Window in JavaScript

On button click event call the following javascript function, named RefreshParent.

<script type=”text/javascript”>
function RefreshParent()
{
//parentPage.php is the parent page you want to redirect and refresh
window.opener.location.href = “parentPage.php”;
self.close();
}
</script>

HTML:
<input type=”button” name=”submit” value=”Close” onclick=”return RefreshParent();” />

Reset Form Field Values using jQuery

To show a practical example we’ll start with a simple HTML form that has a few different form elements:

HTML:
<form id=”form1″>
<table width=”50%”>
<tr>
<td width=”15%”>Select Category:</td>
<td>
<select id=”category” name=”category”>
<option value=”-1″>—Select—</option>
<option value=”Circle”>Circle</option>
<option value=”Square”>Square</option>
<option value=”Rectangle”>Rectangle</option>
</select>
</td>
</tr>
<tr>
<td>Enter Date:</td>
<td><input type=”text” id=”date” name=”date” /></td>
</tr>
<tr>
<td>Enter Description:</td>
<td><input type=”text” id=”description” name=”description” /></td>
</tr>
<tr>
<td></td>
<td><input type=”button” id=”reset” name=”Reset” value=”Reset” /></td>
</tr>
</table>
</form>

JavaScript:
<script src=”//code.jquery.com/jquery-1.8.3.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function() {
$(‘#reset’).click(function() {
$(this).closest(‘form’).find(‘input[type=text], select’).val(”);
});
});

If you want to reset all form field values, use following block of code:

$(document).ready(function() {
$(‘#reset’).click(function() {
$(“#form1”)[0].reset();
});
});
</script>

On clicking Reset button, all form elements changed back to their original values as they were when the page was loaded.

cURL and its Features

1. cURL is a library that lets you make HTTP requests in PHP.

2. cURL is a way you can hit a URL from your code to get a html response from it. cURL means client URL which allows you to connect with other URLs and use their responses in your code.

3. The curl library is used for making network requests from a high-level programming language.

4. It greatly simplifies the code necessary to make HTTP GETs and POSTs and offers developers a lot of network connection options.

5. Each time you do a cURL request with PHP, you’ll need to do the following:
– Init cURL
– Set the needed options
– Ask cURL to execute the request
– Free the resources

Send HTTP GET Request with CURL

$url = ‘http://www.testsite.com/curl_example.php?username=$username&password=$password‘;

//Open Connection
$ch = curl_init();

//Sets the url &
curl_setopt($ch, CURLOPT_URL, $url);

//Receive Server Response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Send request and save response to $response
$response = curl_exec($ch);

//Close cURL resource and free up system resources
curl_close($ch);

var_dump($response); //Show output

Explanation of above code:

1) $ch = curl_init(); will initiate the curl session.
2) $url is the url where you want to post the values. CURLOPT_URL will tell curl that $url to post values is $url.

3) In curl_setopt() you can set the options for your curl session:
CURLOPT_RETURNTRANSFER will tell curl to return values from $url in string format.

4) curl_exec(); will execute the curl session.
5) curl_close(); will close curl session.

Send HTTP POST Request with CURL

$url = ‘http://www.testsite.com/curl_example.php‘;

$post_parameters = ‘username=$username&password=$password’;

//Open Connection
$ch = curl_init();

//Sets the url & POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_parameters);

//Receive Server Response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Send request and save response to $response
$response = curl_exec($ch);

//Close cURL resource and free up system resources
curl_close($ch);

var_dump($response); //Show output

Explanation of above code:

1) $ch = curl_init(); will initiate the curl session.
2) $url is the url where you want to post the values. CURLOPT_URL will tell curl that $url to post values is $url.

3) In curl_setopt() you can set the options for your curl session:
CURLOPT_POST if set TRUE, then you can post the values.
CURLOPT_POSTFIELDS will have all the values to be post on the $url.
CURLOPT_RETURNTRANSFER will tell curl to return values from $url in string format.

4) curl_exec(); will execute the curl session.
5) curl_close(); will close curl session.

Backup Database Using mysqldump class

This script can be used to backup MySQL databases.

This script is written in PHP for dumping entire database’s into a MySQL friendly format. It can generate a backup file (.sql) that contain SQL statements to create all the database tables and insert its table record data.

index.php

<?php
require_once(‘mysqldump.class.php’); //Location Of Class File.
$drop_table_if_exists = false; //Add MySQL ‘DROP TABLE IF EXISTS’ Statement To Output?

$dbhost = ‘hostname’; //Server Hostname.
$dbuser = ‘username’; //Server User Name.
$dbpass = ‘password’; //Server Password.
$dbname = ‘database name’; //Database Name On MySQL Server.

$creation_date = date(“d-M-Y”);

//START Header Output. ———————————————-
$title = ‘Database MySQLDump for ‘.$dbname; //Write Title Info to Variable Using Chosen Database Name and Version.
echo “<pre>”;
echo “<!DOCTYPE html PUBLIC \”-//W3C//DTD XHTML 1.0 Strict//EN\” \”http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\”>”;
echo “<html xmlns=\”http://www.w3.org/1999/xhtml\”>”;

echo “<head>”;
echo “<meta http-equiv=\”Content-Type\” content=\”text/html; charset=utf-8\” />”;
echo “<title>”.$title.”</title>”;
echo “</head>”;
echo “<body>”;
echo “</pre>”;
//END Header Output. ————————————————

$backup = new MySQLDump();
$backup->droptableifexists = $drop_table_if_exists;
$backup->connect($dbhost,$dbuser,$dbpass,$dbname); //Connect To Database

if (!$backup->connected) { die(‘Error: ‘.$backup->mysql_error); } //On Failed Connection, Show Error.

$backup->list_tables(); //List Database Tables.
$broj = count($backup->tables); //Count Database Tables.

echo “<pre>”;

echo “<div>”;
echo “\n<b>Database Selected:</b> $dbname on $dbhost { <span style=’color:red’>Creation Date: “.$creation_date.”</span> } “; //Show Database, Server, User Name and Password.
echo “</div>\n”;

$data = ”;

echo ‘<div id=”main-right-gc” style=”height: 50%; overflow-y: scroll; border:1px solid black;”>’;

//START Database MySQL Dump. ———————————————————

$data .= “– phpMyAdmin SQL Dump”;

$data .= “\n\n–“;
$data .= “\n– Database: `”.$dbname.”`”;
$data .= “\n–“;

$data .= “\n\nCREATE DATABASE IF NOT EXISTS `”.$dbname.”`”;

$data .= “\nUSE `”.$dbname.”`”;

$data .= “\n\n– ——————————————————–“;
$data .= “\n\n–“;

$data .= “\n”;

for ($i=0;$i<$broj;$i++)  {
$table_name = $backup->tables[$i]; //Get Table Names.
$backup->dump_table($table_name); //Dump Data to the Output Buffer.
echo htmlspecialchars($backup->output); //Display Output.
$data .= htmlspecialchars($backup->output);
}
echo “\n– END OF MYSQL DATABASE DUMP –“;
//END Database MySQL Dump. ———————————————————–

echo ‘</div>’;

echo “</body>”;
echo “</html>”;

//Write to file
$my_file = $dbname.’.sql’;
$handle = fopen($my_file, ‘w’) or die(‘Cannot open file: ‘.$my_file);
fwrite($handle, $data); // Writes data to sql file
?>

Continue reading