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
Problem with radio buttons and PHP
Old 05-21-2011, 03:04 PM Problem with radio buttons and PHP
Novice Talker

Posts: 7
Trades: 0
Hello eveybody! I'm a beginner in PHP. I'm trying to make a fake poll using radio buttons and a script, and my code doesn't work to the point where I'm thinking I've gotten something completely wrong.

I don't need the answer to be stored anywhere, but I need the poll to refresh the page with a thank you message after a vote has been cast. Here is the code of the main page:

PHP Code:
<?php
    
    
if ($_POST[vote]=='1') {echo("Thank you for voting.");} 
    else {echo(
"
    <p><b>QUESTION</b></p>
    <p>&nbsp; </p>
    <form action=\"vote.php\" method=\"post\">
      <p>
        <input type=\"radio\" name=\"vote1\" value=1 > OPTION 1<br />
        <input type=\"radio\" name=\"vote2\" value=1 > OPTION 2<br />
        <input type=\"radio\" name=\"vote3\" value=1 > OPTION 3
      </p>
      <p>&nbsp;</p>
    </form> 
    <input type=\"submit\" value=\"VOTE\" />
    </form>
    "
);}
    
    
?>
and the script (vote.php):

PHP Code:
<?php 

$vote
=$_POST[vote];

header("main.php?vote=".$vote);

?>

I've been looking at tutorials but I still haven't found what the problem is... The output is crazy wrong, I'm getting fields where there should have been buttons and the ");} ?> trail of the code is showing. Please help...
mussgr is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-21-2011, 03:31 PM Re: Problem with radio buttons and PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Two closing form tags and the submit button is on the wrong side of one.

Put ALL the code in one page it does not require two seperate pages.

http://tycoontalk.freelancer.com/php...ck-button.html
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-21-2011, 03:41 PM Re: Problem with radio buttons and PHP
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
EDIT: Darn it, you beat me to it Chris :P

The $_POST in your main.php should be using $_GET since you're sending the vote parameter in the url, and you have two closing </form> tags.

Although, it would be much simpler to have it all on the same page. Something in this style
PHP Code:
if (isset($_POST['submit_vote'])) {
   
// Thank you etc.
} else {
   
// show the form

This simple checks if the form has been submited and if so says "thank you", otherwise it shows the form. The 'submit_vote' from my example should be the name of the submit button. You can ofcourse change it to what you want, as long as you add it to the button (as in <input type="submit" name="submit_vote" ... />)
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 05-21-2011 at 03:43 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-21-2011, 08:59 PM Re: Problem with radio buttons and PHP
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
Radio options in a group should all have the same name (but different values of course).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 05-22-2011, 07:11 AM Re: Problem with radio buttons and PHP
Novice Talker

Posts: 7
Trades: 0
nothing seems to work... here is the new code (I isolated it in a test script):

PHP Code:
<?php

if (isset($_POST['submit_vote'])) {
           echo(
"THANK YOU");
}     else {    
           echo(
"
    <p><b>QUESTION</b></p>
    <br /> 
    <form action=\"test.php\" method=\"POST\">
      <p>
        <input type=\"radio\" name=\"vote\" value=1 > option1<br />
        <input type=\"radio\" name=\"vote\" value=2 > option2<br />
        <input type=\"radio\" name=\"vote\" value=3 > option3
      </p>
      <br />
    <input type=\"submit\" name=\"submit_vote\"/>
    </form>
    "
);
}  

?>
and here is a screenshot of the output:



Uploaded with ImageShack.us

ignore the characters, I actually have text in Greek and I didn't bother putting in the meta tags cause this is just a test script.

could it be an external issue? something with my php?
mussgr is offline
Reply With Quote
View Public Profile
 
Old 05-22-2011, 11:13 AM Re: Problem with radio buttons and PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Well it looks like your code is outputting text inputs (the default) instead of radio buttons,

But as images can't be debugged it's impossible to actually tell why.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-22-2011, 02:48 PM
Novice Talker

Posts: 7
Trades: 0
It's ok, I solved it. I'm trying to maintain the data when going to other pages now.

should i try session_register? (sorry for doubleposting)

Last edited by mgraphic; 05-22-2011 at 03:07 PM.. Reason: double post
mussgr is offline
Reply With Quote
View Public Profile
 
Old 05-22-2011, 04:11 PM Re: Problem with radio buttons and PHP
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You could use sessions. If so first call session_start() and then use the $_SESSION array to store/read your data.

Or you could add the data as hidden fields in your form and send them again (<input type="hidden" ... />).

Or thirdly, you can have the data as a query string in the URL, as in you_page.php?var1=val1&var2=val2.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 05-22-2011 at 04:11 PM.. Reason: typo
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-22-2011, 04:18 PM Re: Problem with radio buttons and PHP
Novice Talker

Posts: 7
Trades: 0
Nah, I need the data to be maintained throughout browsing the site. Here is the code so far. (Of course it doesn't work :P)

PHP Code:
    <?php

        
if (isset($_SESSION["Submit2"])) {

            echo (
"<font size=\"+2\"><b>");
            echo (
$_SESSION["select"]);                                   

        [
stuff]          
     
        }
    else {
    echo (
"

        [stuff]

      <form method=\"POST\" action=\"session.php\">
      
        [stuff]

        <input type=\"submit\" value=\"Go\" name=\"submit2\" />

      </form>
      "
);
        }
      
?>
the script (session.php):
PHP Code:
<?PHP

session_start
();

echo(
"*");

if (isset(
$_POST['Submit2'])) {
    
    echo(
"*");
    
    
$submit2=$_POST['Submit2'];
    
    
//$_SESSION['Submit2']=submit2;
    
session_register('submit2');
}
/*
if (isset($_POST['select'])) {
$_SESSION['select']= $_POST['select'];
}
*/
//header("Location: main.php");  

?>
mussgr is offline
Reply With Quote
View Public Profile
 
Old 05-22-2011, 05:28 PM Re: Problem with radio buttons and PHP
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You need to call session_start() on every page that you use sessions on, before you can access the $_SESSION array. And you store data in it just as with a normal array:

$_SESSION['submit2'] = $submit2;
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-23-2011, 07:47 AM Re: Problem with radio buttons and PHP
Novice Talker

Posts: 7
Trades: 0
It's going ok. Does anybody know how am I gonna get my script to go back to the original page once it's ran though? Cause everything works but once the script that gets the session updated runs(getting POST data) it gets stuck there, It doesn't go back like I thought it would. I could of course use HEADER but I need this to run on every page (think a widget that updates in the same page). Is there any easier way to go other than storing the last url in a variable or something?
mussgr is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem with radio buttons and PHP
 

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 0.83010 seconds with 12 queries