Solved

Php form validation check radio button

Posted on 2013-11-25
9
626 Views
Last Modified: 2013-11-25
In a contact form validation I need to echo html and check the button the user has selected.

What is the easiest way to do this?

A string set to ' ' then if statements and 'checked' written in the code?

It seems like that would work but there must be an easier/better way.
0
Comment
Question by:burnedfaceless
[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
  • 4
  • 4
9 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39675336
For a radio button group, each button should have the same name but a different value.  Then all you have to do is check the value to see which one was selected.
0
 

Author Comment

by:burnedfaceless
ID: 39675344
Thanks for responding David.

I am able to obtain the value of the button, however I am echoing html and need to select the button user selected.

So when the html is echoed.
0
 

Author Comment

by:burnedfaceless
ID: 39675351
I was going to have a string for each value then if it was the one selected have it say checked and write in every html radio button tag.

It seems like there's an easier or better way and this is a rickety structure.

so example

$commentchecked = ' ';
$inquiry checked = ' ';

the if isset (let's say comment) then $commentchecked = "checked";

then echo that string in the corresponding tag of every html radio tag.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39675361
So echo the value.  Maybe I don't understand what problem you're trying to solve.  Do you have an example code to show us?

In general, I never start out trying to make code "easier and better", I try to make it work.  Once it works, I have something to work with to make it better.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39675377
When you try to 'echo' the HTML page that you received the form data from... you end up writing at least one line for each and every value.  I have done that for some long forms with about 50 variables and it's just a lot of code.  "Easier and better" doesn't really apply because it doesn't 'work' until you have recreated all the details... one by one.
0
 

Author Comment

by:burnedfaceless
ID: 39675381
$reason = htmlspecialchars($_POST['ts']);

this obtains the value of the html radio button that is checked.

then i echo html and i want the html to retain the value the user has checked

let's say you have comment and inquiry

$reasonchecked = ' ';
$inquirychecked = ' ';

if $reason = 'comment' then $commentchecked = 'checked'
if $reason ='inquiry' then $inquirychecked = 'checked'


echo

<input type = "radio" id = "inquiry" name = "ts" value = "Inquiry" "'.$inquirychecked.'" /> Inquiry                  
      <input type = "radio" id = "comment" name = "ts" value ="Comment""'. $commentchecked.'" /> Comment      


realize syntax is off
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39675424
This is pretty HTML/PHP 101.  You might want to look into some of the learning resources in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

See http://www.laprbass.com/RAY_temp_burnedfaceless.php

<?php // RAY_temp_burnedfaceless.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO REMEMBER SELECT FORM DATA FROM ONE FORM SUBMISSION TO THE NEXT
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28303265.html


// USE THE SESSION ARRAY TO STORE THE FORM VALUES
session_start();

// THESE ARE THE ACCEPTABLE COLORS
$colors = array
( 'red'
, 'green'
, 'blue'
)
;

// IF THERE IS A REMEMBERED COLOR
$color = isset($_SESSION['color']) ? $_SESSION['color'] : NULL;

// IF THERE IS A POST-METHOD REQUEST TO SET THE COLOR
if (!empty($_POST['color']))
{
    // CHECK THAT THE POST DATA MATCHES THE FORM DATA
    if (in_array($_POST['color'], $colors))
    {
        // SAVE THE COLOR
        $_SESSION['color'] = $color = $_POST['color'];
        echo PHP_EOL . "THE COLOR IS $color";
    }
    else
    {
        trigger_error('SCRIPT UNDER ATTACK', E_USER_ERROR);
    }
}

// CREATE THE RADIO BUTTONS FOR THE COLORS
$radios = NULL;
foreach ($colors as $rgb)
{
    $radios .= '<input type="radio" name="color" value="' . $rgb . '"';
    if ($rgb == $color) $radios .= ' checked';
    $radios .= ' />' . $rgb . PHP_EOL;
}

// CREATE THE FORM USING HEREDOC SYNTAX
$form = <<<FORM
<form method="post">
$radios
<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39675426
That's pretty much what you have to do.  I know it makes for a lot of code but programming is about generating the details.  Sometimes I feel like I'm doing secretarial office work with all the repetitive typing.
0
 

Author Closing Comment

by:burnedfaceless
ID: 39675571
thanks got it working
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

730 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