Solved

Php form validation check radio button

Posted on 2013-11-25
9
631 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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

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!

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

729 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