Avatar of peter Ojeda
peter Ojeda
 asked on

Radio button results to sqlserver

Hi I am attempting to submit the values of my radiobuttons to my sqlserver database. I have been able to submit all other text fields so I know the connection and insert is working properly, I commented the radio buttons out so the form would work correctly, which it does, but with the radio buttons in I keep receiving the undefined index answer. Any suggestions on what I am doing wrong?

(in the form page)
 <!-- <input type="radio" name="startup"
<?php if (isset($startup) && $startup=="startup") echo "";?>
value="startup">startup
 

<input type="radio" name="pitstop"
<?php if (isset($pitstop) && $pitstop=="pitstop") echo "";?>
value="pitstop">pitstop
 
  <input type="radio" name="multifab"
<?php if (isset($multifab) && $multifab=="multifab") echo "";?>
value="multifab">multifab -->
 
(seperate insert page. I tried to define them this way since it worked in a previous form with drop down boxes results.)
      $stmt = sqlsrv_query( $conn, $sql, $params);
      if( $stmt === false ) {
             die( print_r( sqlsrv_errors(), true));
      }
      else
      {
            echo "Record add successfully";
      }

      sqlsrv_close($conn);
      if(isset($_POST['Submit']) )
{

  $varpitstop = $_POST['pitstop'];
  $varmultipfab = $_POST['multifab'];
  $varstartup = $_POST['startup'];
  $errorMessage = "";
 
}
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

Radio buttons and checkboxes that are not selected do not appear in the request variables.  You can test for isset() or empty() before using these fields.

More explanation and ideas here:
https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
peter Ojeda

ASKER
Thank you so much Julian it works perfectly. Much appreciated.
peter Ojeda

ASKER
Sorry I thought it was working but now I have another issue. After a radio button is clicked, it cannot be unmarked
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Ray Paseur

Sigh... Yes, Peter.  That's because radio buttons choose among options, with a kind of "pick one" logic.  Checkboxes choose options with "pick zero, one or many."
peter Ojeda

ASKER
Ok so from what I understand is that I have three radio buttons, which is why once clicked they have to stay clicked..correct? So if I just have one radio button and included values, that should solve the problem? Sorry I am still new to this and have only been practicing for a few weeks.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

if I just have one radio button...
Once clicked, it will stay clicked unless another input control of the same name attribute is selected.  That's just the way radio buttons work.  Checkboxes can be checked and unchecked.

After you try the example above, change the radio to checkbox and try it again.  You will see that you can omit the "none" controls, because the client can de-select a checkbox if he changes his mind.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
peter Ojeda

ASKER
Thankyou very much Ray
Ray Paseur

Peter: Here are some of my thoughts about our interactions in online forums, such as E-E.  See what you think, and please feel free to add your comments to the article!
https://www.experts-exchange.com/articles/18625/A-NICE-Approach-to-Dialog-at-E-E.html
Julian Hansen

There are ways to de-select checked radio's.
1. A clear or reset button is one way - this can be applied to just the radio's or the entire form. Disadvantages are that it is an extra control
2. Include a default option that is always checked on form enter and to which you can go back if you decide none of the other options are valid. This will also guarantee that a value is returned for the radio in the POST
3. There are JavaScript methods to uncheck a checked radio on click of the radio but they are involved because processing of the radio on click does not follow standard rules so tricks have to be used to ensure that unchecking a clicked radio stays unchecked.

My view is JavaScript should only be used when all other methods fail. Option 1 does not work for me as it is an extra control.
Option 2 also uses an extra control but with benefits
a) It ensures that a value is available in the POST
b) It is a logical addition - an extension to options that are already there
Option 3 is of interest only - I would not use it - the complexity is too high for the payoff  especially when there are other alternatives

If it were up to me I would go with option 2.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

... or just use a checkbox, if the checkbox behavior is what you want.

Here's a summary of the input controls.
http://www.w3schools.com/html/html_form_input_types.asp
Julian Hansen

... or just use a checkbox, if the checkbox behavior is what you want.
The OP lists code with 3 radio buttons which by definition are mutually exclusive. To use a check box would mean writing JavaScript to make the checkboxes mutually exclusive - which is possible - you can see more in this PAQ (https://www.experts-exchange.com/questions/28978627/Mutually-exclusive-checkbox-in-a-gridview.html?anchorAnswerId=41864155#a41864155)

Also not the right answer because checkboxes and radios by definition do different things.

The solution of providing a 4th radio that is a neutral (not selected) option solves most if not all of the OP issues (radio value not coming through and de-select)
Ray Paseur

Peter: I think this was already answered, right?  Please see: https://www.experts-exchange.com/questions/28979607/Radio-button-results-to-sqlserver.html?anchorAnswerId=41864249#a41864249 

Let us know if you still have questions, thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.