简体   繁体   中英

Process PHP form on hidden page, but stay on same page. No Ajax, pure php only (project)

I'm new to PHP, and I'm sure this is a common think to do, but 99% of the answers I have found to this involve AJAX, JQuery and/or JavaScript.

I am only allowed to use HTML/CSS and PHP in my project, so I need a working option that does not involve anything else.

I have the following setup:

  • index.php, this holds my form structure
  • insert.php, this sanitizes/validates and inserts form data into a database table

Leaving action as insert.php sends me to my insert.php page, which I want to keep private and for developer eyes only...no good.

form action=" " method="post"
// OR
form action="index.php" method="post">

Leaving action blank or as index.php keeps me on the same page, but...
I want to keep my form processing in a separate file (insert.php) and NOT on the same page, if at all possible.

Do I have any options for this that are not excessively complex in pure PHP?

Thanks for any advice.
(PS. If there's any blatant errors or poor form here, I'm all ears to constructive criticism)

Here's a snapshot of my insert.php file if its helpful. I can upload my form as well, but its very basic (just select a course, input first/last name, input student id).

// Clean the data coming from the MySQL tables
$course_clean = htmlentities($_POST['course_id']);
$stu_name_clean = htmlentities($_POST['first_last']);
$stu_id_clean = htmlentities($_POST['stu_id']);

// Escape user input coming from forms
$course = mysqli_real_escape_string($open, $_REQUEST['course_id']);
$stu_name = mysqli_real_escape_string($open, $_REQUEST['first_last']);
$stu_id = mysqli_real_escape_string($open, $_REQUEST['stu_id']);

// INSERT DATA 
    $insert = "INSERT IGNORE INTO enrolled (course_id, stu_id) VALUES ('$course', '$stu_id')";
    if(mysqli_query($open, $insert)){
        echo "Records added successfully to ENROLLED.";
    } else{
        echo "ERROR: Could not add records to ENROLLED. " . mysqli_error($open);
    }

Something like this might be what you want:

<?php 
if (!empty($_POST)) {
    require "insert.php";
}
?>
<html><head></head><body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <input type="text" name="course_id"><br/>
        <input type="text" name="first_last"><br/>
        <input type="text" name="stu_id"><br/>
        <input type="submit">
    </form>
</body></html>

It's possible to submit the page to itself and check if the $_POST is empty or not. If it's empty show the form of the page if not insert the data into the database.

<?php if (!empty($_POST)):
    // Clean the data coming from the MySQL tables
    $course_clean = htmlentities($_POST['course_id']);
    $stu_name_clean = htmlentities($_POST['first_last']);
    $stu_id_clean = htmlentities($_POST['stu_id']);

    // Escape user input coming from forms
    $course = mysqli_real_escape_string($open, $_REQUEST['course_id']);
    $stu_name = mysqli_real_escape_string($open, $_REQUEST['first_last']);
    $stu_id = mysqli_real_escape_string($open, $_REQUEST['stu_id']);

    // INSERT DATA 
    $insert = "INSERT IGNORE INTO enrolled (course_id, stu_id) VALUES ('$course', '$stu_id')";
    if(mysqli_query($open, $insert)){
        echo "Records added successfully to ENROLLED.";
    } else{
        echo "ERROR: Could not add records to ENROLLED. " . mysqli_error($open);
    }
else: ?>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <input type="text" name="course_id"><br/>
        <input type="text" name="first_last"><br/>
        <input type="text" name="stu_id"><br/>
        <input type="submit">
    </form>
<?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM