Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6239315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:22:19+00:00 2026-05-24T11:22:19+00:00

I have a form with 2 sections, each of which starts out with one

  • 0

I have a form with 2 sections, each of which starts out with one row and to which further rows can be added using a script. You can see the form on the bottom of this page.

The form uses default values and I am looking for a way of not submitting rows that contain default values (to both MySQL database and via email). Because people may only complete one row (i.e. at Waged or Unwaged rate), often I will only want one row of information to be submitted. The current code for the form is below.

Thanks for any help in advance,

Nick

HTML:

<form method="post" name="booking" action="bookingengine.php">
    <fieldset>
        <h2>Waged/Organisation Rate</h2>
        <p>
            <input type="text" name="name[]">
            <input type="text" name="email[]">
            <input type="text" name="organisation[]">
            <input type="text" name="position[]">
        </p>
        <p><span class="add">Add person</span></p>
    </fieldset>

    <fieldset>
        <h2>Unwaged Rate</h2>
        <p>
            <input type="text" name="name2[]">
            <input type="text" name="email2[]">
        </p>
        <p><span class="add">Add person</span></p>
    </fieldset>

    <p><input type="submit" name="submit" id="submit" value="Submit and proceed to payment page" class="submit-button" /></p>

</form>

Script:

<script>
$(function() {
    var defaults = {
        'name[]':           'Name',
    'name2[]':           'Name',
        'email[]':          'Email',
     'email2[]':          'Email',
        'organisation[]':   'Organisation',
        'position[]':       'Position'

    };

    // separating set and remove
    // note that you could add "defaults" as an arg if you had different
    // defaults for different fieldsets
    var setDefaults = function(inputElements) {
        $(inputElements).each(function() {
            var d = defaults[this.name];
            if (d) {
                // set with jQuery
                // we don't need the data - just check on the class
                $(this).val(d)
                    .addClass('default_value');
            }
        });
    };

    var removeDefaults = function(inputElements) {
        $(inputElements).each(function() {
           if ($(this).hasClass('default_value')) {
                $(this).val('')
                   .removeClass('default_value');
           }
        });
    };

    setDefaults(jQuery('form[name=booking] input'));

    $("span.add").click(function() {
        // get the correct fieldset based on the current element
        var $fieldset = $(this).closest('fieldset');
        var $inputset = $('p', $fieldset)
                .first()
                .clone()
                .insertBefore($('p', $fieldset).last());
        // add a remove button
        $inputset.append('<span class="remove">Remove</span>');
        setDefaults($('input', $inputset));
        // return false; (only needed if this is a link)
    });

    // use delegate here to avoid adding new 
    // handlers for new elements
    $('fieldset').delegate("span.remove", {
        'click': function() {
            $(this).parent().remove();
        }
    });

    // Toggles 
    $('form[name=booking]').delegate('input', {
        'focus': function() {
            removeDefaults($(this));
        },
        'blur': function() {
            // switch to using .val() for consistency
            if (!$(this).val()) setDefaults(this);
        }
    });
 }); 
 </script>

PHP:

<?php

$emailFrom = "****";
$emailTo = "****";
$subject = "****";

$body = "****" . "\n\n";
$row_count = count($_POST['name']);
$row_count2 = count($_POST['name2']);

$values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));
     $organisation = trim(stripslashes($_POST['organisation'][$i]));
     $position = trim(stripslashes($_POST['position'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "  Organisation: " . $organisation . "   Position: " . $position . "\n\n";

     //prepare the values for MySQL
     $values[] = '(\'' . $name . '\',\'' . $email . '\',\'' . $organisation . '\',\'' . $position . '\')';

}

mysql_select_db($database, $connection);

$query1 = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES "  . implode(',', $values);

$result1 = mysql_query($query1);
if (!$result1) {
  die('Invalid query: ' . mysql_error());
}


 $body .= "****" . "\n\n";

 $values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name2'][$i]));
     $email = trim(stripslashes($_POST['email2'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "\n\n";

     //prepare the values for MySQL
     $values2[] = '(\'' . $name . '\',\'' . $email . '\')';
}
$query2 = "INSERT INTO conference (Name, Email) VALUES " . implode(',', $values2);

$result2 = mysql_query($query2);
if (!$result2) {
  die('Invalid query: ' . mysql_error());
}

// send email 
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=/conference/payment.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:22:20+00:00Added an answer on May 24, 2026 at 11:22 am

    I would use an IF statement in PHP assuming any validation you have has returned no errors.

    For example

    // SET VARIABLES
    $name2 = $_POST['name2'];
    // SET CORRECT VALUES
    if($name2 == "Surname") { $name2 = ""; }
    // RUN DB FUNCTIONS
    

    I would do this for each default value allowing me to insert just what I need or want from the default values and remove or change the rest. It also means user data remains in tact.

    I hope this helps you get on the right track 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script which dynamically adds rows to a form with default values:
I have form, where some fields are looks like rows, so I can add/delete
I've got a standard HTML form, which is split into two sections using fieldset
I have a script below which the user logs a php form. There is
I have two sections(primary and secondary) in a form with several textboxes which display
I have a website which is one page and the user navigates to each
Javascript I have code that will hide various sections in a MS CRM form
I have a form with a secondary address section. In this section you can
As you can see in the Form Widgets section, I don't have a EditText
I have form where user submits field. Field can have letters, numbers, and punctuation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.