?
Solved

Php form validation check radio button

Posted on 2013-11-25
9
Medium Priority
?
635 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 84

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

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 84

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 111

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…
Suggested Courses

764 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