Rumah >pembangunan bahagian belakang >tutorial php >Cara Mengintegrasikan Helaian Google dengan Borang Laman Web PHP: Panduan Langkah demi Langkah
function doPost(e) { let ss = SpreadsheetApp.openById("123123asdasd"); // Change "SpreadsheetAppId" to your actual sheet id let sheet = ss.getSheetByName("Sheet1"); // Change "Sheet1" to your actual sheet name let data; try { data = JSON.parse(e.postData.contents); } catch (err) { data = e.parameter; } sheet.appendRow([data.fname, data.email, data.message]); return ContentService.createTextOutput("Success").setMimeType(ContentService.MimeType.TEXT); }
Penjelasan:
fungsi doPost(e)
let ss = SpreadsheetApp.openById("123123asdasd");
var sheet = ss.getSheetByName("Sheet1");
cuba { data = JSON.parse(e.postData.contents); } tangkapan (err) { data = e.parameter; }
sheet.appendRow([data.fname, data.email, data.message]);
kembali ContentService.createTextOutput("Kejayaan").setMimeType(ContentService.MimeType.TEXT);
4. Gunakan Skrip sebagai Apl Web
5. Kod PHP untuk Menghantar Data Borang ke Apl Web Skrip Google Apps
Kod html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Submit Form</title> </head> <body> <form method="post" action="submit.php"> <label for="name">Name:</label> <input type="text" name="name" required><br> <label for="email">Email:</label> <input type="email" name="email" required><br> <label for="message">Message:</label> <textarea name="message" required></textarea><br> <input type="submit" value="Submit"> </form> </body> </html>
Kod PHP:
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $url = 'YOUR_WEB_APP_URL'; // Replace with your Google Apps Script Web App URL $postData = array( 'name' => $_POST['name'], 'email' => $_POST['email'], 'message' => $_POST['message'], ); $ch = curl_init($url); $postFields = http_build_query($postData); curl_setopt($ch, CURLOPT_POST, 1); // Send a POST request curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); // Attach the POST fields curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string $response = curl_exec($ch); if ($response === false) { $error = curl_error($ch); echo "cURL error: $error"; } else { echo "Server response: $response"; } curl_close($ch); } ?>
Atas ialah kandungan terperinci Cara Mengintegrasikan Helaian Google dengan Borang Laman Web PHP: Panduan Langkah demi Langkah. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!