0

I am developing a website that will have ten different pages for ten different properties. Each page will have two forms one to set up an appointment and one for something else. How do I process each form? Right now I have a processing page for each form. So a total of twenty different processing pages. For all variables, names, and Ids, I add a 2..3...4..etc. for each form. Is this the correct way of doing it?

        <form role="form" id="apptform2" name="apptform2" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
            <div class="form-group cushion">
                <label for="apptName2">Full Name*</label>
                <input type="text" name="apptName2" class="form-control" id="apptName2" placeholder="Last, First">
                <?php if (isset($apptErrors2['apptNamePgTwo1'])) { echo '<div class = "pink-text"/><p>', $apptErrors2['apptNamePgTwo1'] , '</p></div>'; } ?>
                <?php if (isset($apptErrors2['apptNamePgTwo2'])) { echo '<div class = "pink-text"/><p>', $apptErrors2['apptNamePgTwo2'] , '</p></div>'; } ?>
                <?php if (isset($apptErrors2['apptNameLengthPgTwo'])) { echo '<div class = "pink-text"/><p>', $apptErrors2['apptNameLengthPgTwo'] , '</p></div>'; } ?>
            </div>
            <div class="form-group cushion">
                <label for="apptEmail2">Email address*</label>
                <input type="email" name="apptEmail2" class="form-control" id="apptEmail2" placeholder="Email">
                <?php if (isset($apptErrors2['apptEmailPgTwo1'])) { echo '<div class = "pink-text"/><p>', $apptErrors2['apptEmailPgTwo1'] , '</p></div>'; } ?>
                <?php if (isset($apptErrors2['apptEmailPgTwo2'])) { echo '<div class = "pink-text"/><p>', $apptErrors2['apptEmailPgTwo2'] , '</p></div>'; } ?>
            </div>
            <div class="form-group cushion">
                <label for="apptPhone2">Phone*</label>
                <input type="text" name="apptPhone2" class="form-control" id="apptPhone2" placeholder="Phone">
                <?php if (isset($apptErrors2['apptPhonePgTwo2'])) { echo '<div class = "pink-text"/><p>', $apptErrors2['apptPhonePgTwo2'] , '</p></div>'; } ?>
            </div>

            <div class="form-group cushion">
                <label for="apptMessage2">Message:</label>
                <textarea class="form-control" name="apptMessage2" rows="5" id="apptMessage2"></textarea>
            </div>

            <div class="cushion text-center">
                <button type="submit" name="appt-form-2" class="btn btn-danger btn-md">Request</button>
            </div>
        </form>
corelur
  • 3
  • 2
  • 2
    It sounds like you need two different form processing scripts, not twenty. It might be helpful if you add two scripts to the question so it's clear if they are duplicates (with different variable names). – JimL Jun 01 '16 at 20:18

3 Answers3

0

You only need two forms, and within each form, you add a hidden and read-only input containing the ID of that particular form, so that when the form gets submitted, your processing page will know what to do with the data. All the processing page will have to do is look at what's in the "form_id" field to know which form was submitted.

Example:

<form action="processingpage.php">
<input type="text" name="form_id" value="AppointmentForm" readonly/>
...
</form>

<form action="processingpage.php">
<input type="text" name="form_id" value="SomethingElseForm" readonly/>
...
</form>

Technically, you could do away with the hidden "form_id" input and just use two different processing pages, but the example I provided above is if you absolutely have to use just one processing page.

user3163495
  • 2,425
  • 2
  • 26
  • 43
0

If I didn't understand wrong, you want to process each of them separately by staying at the current page. Then there are 2 alternatives I can tell:

  1. One form per page This would give you a clean design and probably will be more intuitively understandable by the user.

  2. Asynchronously processing each form. You can use target iframes (How do you post to an iframe?), or use JavaScript functions to do it.

Using JavaScript is more elegant to me but requires more work. You can do something like this:

$("#form-id").submit(function(e){
   e.preventDefault();
   var form = {};
   form.field1 = $("#field1").val();
   form.field2 = $("#field2").val();
   var url = "your-submit-url";

   $.post(url, form)
    .done(function(data) {
        // do some success action
    })
    .fail(function(){
        // do some failure action
    })
    .always(function(){
        // do some action (optional) at the end
    });
});
Community
  • 1
  • 1
Ender
  • 176
  • 1
  • 6
  • `var form = $(this); $.post(form.attr('action'), form,serialize())` – Steve Jun 01 '16 at 20:52
  • Even though I prefer being explicit, what Steve is saying is a nice improvement to save some lines from filling out your form manually. – Ender Jun 01 '16 at 20:57
0

Thanks for all your help. This is how I ended up doing it. I add a hidden input type on each form.

<input type="hidden" value="Listing One" name="apptType">

I am using just one processing page. On the processing page I added this so I can identify which form is being filled out, and email the info:

        if (isset($_POST['apptType'])):

          if ($_POST['apptType']== "Listing One" ):

            $messagePt = "Some message here:";
            $headerPT ="location: some page here" ;

          endif;

          (repeat above code for each value "Listing One" thru "Listing Ten")

        end if;
corelur
  • 3
  • 2