簡體   English   中英

如何為 PHP 自定義我自己的狀態消息? [等候接聽]

[英]How Do I Customize My Own Status Messages for PHP? [on hold]

當我在其他網站上創建新帳戶時,我會看到一個警報樣式對話框,告訴我我的信息是否已成功發送,同時停留在我創建帳戶的同一網頁上。 該對話框有一個指向 select 的“確定”按鈕,用於關閉對話框並將 HTML 表單重置為空字段。

我想在我的網站上這樣做。 目前,我有一個 HTML 表單,它捕獲 email 地址。 When I click SUBMIT, the PHP file takes the email address, verifies the email, uploads it into mysql, then opens a new browser window echoing the status of the PHP processes that were just run.

我不希望 PHP 打開瀏覽器 window 顯示進程狀態。 我只是想在表單所在的同一網頁中打開一個對話框,說明 email 地址已成功上傳,或未成功上傳,或者 email 地址無效。 無論哪種方式,都不會打開第二個 window 和一個警報樣式對話框,該對話框顯示狀態,帶有“確定”按鈕以關閉對話框,並使用空白字段重置表單。

我花了 2 天時間在互聯網上搜索各種措辭,並在這里探索了圖書館,但我找不到這方面的幫助。

我認為我的問題有一個標准解決方案,因為這是大多數網站在開設帳戶時的工作方式。 我可能在搜索時使用了不正確的術語。 這是我的 php 代碼,用於將 HTML 表單數據上傳到 mysql:

<?php

    //MySQL/MariaDB connection block with the server location (localhost), the database name, and the database username and password.

    $email_a = $_POST['emailDesktop'];
    $email_b = $_POST['emailVerifyDesktop'];

    if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
        echo "Email address '$email_a' is considered valid.\n";
}
    if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
        echo "Email address '$email_b' is considered valid.\n";
}   else {
        echo "Email address '$email_b' is considered invalid.\n";
}


    $hostname = "xxxxxxxxxxxxxx";
    $username = "xxxxxxxxxxxxxx";
    $password = "xxxxxxxxxxxxxx";
    $db = "xxxxxxxxxxxxxx";

    //Then we add a section to connect to the database, and give an error if the connection fails:

    $dbconnect=mysqli_connect($hostname,$username,$password,$db);

    if ($dbconnect->connect_error) {
      die("Database connection failed:" .$dbconnect->connect_error);
    }


    //Next, we "translate" the data from the HTML form into PHP variables:

    if(isset($_POST['submit'])) {
        $emailDesktop=$_POST['emailDesktop'];




        //Then insert this information into the database:

        $query = "INSERT INTO members (emailDesktop)
        VALUES ('$emailDesktop')";


        //Add a nested if statement which will give an error message if the process fails, or thank the user for their review if it succeeds:

        if (!mysqli_query($dbconnect, $query)) {
        echo 'An error occurred when submitting.';
        } else {
        echo 'Upload successful.';


        }
    }

您可以使用 ajax 將您的表單數據提交到服務器,並使用模式來提醒最終用戶有關響應。

以下代碼是通過 ajax 提交數據的示例,使用jQuery

$("button").click(function(){
  $.ajax({
    url: "verify_email.php",
    data: your_data,
    success: function(result){
      showalert(result);
    }
  });
});

請參閱w3schools以了解如何制作 CSS/JS 模態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM