Php and js achieve deletion confirmation by clicking the hyperlink. js hyperlink _ PHP Tutorial

Source: Internet
Author: User
Tags switch case
Php and js click the hyperlink to execute the delete confirmation operation. js hyperlink. Php and js click the hyperlink to execute the delete confirmation operation. the js hyperlink is as follows: click the hyperlink to execute the js code and confirm whether to delete the database data, php and js are used to achieve deletion confirmation by clicking the hyperlink. js hyperlink

As shown in the following figure, click the hyperlink to execute the js code and confirm whether to delete the database data. php is used.

First, connect to the database to query the database data:

The code is as follows:


<? Php
$ Dbms = 'mysql'; // Database type. developers do not need to remember so many functions to use different databases.
$ Host = 'localhost'; // database host name
$ DbName = 'DB _ database19 '; // database used
$ User = 'root'; // database connection username
$ Pass = 'root'; // password
$ Dsn = "$ dbms: host = $ host; dbname = $ dbName ";
Try {
$ Pdo = new PDO ($ dsn, $ user, $ pass); // initialize a PDO object, that is, create a database connection object $ pdo
$ Query = "select * from tb_pdo_mysql"; // define an SQL statement
$ Result = $ pdo-> prepare ($ query); // prepare the query statement.
$ Result-> execute (); // execute the query statement and return the result set.
While ($ res = $ result-> fetch (PDO: FETCH_ASSOC) {// while loop outputs the query result set, and sets the result set as the associated index
?>

<? Php echo $ res ['id'];?>
<? Php echo $ res ['pdo _ type'];?>
<? Php echo $ res ['database _ name'];?>
<? Php echo $ res ['dates'];?>
) "> Delete

<? Php
}
} Catch (PDOException $ e ){
Die ("Error! : ". $ E-> getMessage ()."
");
}
?>


Call a javascript method to delete a hyperlink and pass the record id. the js method is as follows:

The code is as follows:


Script
Function del (_ id ){
If (confirm ("confirm deletion "))
{
Window. location. href = "index. php? Conn_id = "+ _ id; // refresh this page
}
}
Script

Code for deleting database records:

The code is as follows:


<? Php
If (@ $ _ GET ['Conn _ id']! = ""){
$ Dbms = 'mysql'; // Database type. developers do not need to remember so many functions to use different databases.
$ Host = 'localhost'; // database host name
$ DbName = 'DB _ database19 '; // database used
$ User = 'root'; // database connection username
$ Pass = 'root'; // password
$ Dsn = "$ dbms: host = $ host; dbname = $ dbName ";
Try {
$ Pdo = new PDO ($ dsn, $ user, $ pass); // initialize a PDO object, that is, create a database connection object $ pdo
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION );
$ Query = "delete from tb_pdo_mysql where Id =: id ";
$ Result = $ pdo-> prepare ($ query); // pre-prepared statement
$ Result-> bindParam (': ID', $ _ GET ['Conn _ id']); // bind the updated data
$ Result-> execute ();
} Catch (PDOException $ e ){
Echo 'pdo Exception Caught .';
Echo 'error with the database:
';
Echo 'SQL Query:'. $ query;
Echo'

';
echo "Error: " . $e->getMessage(). "
";
echo "Code: " . $e->getCode(). "
";
echo "File: " . $e->getFile(). "
";
echo "Line: " . $e->getLine(). "
";
echo "Trace: " . $e->getTraceAsString(). "
";
echo '
';
}
}
?>

This code is placed at the beginning of the body part of the html page. The worst part should also be placed before the query record code.


Click the link to confirm the deletion code of Cainiao php js. the pop-up window will pop up, including the entire code segment for php get feedback. there is only JS part on the Internet.

Haha, this is simple,
Delete </a>
Click OK to directly jump to the delete page.
Click Cancel.
Php retrieves url parameters
$ Id = $ _ GET [id];
If you have any questions, you can leave a message via Baidu Hi.

Php: click the hyperlink text to delete or change mysql data.

Delete </a>

When foreach traverses the data, write the id value, and then delete the data on the delete. php page.
You can either include the page for database connection to delete. php (not recommended)
Either write them on a page, and then switch case to determine whether the operation accepts the id and then execute
Delete </a> This is recommended.
Delete. php page
Connect to the database switch ($ act = $ _ GET ['act ']) {case 'Del':...; break; case ....}

Click the hyperlink to execute the js code and confirm whether to delete the database data...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.