开发者

How do I embed buttons that call php scripts into a MySQL table

开发者 https://www.devze.com 2023-03-18 21:43 出处:网络
I have various records in a MySQL DB which I have presented in an html table on a webpage. I would like two buttons next to each record both of which call php scripts.

I have various records in a MySQL DB which I have presented in an html table on a webpage. I would like two buttons next to each record both of which call php scripts.

Example: The table contains the names of three people and two buttons say Approve or Deny next to each name in the table. When the user chooses Approve the name that is next to the button is sent to a php script which can do many functions, such an email the name to a person.

What's the best way of going about this? I though maybe to sent the buttons as html string to the mysql db alongside each record开发者_如何学运维 and hopefully they would render in the table as a button? any ideas? i'm lost...


You are going to be rendering the table in html likely via php. When you are outputting the table, also output the buttons. Don't put the buttons in the database because what they do might change.

The buttons can either A. Simply link to another PHP page (you could just make it a link) OR B. Call a AJAX to load a different PHP page in the background while leaving you on the same page.


Very very basically:

yourpage.php:

<button onclick="document.location=yourscript.php?action=email&name=Fred">Fred</button>

yourscript.php:

<?php

$name = $_GET['name'];
$action = $_GET['action'];

switch($action) {
    case 'email':
         mail("$name@yourcompany.com", 'Button click', 'Someone done did a click, boss');
         break;
    case 'something else':
         ...
         break
     default:
         ... unknown action
}

header("Location: yourpage.php");

This is a rather ugly way of doing it. Better methods would use AJAX, perhaps, to save the full roundtrip.


You don't embed the buttons in the table. You have the records in your table something like:

name | approval_state
---------------------
joe  |    0
jane |    1     where 'approval state' is some like:
bob  |    2     0 = 'neither approved nor denied
                1 = 'approved'
                2 = 'denied'

Then render your table based on the current approval state, and link your button to an ajax call to your PHP script.

0

精彩评论

暂无评论...
验证码 换一张
取 消