Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
radio button simple php form
Old 05-14-2008, 10:21 AM radio button simple php form
Experienced Talker

Posts: 41
Trades: 0
hello everyone okay ive never used radio buttons or anything of the sort so dont have a clue were to start.

basicly i need to build a simple question form with 4 answers and only one right and if the true one is selected then 1 message is printed and if false then 2 message is printed

simple but i dont know were to start...

any help is much appreciated
thankyou
Lewis
PlazaGaming is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-14-2008, 02:13 PM Re: radio button simple php form
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
You do realize that you're not asking for help, but asking for someone to code a script for you, right? Well, you couch it in "I don't know where to start", but that's after you've said, effectively:

1) I need a form with 4 options.
2) I need the script to print a success message if the selected answer is correct; a failure message otherwise.

Start with the first part -- how do you create a form? Too generic really for a forum post, so you may want to start by looking at some PHP tutorials.

Before coding, be sure to take the time to learn the concepts. Otherwise, you'll get stuck on just about everything you try to do.

If coding isn't your thing, then hiring a pro should be.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-14-2008, 02:29 PM Re: radio button simple php form
Experienced Talker

Posts: 41
Trades: 0
hiring a pro to do this what is a 5 minute job... i dont think so

i was going to post the html section but i didn't think it was really worth it as it was soo simple but i didnt know were to start with the php even after looking at tutorials as none of them worked as i wanted it to work
PlazaGaming is offline
Reply With Quote
View Public Profile
 
Old 05-14-2008, 03:41 PM Re: radio button simple php form
Average Talker

Posts: 28
Trades: 0
From your description of the problem, I would think AJAX would actually be the best tool for the job. There's no sense in doing a round trip to the server for the whole page just for a quick "You guessed wrong. Please try again." message.
Cory Dee is offline
Reply With Quote
View Public Profile
 
Old 05-14-2008, 04:46 PM Re: radio button simple php form
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
come on man, he's asking for help in how to do this, this is a help forum and yes.. it is simple

Code:
<form action="result.php" method="post">
  Answer 1 <input type="radio" name="ans" value="ans1" /><br />
  Answer 2 <input type="radio" name="ans" value="ans2"  /><br />
  Answer 3 <input type="radio" name="ans" value="ans3"  /><br />
  Answer 4 <input type="radio" name="ans" value="ans4"  /><br />
  <input type="submit" value="submit" />
</form>
result.php
PHP Code:
<?php 

$answer 
$_POST['ans'];

    if (
$answer == "ans1") {
        
        echo 
'Correct';
        
    }
    
    else {
        
        echo 
'Incorrect';
        
    }    
        
?>
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-15-2008, 05:36 AM Re: radio button simple php form
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
My point was for him/her to try. Not so much as an attempt at the code was made (or, at least, provided); but a request for the code to simply be handed over. I don't see the value in "helping" by "doing" (except in the case where the doing exposes the information necessary for education).

So, to bump this thread up to being a bit more educational, let's extend this to any number of questions with the option of a different response for each option:

PHP Code:
<?php
$available_options 
= array("op1_value"=>"Option 1",
                           
"op2_value"=>"Option 2",
                           
"op3_value"=>"Option 3",
                           
"op4_value"=>"Option 4",
                           
"op5_value"=>"Option 5",
                           
"op6_value"=>"Option 6",
                          );

$processing_message '';
if (isset(
$_POST['option_choice'])) {
  switch (
$_POST['option_choice']) {
    case 
'op1_value':
      
$processing_message 'Nice choice!';
      break;
    case 
'op2_value':
      
$processing_message 'Good choice.';
      break;
    case 
'op3_value':
      
$processing_message 'OK choice.';
      break;
    case 
'op4_value':
      
$processing_message 'Moderate choice.';
      break;
    case 
'op5_value':
      
$processing_message 'Horrible choice.';
      break;
    default:
      
$processing_message 'Incorrect';
      
//In this example, either op6_value is chosen, or something else was entered (say through a hack of the form)
      
break;
  }
}
if (
strlen($processing_message) > 0) {
  echo 
'<div class="processing_message">'.$processing_message.'</div>';
}
echo 
'<form method="post" action="'.basename(__file__).'">';
foreach(
$available_options as $option_value=>$option_label) {
  
//Notice the use of the ternary operator ()?: to check the selected radio button after the form was submitted.
  
echo $option_label.'<input type="radio" name="option_choice" value="'.$option_value.'" '.(($option_value == $_POST['option_choice'])?' checked="checked"':'').'/><br />';
}
echo 
'<input type="submit" value="Check Answer" />';
echo 
'</form>';
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-15-2008, 12:32 PM Re: radio button simple php form
Experienced Talker

Posts: 41
Trades: 0
thank you ever soo much
and it is educational reading other peoples code as then if you know the basics or even moderate knowledge then you learn how the next things work by reading the codes...
PlazaGaming is offline
Reply With Quote
View Public Profile
 
Old 05-15-2008, 02:23 PM Re: radio button simple php form
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Puh-lease people. You didn't even present an attempt at the code.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to radio button simple php form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.30362 seconds with 12 queries