Solved

Radio button results to sqlserver

Posted on 2016-10-28
15
23 Views
Last Modified: 2016-11-14
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
Comment
Question by:peter Ojeda
  • 7
  • 4
  • 3
15 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41864126
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
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41864166
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
 

Author Comment

by:peter Ojeda
ID: 41864179
Thank you so much Julian it works perfectly. Much appreciated.
0
 

Author Comment

by:peter Ojeda
ID: 41864225
Sorry I thought it was working but now I have another issue. After a radio button is clicked, it cannot be unmarked
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41864235
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
 

Author Comment

by:peter Ojeda
ID: 41864244
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41864249
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41864252
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
 

Author Comment

by:peter Ojeda
ID: 41864369
Thankyou very much Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41865097
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41865137
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41865247
... 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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41865453
... 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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41866813
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now