Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 44
  • Last Modified:

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 = "";
 
}
0
peter Ojeda
Asked:
peter Ojeda
  • 7
  • 4
  • 3
2 Solutions
 
Ray PaseurCommented:
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
0
 
Julian HansenCommented:
You can replace your assignments with these

$varpitstop   = isset($_POST['pitstop'])  ? $_POST['pitstop']  : '';
$varmultipfab = isset($_POST['multifab']) ? $_POST['multifab'] : '';
$varstartup   = isset($_POST['startup'])  ? $_POST['startup']  : '';

Open in new window


The above uses the ternary operator ? : to do the assignment. This is the same as an if then else but in a shorthand form.
Take the $pitstop assignment - what we are saying is
if $_POST['pitstop'] exists then assign that to $varpitstop otherwise assign the empty string - same as
if (isset($_POST['pitstop'])) {
  $varpitstop = $_POST['pitstop'];
}
else {
  $varpitstop = ';
}

Open in new window


The '' can be replaced with whatever you want the default value to be if a radio button is not checked.

There are other tricks you can use to force a value to be submitted such as using hidden inputs with the same name but this makes for messy markup and is not really necessary in the context of the above code.
0
 
peter OjedaAuthor Commented:
Thank you so much Julian it works perfectly. Much appreciated.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
peter OjedaAuthor Commented:
Sorry I thought it was working but now I have another issue. After a radio button is clicked, it cannot be unmarked
0
 
Ray PaseurCommented:
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."
0
 
peter OjedaAuthor Commented:
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.
0
 
Ray PaseurCommented:
See if this strategy makes sense, or consider using checkboxes instead.
https://iconoun.com/demo/temp_peter_ojeda.php
<?php // demo/temp_peter_ojeda.php
/**
 * https://www.experts-exchange.com/questions/28979607/Radio-button-results-to-sqlserver.html#a41864225
 *
 * https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
 * https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
 */
error_reporting(E_ALL);

// SHOW THE REQUEST VARIABLES
var_dump($_POST);

// HEREDOC NOTATION
$form = <<<EOF
<form method="post">
<input type="hidden" name="choice" value="none" />
<br>First  <input type="radio"  name="choice" value="first" />
<br>Second <input type="radio"  name="choice" value="second" />
<br>None   <input type="radio"  name="choice" value="none" />
<br><input type="submit" />
</form>
EOF;

echo $form;

Open in new window

0
 
Ray PaseurCommented:
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.
0
 
peter OjedaAuthor Commented:
Thankyou very much Ray
0
 
Ray PaseurCommented:
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
0
 
Julian HansenCommented:
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.
0
 
Ray PaseurCommented:
... 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
0
 
Julian HansenCommented:
... 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#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)
0
 
Ray PaseurCommented:
Peter: I think this was already answered, right?  Please see: https://www.experts-exchange.com/questions/28979607/Radio-button-results-to-sqlserver.html#a41864249 

Let us know if you still have questions, thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now