Home >Backend Development >PHP Tutorial >PHP combined with js implements click on hyperlink to perform delete confirmation operation, js hyperlink_PHP tutorial

PHP combined with js implements click on hyperlink to perform delete confirmation operation, js hyperlink_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:341045browse

php combines with js to implement click on the hyperlink to perform the deletion confirmation operation, js hyperlink

As the title says, this time we want to execute the js code by clicking on the hyperlink and confirm whether to delete the database data, using php.

First connect to the database and query the database data:

Copy code The code is as follows:

$dbms='mysql'; //Database type, for developers, if they use different databases, they only need to change this, and there is no need to remember so many functions
$host='localhost'; //Database host name
$dbName='db_database19'; //Database used
$user='root'; //Database connection user name
$pass='root';                                                                                                                                                                                                                                                     // Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //Initializing a PDO object means creating a database connection object $pdo
$query="select * from tb_pdo_mysql"; //Define SQL statement
$result=$pdo->prepare($query); //Prepare 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 to the associated index
?> 





delete

}
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "
");
}
?>

Call a javascript method at the hyperlink deletion point and pass the record id. The js method is:

Copy code The code is as follows:

<script><br> Function del(_id) {<br>             if (confirm("Confirm deletion"))<br>              {<br>                  window.location.href="index.php?conn_id="+_id;          }<br> }<br> </script>

Delete database record code:

Copy code The code is as follows:

If(@$_GET['conn_id']!=""){
$dbms='mysql'; //Database type. For developers, if they use different databases, they only need to change this and don’t need to remember so many functions
$host='localhost'; //Database host name
$dbName='db_database19'; //Database used
$user='root'; //Database connection username
$pass='root'; //Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
Try {
           $pdo = new PDO($dsn, $user, $pass);           //Initializing a PDO object means creating 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);                                                                                                                                             $ Result-& gt; bindparam (': id', $ _ get ['conn_id']); // Binded updated data
          $result->execute();
} catch (PDOException $e) {
echo 'PDO Exception Caught.';
echo 'Error with the database:
';
echo 'SQL Query: '.$query;
echo '
';<br>
            echo "Error: " . $e->getMessage(). "<br/>";           <br>
echo "Code: " . $e->getCode(). "<br/>";<br>
              echo "File: " . $e->getFile(). "<br/>";<br>
echo "Line: " . $e->getLine(). "<br/>";<br>
             echo "Trace: " . $e->getTraceAsString(). "<br/>";<br>
echo '
';
}
}
?>

This code should be placed at the beginning of the body part of the html page, or at worst, before the query record code.

Rookie php js confirms the deletion code and clicks the link. A pop-up window will appear, including the entire code for php get to obtain feedback. There is only the JS part on the Internet

Haha, this is simple,

Delete
Click OK to jump directly to the delete page.
Click Cancel and leave it unchanged.
php gets url parameters
$id = $_GET[id];
If you still don’t understand anything, you can leave me a message through Baidu Hi.

php, click on the hyperlink text to redirect to delete/change mysql data

Delete

When foreach traverses the data, write the id value, and then perform the delete operation on the delete.php page.
Either you include the page connecting to the database into delete.php (not recommended)
Or write them all on one page, and then switch case to determine whether the operation accepts the id and then execute
Delete It is recommended to
delete.php page
connect the database switch($act=$_GET['act']){ case 'del' : .........;break;case....}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904015.htmlTechArticlephp combines with js to implement click hyperlink to perform delete confirmation operation. The js hyperlink is as titled. This time it is to achieve click hyperlink The link implements executing the js code and confirms whether to delete the database data, using...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn