php – Send an user to diferent form depening on input

Questions:

I have a doubt on how can I achieve sending an user to FORM1.PHP or FORM2.PHP depending on the value that He/She input in the box.

I have this:

<p><input type="number" name="xdni" class="buscador" size="15"> DNI</p>

This xdni value is unique in both tables (Called dni on the DB) contratistas and empleados

If the input match a value from contratistas it should go to FORM1.PHP.

If the input match a value from empleados it should go to FORM2.PHP.

Any help on how can I proceed? Thanks in advance!

How to&Answers:

very simple solution:

write FORM.PHP, where your form will be submitted.

check in this FORM.PHP your value is matching contratistas or empleados.

include FORM1.PHP or FORM2.PHP

other solution:

jquery -> on submit -> check value with ajax -> submit your form to the answer

###

since you’re just starting you should try something like this:

    <?php
    require_once("your_database.php")

    function inContratistas($xdni){}    // query db
    function inEmpleados($xdni){}

    if(isset($_POST['xdni'])){
        // sanitize xdni
        $xdni = $_POST['xdni'];
        if(inContratistas($xdni)){
            header("Location: form1.php");
        } else if(inEmpleados($xdni)){
            header("Location: form2.php");
        } else {
            // redirect to error 'not found'
        }
        exit();
    }
    ?>

    <html>
        <form action="temp.php" method="post">
            <p><input type="number" name="xdni" class="buscador" size="15"> DNI</p>
            <input type="submit" value="go">
        </form>
    </html>

Then you could move into javascript validation and Ajax requests.

###

Under these constraints (no client-side scripting), I would make 1 php page: FORM.PHP for example.

  • Submitting the form takes you to FORM.PHP.
  • In FORM.PHP run query searching contratistas for a match.
  • If a match is found, call a function containing the code you would have run in FORM1.PHP.
  • Else run query searching empleados for a match.
  • If a match is found, call a function containing the code you would have run in FORM2.PHP
  • Else handle the “no match” in either table scenario.

This does not handle a match in both tables which would depend on your use case.

Leave a Reply

Your email address will not be published. Required fields are marked *