programing

WordPress에서 사용자 정의 양식을 만들려면 어떻게 해야 합니까?

linuxpc 2023. 2. 25. 19:50
반응형

WordPress에서 사용자 정의 양식을 만들려면 어떻게 해야 합니까?

WordPress 사이트를 가지고 있으며 양식(입력 → 데이터베이스)을 만들고 싶습니다.

두 가지 튜토리얼을 봤어요

둘 다 아주 비슷해요.HTML과 JavaScript로 프런트 엔드를 작성한 후 PHP를 사용하여 데이터베이스로 정보를 처리합니다.

문제는 양식을 제출할 때마다 다음과 같은 404 페이지가 표시된다는 것입니다.

이 페이지를 찾을 수 없습니다.

여기서의 문제는 (또는 알고 싶다)입니다.

  1. 어디다 두죠?process.php파일? (파일질라를 사용하고 있습니다)그 중 몇 군데는 가봤는데public_html/wp-content폴더입니다.

  2. 이름 및 이메일 필드의 유효성을 검사하지 않는 이유는 무엇입니까?(빈 이름 필드 등에 대한 경고 없음)

폼 구조:

이름 : [TEXT], 이메일 : [TEXT], 성별 : [*남성 *여성], 연령 :[텍스트], [전송버튼]

폼 페이지:

<form name="myForm" method="POST" onsubmit="return form_validation()" action="../process.php">
    Your Name: <input id="customer_name" name="customer_name" type="text" />
    Your Email: <input id="customer_email" name="customer_email" type="text" />
    Sex: <input name="customer_sex" type="radio" value="male" />Male <input name="customer_sex" type="radio" value="female" />Female
    Your Age: <input id="customer_age" name="customer_age" type="text" />
    <input type="submit" value="Submit" />
</form>

<script type="text/javascript">
    function form_validation() {
        /* Check the Customer Name for blank submission */
        var customer_name = document.forms["myForm"]["customer_name"].value;
        if (customer_name == "" || customer_name == null) {
            alert("Name field must be filled.");
            return false;
        }

        /* Check the Customer Email for invalid format */
        var customer_email = document.forms["myForm"]["customer_email"].value;
        var at_position = customer_email.indexOf("@");
        var dot_position = customer_email.lastIndexOf(".");
        if (at_position < 1 || dot_position < at_position + 2 || dot_position + 2 >= customer_email.length) {
            alert("Given email address is not valid.");
            return false;
        }
    }
</script>

파일process.php(편집되지 않음):

<?php
    $customer_name = $_POST["customer_name"];
    $customer_email = $_POST["customer_email"];
    $customer_sex = $_POST["customer_sex"];
    $customer_age = $_POST["customer_age"];

    $conn = mysqli_connect("Database Host", "Database Username", "Database  Password", "Database Name");
    if(!$conn) {
        die(‘Problem in database connection: ‘ . mysql_error());
    }

    $query = "INSERT INTO ‘Database Name’.’Table Name’ ( ‘customer_name’, ‘customer_email’, ‘customer_sex’, ‘customer_age’ ) VALUES ( $customer_name, $customer_email, $customer_sex, $customer_age )";
    mysqli_query($conn, $query);

    header("Location: my-site.com/success"); // Redirects to success page
?>

질문 1: WordPress는 개발자가 커스텀 PHP 코드 또는 함수를 추가할 수 있는 액션과 필터 후크를 제공합니다.스니펫을 생성하는 플러그인을 사용하면 폼을 로드하지 않고 PHP 코드를 실행할 수 있기 때문에 대신 성공 페이지가 표시되기 때문에 이 점에 대해 알아봐야 합니다.

액션 및 필터 후크에 대한 자세한 내용은 여기를 참조하십시오.

액션/필터 후크 대신 PHP 파일을 테마 폴더에 업로드할 수 있습니다.하지만 거기에는 결점이 있습니다.WordPress 업데이트 시 파일이 손실될 수 있습니다.


질문 2에 답하려면:JavaScript를 사용하는 경우 폼을 보다 쉽게 검증할 수 있습니다.입력 태그에 'required'라는 단어만 추가하면 됩니다.입력 유형 '이메일'을 필수 키워드와 함께 사용하여 이메일을 확인할 수도 있습니다.아래 예를 참조하십시오.

<form name="myForm" method="POST" action="../process.php">
    Your Name: <input id="customer_name" name="customer_name" type="text" required/>
    Your Email: <input id="customer_email" name="customer_email" type="email" required/>
    Sex: <input name="customer_sex" type="radio" value="male" />Male <input name="customer_sex" type="radio" value="female" />Female
    Your Age: <input id="customer_age" name="customer_age" type="text" />
    <input type="submit" value="Submit" />
</form>

JavaScript 함수를 계속 사용하려면document.getElementById('customer_name')그리고.document.getElementById('customer_email')대신document.forms스크립트 태그도 마지막에 닫아야 합니다.예에 대해서는, 이하를 참조해 주세요.

<script type="text/javascript">
    function form_validation() {
        /* Check the Customer Name for blank submission */
        var customer_name = document.getElementById('customer_name').value;
        if (customer_name == "" || customer_name == null) {
            alert("Name field must be filled.");
            return false;
        }

        /* Check the Customer Email for invalid format */
        var customer_email = document.getElementById('customer_email').value;
        var at_position = customer_email.indexOf("@");
        var dot_position = customer_email.lastIndexOf(".");
        if (at_position < 1 ||
            dot_position < at_position + 2 ||
            dot_position + 2 >= customer_email.length) {

            alert("Given email address is not valid.");
            return false;
        }
    }
</script>

언급URL : https://stackoverflow.com/questions/39944259/how-can-i-create-a-custom-form-in-wordpress

반응형