Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Radio button results to sqlserver

Posted on 2016-10-28
15
Medium Priority
?
42 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
15 Comments
 
LVL 111

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 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 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
 
LVL 1

Author Comment

by:peter Ojeda
ID: 41864179
Thank you so much Julian it works perfectly. Much appreciated.
0
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.

 
LVL 1

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 111

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
 
LVL 1

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 111

Accepted Solution

by:
Ray Paseur earned 1000 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
 
LVL 111

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
 
LVL 1

Author Comment

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

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 59

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 111

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 59

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 111

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

661 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