Solved

Php form validation check radio button

Posted on 2013-11-25
9
620 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
  • 4
  • 4
9 Comments
 
LVL 82

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 82

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 108

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 82

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
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…

705 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