Solved

Php form validation check radio button

Posted on 2013-11-25
9
623 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 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 109

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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…
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.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

776 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