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 8280929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:51:28+00:00 2026-06-08T09:51:28+00:00

Background I have a form that PHP generates from my database. The page is

  • 0

Background
I have a form that PHP generates from my database. The page is a seat registration for a dinner event. Each [dinner] table receives a certain number of tickets to the reception. For example, a table of 10 seats, may have 7 tickets to the Gala reception, and 3 tickets to the Chairman’s reception.

Each row has a prefix value that stores the prefixes in an array for which I loop through to get my values out. Looks something like:

<select name='prefix[]'  onchange='prefixCheck(this.value)' >
    <option value=''>Select</option>
    <option value='Mr.'selected >Mr.</option>
    <option value='Ms.' >Ms.</option>
    <option value='Dr.' >Dr.</option>
    <option value='Sen.' >Sen.</option>
    <option value='Sec.' >Sec.</option>
    <option value='Rep.' >Rep.</option>
</select>

I’ll have a varying number of these prefix[] fields on my form. I also have at the top of my page a counter of how many Gala and Chairman reception tickets a table currently has:

<td align = left>
    Total Gala Reception Tickets Available:
</td>
<td align = center>
    <input  type='text' size='2' id='gala' name='gala' value=7 style='background-color:#C8C8C8  '>
    <input  type='hidden' size='2' id='gala_orig' name='gala' value=7 >
</td>
 </tr>
 <tr>
    <td align = left>
        Total Chairman's Reception Tickets Available
    </td>
    <td align = center>
        <input type='text' id='chairman' name='chairman' size='2' value=3 style='background-color:#C8C8C8  '>
        <input type='hidden' id='chairman_orig' name='chairman' size='2' value=3 >
    </td>
 </tr>

Objective
The prefix values of Sen., Sec., Rep. are special, meaning if the user selects either of these for each seat, I would like the count for the gala to decrement, and the number for the chairman to increment. I have this javascript function, but I seem to be a critical piece. If a user selects Sen., the action occurs, but if they select another qualifiying prefix, the action happens again; decrementing gala, and incrementing chair.

function prefixCheck(v){
                gala_limit = document.getElementById('gala').value;
                chair_limit = document.getElementById('chairman').value;

                if(v == 'Sec.' || v == 'Sen.' || v == 'Rep.'){
                    gala_limit--;
                    chair_limit++;

                    document.getElementById('gala').value = gala_limit;
                    document.getElementById('chairman').value = chair_limit;
                }
                else{
                    document.getElementById('gala').value = document.getElementById('gala_orig').value;
                    document.getElementById('chairman').value = document.getElementById('chair_orig').value
                }

            }

I’d prefer not to change the object structure of the prefix array. What am I missing?

  • 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-06-08T09:51:29+00:00Added an answer on June 8, 2026 at 9:51 am

    What you’re not doing is tracking previous values that might have been selected. Your code can easily be altered to do so, however:

    1. Change 'prefixCheck(this.value) to 'prefixCheck(this) to pass a reference to the element itself.

    2. change your function a bit:


    function prefixCheck(v)
    {//v is now a reference to the changed elem:
        var wasSpecial = elem.oldValue || '';//if nothing was selected, it wasn't a special guest before
        //don't use globals, use var inside functions
        var gala_limit = document.getElementById('gala').value;
        var chair_limit = document.getElementById('chairman').value;
        if(v.value == 'Sec.' || v.value == 'Sen.' || v.value == 'Rep.')
        {
            if (wasSpecial === '')
            {
                gala_limit--;
                chair_limit++;
                if (gala_limit < 0)
                {
                    alert ('No room for you '+ v.value);
                }
            }
            document.getElementById('gala').value = gala_limit;
            document.getElementById('chairman').value = chair_limit;
            v.oldValue = 'yes';//set some oldValue property, to mark that this element has been set to a special guest value
        }
        else
        {
            if (wasSpecial !== '')
            {
                document.getElementById('gala').value = ++gala_limit;
                document.getElementById('chairman').value = --chair_limit;
            }
            if (chair_limit < 0)
            {
                alert('erm... no more chairs?');
            }
            v.oldValue = '';//always set your oldValue to mark a non special value was selected
            //I see no real reason why you should reset your counters here, but you can
            //Though I'd suggest just setting the #gala and #chairman elements when the page loads and keep using the values herein.
            //document.getElementById('gala').value = document.getElementById('gala_orig').value;
            //document.getElementById('chairman').value = document.getElementById('chair_orig').value
        }
    }
    

    You could also pass the event itself to the function: prefixCheck(this,event) and, instead of alerting, catch it (e.preventDefault() or e.returnValue = false).
    Another thing: instead of if ((v.value === 'Sen.'.... a regex v.value.match(/^[SR]e[cnp]\.$/ or even better: if (v.value.charAt(1) === 'e' will work just as well and is more readable…

    Though I’m not sure this answers your question, I hope it was at least of some use.

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

Sidebar

Related Questions

Background, Part 1 I have a form that collects both frequency and duration from
I have this form that pops up in a modal window like so: <?php
i have simple form that uploads image to server and returns php string containing
I currently have a form set up that passes some data to a php
Another question by a newbie. I have a php variable that queries the database
Another question by a newbie. I have a php variable that queries the database
I have an Html site.In that there have form for sending mail in Each
I have a form that's calling a Post a .php file, which i though
A little background, I have a client that has a legacy php site that
I have a PHP script that launches another script in the background. Recently my

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.