سؤال

This is what I did in a nutshell:

<?php
// define variables and initialize with empty values

//error variables
$agentNameErr = "";

//non-error variables
$agentemail = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["agentname"])) {
        $agentNameErr = "Missing";
    }
else {
        $agentname = $_POST["agentname"];
    }
}

// form

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

<input name="agentname" type="text" id="agentname" autocomplete="on"  placeholder="Agent Name" value= "<?php echo htmlspecialchars($agentname);?>" />

//only shows up if $agentNameErr is assigned the value of "Missing"

<span class="error"><?php echo $agentNameErr;?></span>

</form>

It checks if $agentname is erroneous (blank). If it's not blank, I don't know how to proceed. I want it to just automatically submit all the information without any additional user input to a review page so the user can see if the name was spelled correctly. And then they can do a final submission.

I don't know MySQL.

In normal english:

//user presses the submit button

if ($agentname has error)
   stay on page and display errors
else 
   submit automatically to next page (order review page for visual checking)

What do I do to "submit automatically to next page"?

لا يوجد حل صحيح

نصائح أخرى

Use a php session.

if ($agentname has error)
   stay on page and display errors
else
{
   $_SESSION['key1'] = 'something'
   $_SESSION['key2'] = 'something else'
   ...
   header('location: ' . $next_page);
}

If you don't know how to use php sessions see the examples here

Try this:

    <?php

    $agentNameErr = "";
    $agentemail = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        if (empty($_POST["agentname"])) {
            $agentNameErr = "Missing";
        } else {
            $agentname = $_POST["agentname"];
            header("Location: nextpage.php?agent=".$agentname);
        }
    } else {

    ?>

    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

    <input name="agentname" type="text" id="agentname" autocomplete="on"  placeholder="Agent Name" value= "<?php echo htmlspecialchars($agentname);?>" />

    //only shows up if $agentNameErr is assigned the value of "Missing"
    <?php if(!empty($agentNameErr)) { ?>
    <span class="error"><?php echo $agentNameErr;?></span>
    <?php } ?>
    </form>
<?php } ?>

Keep it simple. Capture the entire contents of the form and store in a session variable.

$agentNameErr = '';
$agentemail = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (empty($_POST['agentname'])) {
        $agentNameErr = 'Missing';
    } else {
        $_SESSION['agent-form'] = $_POST;
        // Move to next page
        header('Location: nextpage.php');
        exit;
    }
}

Once forwarded to the next page you can access the form data from the session variable.

// Entire contents of form
print_r($_SESSION['agent-form']);
// "agentname" data
print_r($_SESSION['agent-form']['agentname']);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top